blob: a2a7fdcdd5392505704c5227ad34a39e1676bb49 [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
Rafael Espindolaf5fe2922010-12-07 15:23:23 +0000678 if (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin) {
679 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
680 return;
681 }
682
Chris Lattner6b6b5372008-06-26 18:38:35 +0000683 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000684
Eric Christopherf48f3672010-12-01 22:13:54 +0000685 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
686 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000687}
688
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000689static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000690 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000691 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000692 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000693 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000694 return;
695 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000696
Chris Lattnerc5197432009-04-14 17:02:11 +0000697 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000698 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000699 << Attr.getName() << 0 /*function*/;
700 return;
701 }
702
703 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
704}
705
706static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
707 Sema &S) {
708 // Check the attribute arguments.
709 if (Attr.getNumArgs() != 0) {
710 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
711 return;
712 }
713
714 if (!isa<FunctionDecl>(d)) {
715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
716 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000717 return;
718 }
Mike Stumpbf916502009-07-24 19:02:52 +0000719
Sean Huntcf807c42010-08-18 23:23:40 +0000720 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000721}
722
Ryan Flynn76168e22009-08-09 20:07:29 +0000723static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000724 // Check the attribute arguments.
Ryan Flynn76168e22009-08-09 20:07:29 +0000725 if (Attr.getNumArgs() != 0) {
726 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
727 return;
728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000730 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000731 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000732 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000733 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000734 return;
735 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000736 }
737
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000738 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000739}
740
Dan Gohman34c26302010-11-17 00:03:07 +0000741static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
742 // check the attribute arguments.
743 if (Attr.getNumArgs() != 0) {
744 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
745 return;
746 }
747
Dan Gohman34c26302010-11-17 00:03:07 +0000748 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
749}
750
Eric Christophera6cf1e72010-12-02 02:45:55 +0000751static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
752 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-12-03 06:58:14 +0000753 if (isa<VarDecl>(d))
754 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
755 else
756 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
757 << Attr.getName() << 12 /* variable */;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000758}
759
760static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
761 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-12-03 06:58:14 +0000762 if (isa<VarDecl>(d))
763 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
764 else
765 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
766 << Attr.getName() << 12 /* variable */;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000767}
768
Ted Kremenekb7252322009-04-10 00:01:14 +0000769static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000770 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
771 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000772 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000773}
774
775static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
776 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000777
778 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
779 // because 'analyzer_noreturn' does not impact the type.
780
781 if (Attr.getNumArgs() != 0) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
783 return;
784 }
785
786 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
787 ValueDecl *VD = dyn_cast<ValueDecl>(d);
788 if (VD == 0 || (!VD->getType()->isBlockPointerType()
789 && !VD->getType()->isFunctionPointerType())) {
790 S.Diag(Attr.getLoc(),
791 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
792 : diag::warn_attribute_wrong_decl_type)
793 << Attr.getName() << 0 /*function*/;
794 return;
795 }
796 }
797
798 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000799}
800
John Thompson35cc9622010-08-09 21:53:52 +0000801// PS3 PPU-specific.
802static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
803 Sema &S) {
804/*
805 Returning a Vector Class in Registers
806
Eric Christopherf48f3672010-12-01 22:13:54 +0000807 According to the PPU ABI specifications, a class with a single member of
808 vector type is returned in memory when used as the return value of a function.
809 This results in inefficient code when implementing vector classes. To return
810 the value in a single vector register, add the vecreturn attribute to the
811 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +0000812
813 Example:
814
815 struct Vector
816 {
817 __vector float xyzw;
818 } __attribute__((vecreturn));
819
820 Vector Add(Vector lhs, Vector rhs)
821 {
822 Vector result;
823 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
824 return result; // This will be returned in a register
825 }
826*/
John Thompson01add592010-09-18 01:12:07 +0000827 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000828 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
829 << Attr.getName() << 9 /*class*/;
830 return;
831 }
832
833 if (d->getAttr<VecReturnAttr>()) {
834 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
835 return;
836 }
837
John Thompson01add592010-09-18 01:12:07 +0000838 RecordDecl *record = cast<RecordDecl>(d);
839 int count = 0;
840
841 if (!isa<CXXRecordDecl>(record)) {
842 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
843 return;
844 }
845
846 if (!cast<CXXRecordDecl>(record)->isPOD()) {
847 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
848 return;
849 }
850
Eric Christopherf48f3672010-12-01 22:13:54 +0000851 for (RecordDecl::field_iterator iter = record->field_begin();
852 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +0000853 if ((count == 1) || !iter->getType()->isVectorType()) {
854 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
855 return;
856 }
857 count++;
858 }
859
Sean Huntcf807c42010-08-18 23:23:40 +0000860 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000861}
862
Sean Huntbbd37c62009-11-21 08:43:09 +0000863static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
864 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
865 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000866 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000867 return;
868 }
869 // FIXME: Actually store the attribute on the declaration
870}
871
Ted Kremenek73798892008-07-25 04:39:19 +0000872static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
873 // check the attribute arguments.
874 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000875 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000876 return;
877 }
Mike Stumpbf916502009-07-24 19:02:52 +0000878
John McCallaec58602010-03-31 02:47:45 +0000879 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
880 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000881 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000882 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000883 return;
884 }
Mike Stumpbf916502009-07-24 19:02:52 +0000885
Sean Huntcf807c42010-08-18 23:23:40 +0000886 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000887}
888
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000889static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
890 // check the attribute arguments.
891 if (Attr.getNumArgs() != 0) {
892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
893 return;
894 }
Mike Stumpbf916502009-07-24 19:02:52 +0000895
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000896 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000897 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000898 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
899 return;
900 }
901 } else if (!isFunctionOrMethod(d)) {
902 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000903 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000904 return;
905 }
Mike Stumpbf916502009-07-24 19:02:52 +0000906
Sean Huntcf807c42010-08-18 23:23:40 +0000907 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000908}
909
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000910static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
911 // check the attribute arguments.
912 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000913 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
914 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000915 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000916 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000917
918 int priority = 65535; // FIXME: Do not hardcode such constants.
919 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000920 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000921 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000922 if (E->isTypeDependent() || E->isValueDependent() ||
923 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000924 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000925 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000926 return;
927 }
928 priority = Idx.getZExtValue();
929 }
Mike Stumpbf916502009-07-24 19:02:52 +0000930
Chris Lattnerc5197432009-04-14 17:02:11 +0000931 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000932 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000933 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000934 return;
935 }
936
Eric Christopherf48f3672010-12-01 22:13:54 +0000937 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
938 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000939}
940
941static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
942 // check the attribute arguments.
943 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000944 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
945 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000946 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000947 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000948
949 int priority = 65535; // FIXME: Do not hardcode such constants.
950 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000951 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000952 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000953 if (E->isTypeDependent() || E->isValueDependent() ||
954 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000955 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000956 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000957 return;
958 }
959 priority = Idx.getZExtValue();
960 }
Mike Stumpbf916502009-07-24 19:02:52 +0000961
Anders Carlsson6782fc62008-08-22 22:10:48 +0000962 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000963 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000964 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000965 return;
966 }
967
Eric Christopherf48f3672010-12-01 22:13:54 +0000968 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
969 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000970}
971
Chris Lattner803d0802008-06-29 00:43:07 +0000972static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973 // check the attribute arguments.
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000974 int noArgs = Attr.getNumArgs();
975 if (noArgs > 1) {
976 S.Diag(Attr.getLoc(),
977 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000978 return;
979 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000980 // Handle the case where deprecated attribute has a text message.
981 StringLiteral *SE;
982 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000983 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000984 SE = dyn_cast<StringLiteral>(ArgExpr);
985 if (!SE) {
986 S.Diag(ArgExpr->getLocStart(),
987 diag::err_attribute_not_string) << "deprecated";
988 return;
989 }
990 }
991 else
992 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpbf916502009-07-24 19:02:52 +0000993
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000994 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
995 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000996}
997
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000998static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
999 // check the attribute arguments.
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001000 int noArgs = Attr.getNumArgs();
1001 if (noArgs > 1) {
Eric Christopherf48f3672010-12-01 22:13:54 +00001002 S.Diag(Attr.getLoc(),
1003 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001004 return;
1005 }
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001006 // Handle the case where unavailable attribute has a text message.
1007 StringLiteral *SE;
1008 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001009 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001010 SE = dyn_cast<StringLiteral>(ArgExpr);
1011 if (!SE) {
1012 S.Diag(ArgExpr->getLocStart(),
1013 diag::err_attribute_not_string) << "unavailable";
1014 return;
1015 }
1016 }
1017 else
1018 SE = StringLiteral::CreateEmpty(S.Context, 1);
1019 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
1020 SE->getString()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001021}
1022
Chris Lattner803d0802008-06-29 00:43:07 +00001023static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001024 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001025 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027 return;
1028 }
Mike Stumpbf916502009-07-24 19:02:52 +00001029
Peter Collingbourne7a730022010-11-23 20:45:58 +00001030 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001031 Arg = Arg->IgnoreParenCasts();
1032 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001033
Chris Lattner6b6b5372008-06-26 18:38:35 +00001034 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001036 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001037 return;
1038 }
Mike Stumpbf916502009-07-24 19:02:52 +00001039
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001040 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001041 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001042
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001043 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001044 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001045 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001046 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001047 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001048 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001049 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001050 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001051 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001052 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001053 return;
1054 }
Mike Stumpbf916502009-07-24 19:02:52 +00001055
Sean Huntcf807c42010-08-18 23:23:40 +00001056 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001057}
1058
Chris Lattner0db29ec2009-02-14 08:09:34 +00001059static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1060 Sema &S) {
1061 if (Attr.getNumArgs() != 0) {
1062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1063 return;
1064 }
Mike Stumpbf916502009-07-24 19:02:52 +00001065
Chris Lattner0db29ec2009-02-14 08:09:34 +00001066 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1067 if (OCI == 0) {
1068 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1069 return;
1070 }
Mike Stumpbf916502009-07-24 19:02:52 +00001071
Sean Huntcf807c42010-08-18 23:23:40 +00001072 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001073}
1074
1075static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001076 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001078 return;
1079 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001080 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001081 QualType T = TD->getUnderlyingType();
1082 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001083 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001084 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1085 return;
1086 }
1087 }
Sean Huntcf807c42010-08-18 23:23:40 +00001088 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001089}
1090
Mike Stumpbf916502009-07-24 19:02:52 +00001091static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001092HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1093 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001094 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001095 return;
1096 }
1097
1098 if (!isa<FunctionDecl>(D)) {
1099 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1100 return;
1101 }
1102
Sean Huntcf807c42010-08-18 23:23:40 +00001103 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001104}
1105
Steve Naroff9eae5762008-09-18 16:44:58 +00001106static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001107 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001108 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001109 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001110 return;
1111 }
Mike Stumpbf916502009-07-24 19:02:52 +00001112
Steve Naroff9eae5762008-09-18 16:44:58 +00001113 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001114 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001115 return;
1116 }
Mike Stumpbf916502009-07-24 19:02:52 +00001117
Sean Huntcf807c42010-08-18 23:23:40 +00001118 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001119 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001120 type = BlocksAttr::ByRef;
1121 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001122 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001123 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001124 return;
1125 }
Mike Stumpbf916502009-07-24 19:02:52 +00001126
Sean Huntcf807c42010-08-18 23:23:40 +00001127 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001128}
1129
Anders Carlsson77091822008-10-05 18:05:59 +00001130static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1131 // check the attribute arguments.
1132 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1134 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001135 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001136 }
1137
Anders Carlsson77091822008-10-05 18:05:59 +00001138 int sentinel = 0;
1139 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001140 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001141 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001142 if (E->isTypeDependent() || E->isValueDependent() ||
1143 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001144 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001145 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001146 return;
1147 }
1148 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001149
Anders Carlsson77091822008-10-05 18:05:59 +00001150 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001151 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1152 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001153 return;
1154 }
1155 }
1156
1157 int nullPos = 0;
1158 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001159 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001160 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001161 if (E->isTypeDependent() || E->isValueDependent() ||
1162 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001163 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001164 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001165 return;
1166 }
1167 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001168
Anders Carlsson77091822008-10-05 18:05:59 +00001169 if (nullPos > 1 || nullPos < 0) {
1170 // FIXME: This error message could be improved, it would be nice
1171 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001172 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1173 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001174 return;
1175 }
1176 }
1177
1178 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001179 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001180 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001181
Chris Lattner897cd902009-03-17 23:03:47 +00001182 if (isa<FunctionNoProtoType>(FT)) {
1183 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1184 return;
1185 }
Mike Stumpbf916502009-07-24 19:02:52 +00001186
Chris Lattner897cd902009-03-17 23:03:47 +00001187 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001188 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001189 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001190 }
Anders Carlsson77091822008-10-05 18:05:59 +00001191 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1192 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001193 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001194 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001195 }
1196 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001197 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1198 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001199 ;
1200 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001201 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001202 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001203 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherf48f3672010-12-01 22:13:54 +00001204 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001205 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001206 int m = Ty->isFunctionPointerType() ? 0 : 1;
1207 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001208 return;
1209 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001210 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001212 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001213 return;
1214 }
Anders Carlsson77091822008-10-05 18:05:59 +00001215 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001216 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001217 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001218 return;
1219 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001220 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1221 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001222}
1223
Chris Lattner026dc962009-02-14 07:37:35 +00001224static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1225 // check the attribute arguments.
1226 if (Attr.getNumArgs() != 0) {
1227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1228 return;
1229 }
1230
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001231 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001232 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001233 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001234 return;
1235 }
Mike Stumpbf916502009-07-24 19:02:52 +00001236
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001237 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1238 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1239 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001240 return;
1241 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001242 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1243 if (MD->getResultType()->isVoidType()) {
1244 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1245 << Attr.getName() << 1;
1246 return;
1247 }
1248
Sean Huntcf807c42010-08-18 23:23:40 +00001249 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001250}
1251
1252static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001253 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001254 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001255 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001256 return;
1257 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001258
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001259 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001260 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001261 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1262 dyn_cast<NamedDecl>(D)->getNameAsString();
1263 return;
1264 }
1265
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001266 // TODO: could also be applied to methods?
1267 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1268 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001269 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001270 return;
1271 }
Mike Stumpbf916502009-07-24 19:02:52 +00001272
Sean Huntcf807c42010-08-18 23:23:40 +00001273 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274}
1275
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001276static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1277 // check the attribute arguments.
1278 if (Attr.getNumArgs() != 0) {
1279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1280 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001281 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001282
1283 // weak_import only applies to variable & function declarations.
1284 bool isDef = false;
1285 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1286 isDef = (!VD->hasExternalStorage() || VD->getInit());
1287 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001288 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001289 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1290 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001291 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001292 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001293 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001294 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001295 !isa<ObjCInterfaceDecl>(D))
1296 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001297 << Attr.getName() << 2 /*variable and function*/;
1298 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001299 }
1300
1301 // Merge should handle any subsequent violations.
1302 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001303 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001304 diag::warn_attribute_weak_import_invalid_on_definition)
1305 << "weak_import" << 2 /*variable and function*/;
1306 return;
1307 }
1308
Sean Huntcf807c42010-08-18 23:23:40 +00001309 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001310}
1311
Nate Begeman6f3d8382009-06-26 06:32:41 +00001312static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1313 Sema &S) {
1314 // Attribute has 3 arguments.
1315 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001317 return;
1318 }
1319
1320 unsigned WGSize[3];
1321 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001322 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001323 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001324 if (E->isTypeDependent() || E->isValueDependent() ||
1325 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1327 << "reqd_work_group_size" << E->getSourceRange();
1328 return;
1329 }
1330 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1331 }
Sean Huntcf807c42010-08-18 23:23:40 +00001332 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1333 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001334 WGSize[2]));
1335}
1336
Chris Lattner026dc962009-02-14 07:37:35 +00001337static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001338 // Attribute has no arguments.
1339 if (Attr.getNumArgs() != 1) {
1340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1341 return;
1342 }
1343
1344 // Make sure that there is a string literal as the sections's single
1345 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001346 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001347 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001348 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001349 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001350 return;
1351 }
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Chris Lattner797c3c42009-08-10 19:03:04 +00001353 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001354 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001355 if (!Error.empty()) {
1356 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1357 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001358 return;
1359 }
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001361 // This attribute cannot be applied to local variables.
1362 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1363 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1364 return;
1365 }
1366
Eric Christopherf48f3672010-12-01 22:13:54 +00001367 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1368 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001369}
1370
Chris Lattner6b6b5372008-06-26 18:38:35 +00001371
Chris Lattner803d0802008-06-29 00:43:07 +00001372static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001373 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001374 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001375 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001376 return;
1377 }
Mike Stumpbf916502009-07-24 19:02:52 +00001378
Sean Huntcf807c42010-08-18 23:23:40 +00001379 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001380}
1381
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001382static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1383 // check the attribute arguments.
1384 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001385 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001386 return;
1387 }
Mike Stumpbf916502009-07-24 19:02:52 +00001388
Sean Huntcf807c42010-08-18 23:23:40 +00001389 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001390}
1391
1392static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1393 // check the attribute arguments.
1394 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001396 return;
1397 }
Mike Stumpbf916502009-07-24 19:02:52 +00001398
Sean Huntcf807c42010-08-18 23:23:40 +00001399 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001400}
1401
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001402static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001403 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001404 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1405 return;
1406 }
Mike Stumpbf916502009-07-24 19:02:52 +00001407
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001408 if (Attr.getNumArgs() != 0) {
1409 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1410 return;
1411 }
Mike Stumpbf916502009-07-24 19:02:52 +00001412
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001413 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001414
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001415 if (!VD || !VD->hasLocalStorage()) {
1416 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1417 return;
1418 }
Mike Stumpbf916502009-07-24 19:02:52 +00001419
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001420 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001421 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001422 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001423 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1424 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001425 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001426 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001427 Attr.getParameterName();
1428 return;
1429 }
Mike Stumpbf916502009-07-24 19:02:52 +00001430
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001431 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1432 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001433 S.Diag(Attr.getParameterLoc(),
1434 diag::err_attribute_cleanup_arg_not_function)
1435 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001436 return;
1437 }
1438
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001439 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001440 S.Diag(Attr.getParameterLoc(),
1441 diag::err_attribute_cleanup_func_must_take_one_arg)
1442 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001443 return;
1444 }
Mike Stumpbf916502009-07-24 19:02:52 +00001445
Anders Carlsson89941c12009-02-07 23:16:50 +00001446 // We're currently more strict than GCC about what function types we accept.
1447 // If this ever proves to be a problem it should be easy to fix.
1448 QualType Ty = S.Context.getPointerType(VD->getType());
1449 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall1c23e912010-11-16 02:32:08 +00001450 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001451 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001452 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1453 Attr.getParameterName() << ParamTy << Ty;
1454 return;
1455 }
Mike Stumpbf916502009-07-24 19:02:52 +00001456
Sean Huntcf807c42010-08-18 23:23:40 +00001457 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001458 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001459}
1460
Mike Stumpbf916502009-07-24 19:02:52 +00001461/// Handle __attribute__((format_arg((idx)))) attribute based on
1462/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1463static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001464 if (Attr.getNumArgs() != 1) {
1465 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1466 return;
1467 }
1468 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1469 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1470 << Attr.getName() << 0 /*function*/;
1471 return;
1472 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001473
1474 // In C++ the implicit 'this' function parameter also counts, and they are
1475 // counted from one.
1476 bool HasImplicitThisParam = isInstanceMethod(d);
1477 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001478 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001479
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001480 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001481 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001482 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001483 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1484 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1486 << "format" << 2 << IdxExpr->getSourceRange();
1487 return;
1488 }
Mike Stumpbf916502009-07-24 19:02:52 +00001489
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001490 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1491 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1492 << "format" << 2 << IdxExpr->getSourceRange();
1493 return;
1494 }
Mike Stumpbf916502009-07-24 19:02:52 +00001495
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001496 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001497
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001498 if (HasImplicitThisParam) {
1499 if (ArgIdx == 0) {
1500 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1501 << "format_arg" << IdxExpr->getSourceRange();
1502 return;
1503 }
1504 ArgIdx--;
1505 }
1506
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001507 // make sure the format string is really a string
1508 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001509
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001510 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1511 if (not_nsstring_type &&
1512 !isCFStringType(Ty, S.Context) &&
1513 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001514 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001515 // FIXME: Should highlight the actual expression that has the wrong type.
1516 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001517 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001518 << IdxExpr->getSourceRange();
1519 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001520 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001521 Ty = getFunctionOrMethodResultType(d);
1522 if (!isNSStringType(Ty, S.Context) &&
1523 !isCFStringType(Ty, S.Context) &&
1524 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001525 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001526 // FIXME: Should highlight the actual expression that has the wrong type.
1527 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001528 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001529 << IdxExpr->getSourceRange();
1530 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001531 }
1532
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001533 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1534 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001535}
1536
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001537enum FormatAttrKind {
1538 CFStringFormat,
1539 NSStringFormat,
1540 StrftimeFormat,
1541 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001542 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001543 InvalidFormat
1544};
1545
1546/// getFormatAttrKind - Map from format attribute names to supported format
1547/// types.
1548static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1549 // Check for formats that get handled specially.
1550 if (Format == "NSString")
1551 return NSStringFormat;
1552 if (Format == "CFString")
1553 return CFStringFormat;
1554 if (Format == "strftime")
1555 return StrftimeFormat;
1556
1557 // Otherwise, check for supported formats.
1558 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1559 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1560 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1561 Format == "zcmn_err")
1562 return SupportedFormat;
1563
Duncan Sandsbc525952010-03-23 14:44:19 +00001564 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1565 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001566 return IgnoredFormat;
1567
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001568 return InvalidFormat;
1569}
1570
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001571/// Handle __attribute__((init_priority(priority))) attributes based on
1572/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1573static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1574 Sema &S) {
1575 if (!S.getLangOptions().CPlusPlus) {
1576 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1577 return;
1578 }
1579
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001580 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1581 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1582 Attr.setInvalid();
1583 return;
1584 }
1585 QualType T = dyn_cast<VarDecl>(d)->getType();
1586 if (S.Context.getAsArrayType(T))
1587 T = S.Context.getBaseElementType(T);
1588 if (!T->getAs<RecordType>()) {
1589 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1590 Attr.setInvalid();
1591 return;
1592 }
1593
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001594 if (Attr.getNumArgs() != 1) {
1595 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1596 Attr.setInvalid();
1597 return;
1598 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001599 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001600
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001601 llvm::APSInt priority(32);
1602 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1603 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1604 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1605 << "init_priority" << priorityExpr->getSourceRange();
1606 Attr.setInvalid();
1607 return;
1608 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001609 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001610 if (prioritynum < 101 || prioritynum > 65535) {
1611 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1612 << priorityExpr->getSourceRange();
1613 Attr.setInvalid();
1614 return;
1615 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001616 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1617 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001618}
1619
Mike Stumpbf916502009-07-24 19:02:52 +00001620/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1621/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001622static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001623
Chris Lattner545dd342008-06-28 23:36:30 +00001624 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001625 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001626 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001627 return;
1628 }
1629
Chris Lattner545dd342008-06-28 23:36:30 +00001630 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001631 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001632 return;
1633 }
1634
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001635 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001636 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001637 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001638 return;
1639 }
1640
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001641 // In C++ the implicit 'this' function parameter also counts, and they are
1642 // counted from one.
1643 bool HasImplicitThisParam = isInstanceMethod(d);
1644 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001645 unsigned FirstIdx = 1;
1646
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001647 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001648
1649 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001650 if (Format.startswith("__") && Format.endswith("__"))
1651 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001652
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001653 // Check for supported formats.
1654 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001655
1656 if (Kind == IgnoredFormat)
1657 return;
1658
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001659 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001661 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001662 return;
1663 }
1664
1665 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001666 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001667 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001668 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1669 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001670 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001671 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001672 return;
1673 }
1674
1675 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001676 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001677 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001678 return;
1679 }
1680
1681 // FIXME: Do we need to bounds check?
1682 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001683
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001684 if (HasImplicitThisParam) {
1685 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001686 S.Diag(Attr.getLoc(),
1687 diag::err_format_attribute_implicit_this_format_string)
1688 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001689 return;
1690 }
1691 ArgIdx--;
1692 }
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Chris Lattner6b6b5372008-06-26 18:38:35 +00001694 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001695 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001696
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001697 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001698 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001699 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1700 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001701 return;
1702 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001703 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001704 // FIXME: do we need to check if the type is NSString*? What are the
1705 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001706 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001707 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001708 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1709 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001710 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001711 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001712 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001713 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001714 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001715 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1716 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001717 return;
1718 }
1719
1720 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001721 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001722 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001723 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1724 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001725 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001726 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001727 return;
1728 }
1729
1730 // check if the function is variadic if the 3rd argument non-zero
1731 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001732 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001733 ++NumArgs; // +1 for ...
1734 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001735 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001736 return;
1737 }
1738 }
1739
Chris Lattner3c73c412008-11-19 08:23:25 +00001740 // strftime requires FirstArg to be 0 because it doesn't read from any
1741 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001742 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001743 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001744 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1745 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001746 return;
1747 }
1748 // if 0 it disables parameter checking (to use with e.g. va_list)
1749 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001751 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001752 return;
1753 }
1754
Sean Huntcf807c42010-08-18 23:23:40 +00001755 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1756 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001757 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001758}
1759
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001760static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1761 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001762 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001763 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001764 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001765 return;
1766 }
1767
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001768 // Try to find the underlying union declaration.
1769 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001770 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001771 if (TD && TD->getUnderlyingType()->isUnionType())
1772 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1773 else
1774 RD = dyn_cast<RecordDecl>(d);
1775
1776 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001777 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001778 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001779 return;
1780 }
1781
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001782 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001783 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001784 diag::warn_transparent_union_attribute_not_definition);
1785 return;
1786 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001787
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001788 RecordDecl::field_iterator Field = RD->field_begin(),
1789 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001790 if (Field == FieldEnd) {
1791 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1792 return;
1793 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001794
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001795 FieldDecl *FirstField = *Field;
1796 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001797 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001798 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001799 diag::warn_transparent_union_attribute_floating)
1800 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001801 return;
1802 }
1803
1804 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1805 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1806 for (; Field != FieldEnd; ++Field) {
1807 QualType FieldType = Field->getType();
1808 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1809 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1810 // Warn if we drop the attribute.
1811 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001812 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001813 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001814 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001815 diag::warn_transparent_union_attribute_field_size_align)
1816 << isSize << Field->getDeclName() << FieldBits;
1817 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001818 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001819 diag::note_transparent_union_first_field_size_align)
1820 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001821 return;
1822 }
1823 }
1824
Sean Huntcf807c42010-08-18 23:23:40 +00001825 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001826}
1827
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001828static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001829 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001830 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001831 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001832 return;
1833 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001834 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001835 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001836
Chris Lattner6b6b5372008-06-26 18:38:35 +00001837 // Make sure that there is a string literal as the annotation's single
1838 // argument.
1839 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001840 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001841 return;
1842 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001843 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1844 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001845}
1846
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001847static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001848 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001849 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001851 return;
1852 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001853
1854 //FIXME: The C++0x version of this attribute has more limited applicabilty
1855 // than GNU's, and should error out when it is used to specify a
1856 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001857
Chris Lattner545dd342008-06-28 23:36:30 +00001858 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001859 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001860 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001861 }
Mike Stumpbf916502009-07-24 19:02:52 +00001862
Peter Collingbourne7a730022010-11-23 20:45:58 +00001863 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001864}
1865
1866void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1867 if (E->isTypeDependent() || E->isValueDependent()) {
1868 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001869 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001870 return;
1871 }
1872
Sean Huntcf807c42010-08-18 23:23:40 +00001873 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001874 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001875 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1876 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1877 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001878 return;
1879 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001880 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001881 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1882 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001883 return;
1884 }
1885
Sean Huntcf807c42010-08-18 23:23:40 +00001886 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1887}
1888
1889void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1890 // FIXME: Cache the number on the Attr object if non-dependent?
1891 // FIXME: Perform checking of type validity
1892 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1893 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001894}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001895
Mike Stumpbf916502009-07-24 19:02:52 +00001896/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1897/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001898///
Mike Stumpbf916502009-07-24 19:02:52 +00001899/// Despite what would be logical, the mode attribute is a decl attribute, not a
1900/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1901/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001902static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001903 // This attribute isn't documented, but glibc uses it. It changes
1904 // the width of an int or unsigned int to the specified size.
1905
1906 // Check that there aren't any arguments
1907 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001908 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001909 return;
1910 }
1911
1912 IdentifierInfo *Name = Attr.getParameterName();
1913 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001914 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001915 return;
1916 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001917
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001918 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001919
1920 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001921 if (Str.startswith("__") && Str.endswith("__"))
1922 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001923
1924 unsigned DestWidth = 0;
1925 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001926 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001927 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001928 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001929 switch (Str[0]) {
1930 case 'Q': DestWidth = 8; break;
1931 case 'H': DestWidth = 16; break;
1932 case 'S': DestWidth = 32; break;
1933 case 'D': DestWidth = 64; break;
1934 case 'X': DestWidth = 96; break;
1935 case 'T': DestWidth = 128; break;
1936 }
1937 if (Str[1] == 'F') {
1938 IntegerMode = false;
1939 } else if (Str[1] == 'C') {
1940 IntegerMode = false;
1941 ComplexMode = true;
1942 } else if (Str[1] != 'I') {
1943 DestWidth = 0;
1944 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001945 break;
1946 case 4:
1947 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1948 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001949 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001950 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001951 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001952 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001953 break;
1954 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001955 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001956 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001957 break;
1958 }
1959
1960 QualType OldTy;
1961 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1962 OldTy = TD->getUnderlyingType();
1963 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1964 OldTy = VD->getType();
1965 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001966 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1967 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001968 return;
1969 }
Eli Friedman73397492009-03-03 06:41:03 +00001970
John McCall183700f2009-09-21 23:43:11 +00001971 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001972 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1973 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001974 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001975 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1976 } else if (ComplexMode) {
1977 if (!OldTy->isComplexType())
1978 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1979 } else {
1980 if (!OldTy->isFloatingType())
1981 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1982 }
1983
Mike Stump390b4cc2009-05-16 07:39:55 +00001984 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1985 // and friends, at least with glibc.
1986 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1987 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001988 // FIXME: Make sure floating-point mappings are accurate
1989 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001990 QualType NewTy;
1991 switch (DestWidth) {
1992 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001993 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001994 return;
1995 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001996 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001997 return;
1998 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001999 if (!IntegerMode) {
2000 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2001 return;
2002 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002003 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002004 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002005 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002006 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002007 break;
2008 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002009 if (!IntegerMode) {
2010 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2011 return;
2012 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002013 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002014 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002015 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002016 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002017 break;
2018 case 32:
2019 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002020 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002021 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002022 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002023 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002024 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002025 break;
2026 case 64:
2027 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002028 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002029 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002030 if (S.Context.Target.getLongWidth() == 64)
2031 NewTy = S.Context.LongTy;
2032 else
2033 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002034 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002035 if (S.Context.Target.getLongWidth() == 64)
2036 NewTy = S.Context.UnsignedLongTy;
2037 else
2038 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002039 break;
Eli Friedman73397492009-03-03 06:41:03 +00002040 case 96:
2041 NewTy = S.Context.LongDoubleTy;
2042 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002043 case 128:
2044 if (!IntegerMode) {
2045 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2046 return;
2047 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002048 if (OldTy->isSignedIntegerType())
2049 NewTy = S.Context.Int128Ty;
2050 else
2051 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002052 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002053 }
2054
Eli Friedman73397492009-03-03 06:41:03 +00002055 if (ComplexMode) {
2056 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002057 }
2058
2059 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002060 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2061 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002062 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002063 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002064 cast<ValueDecl>(D)->setType(NewTy);
2065}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002066
Mike Stump1feade82009-08-26 22:31:08 +00002067static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002068 // check the attribute arguments.
2069 if (Attr.getNumArgs() > 0) {
2070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2071 return;
2072 }
Anders Carlssone896d982009-02-13 08:11:52 +00002073
Anders Carlsson5bab7882009-02-19 19:16:48 +00002074 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002076 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00002077 return;
2078 }
Mike Stumpbf916502009-07-24 19:02:52 +00002079
Sean Huntcf807c42010-08-18 23:23:40 +00002080 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002081}
2082
Mike Stump1feade82009-08-26 22:31:08 +00002083static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002084 // check the attribute arguments.
2085 if (Attr.getNumArgs() != 0) {
2086 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2087 return;
2088 }
Mike Stumpbf916502009-07-24 19:02:52 +00002089
Chris Lattnerc5197432009-04-14 17:02:11 +00002090 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002091 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002092 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002093 return;
2094 }
Mike Stumpbf916502009-07-24 19:02:52 +00002095
Sean Huntcf807c42010-08-18 23:23:40 +00002096 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002097}
2098
Chris Lattner7255a2d2010-06-22 00:03:40 +00002099static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2100 Sema &S) {
2101 // check the attribute arguments.
2102 if (Attr.getNumArgs() != 0) {
2103 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2104 return;
2105 }
2106
2107 if (!isa<FunctionDecl>(d)) {
2108 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2109 << Attr.getName() << 0 /*function*/;
2110 return;
2111 }
2112
Eric Christopherf48f3672010-12-01 22:13:54 +00002113 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2114 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002115}
2116
Peter Collingbourneced76712010-12-01 03:15:31 +00002117static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2118 if (S.LangOpts.CUDA) {
2119 // check the attribute arguments.
2120 if (Attr.getNumArgs() != 0) {
2121 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2122 return;
2123 }
2124
2125 if (!isa<VarDecl>(d)) {
2126 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2127 << Attr.getName() << 12 /*variable*/;
2128 return;
2129 }
2130
2131 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2132 } else {
2133 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2134 }
2135}
2136
2137static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2138 if (S.LangOpts.CUDA) {
2139 // check the attribute arguments.
2140 if (Attr.getNumArgs() != 0) {
2141 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2142 return;
2143 }
2144
2145 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2146 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2147 << Attr.getName() << 2 /*variable and function*/;
2148 return;
2149 }
2150
2151 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2152 } else {
2153 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2154 }
2155}
2156
2157static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2158 if (S.LangOpts.CUDA) {
2159 // check the attribute arguments.
2160 if (Attr.getNumArgs() != 0) {
2161 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2162 return;
2163 }
2164
2165 if (!isa<FunctionDecl>(d)) {
2166 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2167 << Attr.getName() << 0 /*function*/;
2168 return;
2169 }
2170
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002171 FunctionDecl *FD = cast<FunctionDecl>(d);
2172 if (!FD->getResultType()->isVoidType()) {
2173 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc();
2174 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2175 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2176 << FD->getType()
2177 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2178 "void");
2179 } else {
2180 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2181 << FD->getType();
2182 }
2183 return;
2184 }
2185
Peter Collingbourneced76712010-12-01 03:15:31 +00002186 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2187 } else {
2188 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2189 }
2190}
2191
2192static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2193 if (S.LangOpts.CUDA) {
2194 // check the attribute arguments.
2195 if (Attr.getNumArgs() != 0) {
2196 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2197 return;
2198 }
2199
2200 if (!isa<FunctionDecl>(d)) {
2201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2202 << Attr.getName() << 0 /*function*/;
2203 return;
2204 }
2205
2206 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2207 } else {
2208 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2209 }
2210}
2211
2212static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2213 if (S.LangOpts.CUDA) {
2214 // check the attribute arguments.
2215 if (Attr.getNumArgs() != 0) {
2216 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2217 return;
2218 }
2219
2220 if (!isa<VarDecl>(d)) {
2221 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2222 << Attr.getName() << 12 /*variable*/;
2223 return;
2224 }
2225
2226 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2227 } else {
2228 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2229 }
2230}
2231
Chris Lattnercf2a7212009-04-20 19:12:28 +00002232static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002233 // check the attribute arguments.
2234 if (Attr.getNumArgs() != 0) {
2235 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2236 return;
2237 }
Mike Stumpbf916502009-07-24 19:02:52 +00002238
Chris Lattnerc5197432009-04-14 17:02:11 +00002239 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2240 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002241 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002242 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002243 return;
2244 }
Mike Stumpbf916502009-07-24 19:02:52 +00002245
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002246 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002247 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002248 return;
2249 }
Mike Stumpbf916502009-07-24 19:02:52 +00002250
Sean Huntcf807c42010-08-18 23:23:40 +00002251 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002252}
2253
Abramo Bagnarae215f722010-04-30 13:10:51 +00002254static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2255 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2256 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2257 assert(Attr.isInvalid() == false);
2258
2259 switch (Attr.getKind()) {
2260 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002261 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002262 return;
2263 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002264 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002265 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002266 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00002267 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002268 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002269 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00002270 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002271 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002272 case AttributeList::AT_pascal:
2273 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2274 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002275 default:
2276 llvm_unreachable("unexpected attribute kind");
2277 return;
2278 }
2279}
2280
Fariborz Jahanianee760332009-03-27 18:38:55 +00002281static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2282 // check the attribute arguments.
2283 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002285 return;
2286 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002287
Fariborz Jahanianee760332009-03-27 18:38:55 +00002288 if (!isFunctionOrMethod(d)) {
2289 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002290 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002291 return;
2292 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002293
Peter Collingbourne7a730022010-11-23 20:45:58 +00002294 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002295 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002296 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2297 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002298 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2299 << "regparm" << NumParamsExpr->getSourceRange();
2300 return;
2301 }
2302
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002303 if (S.Context.Target.getRegParmMax() == 0) {
2304 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002305 << NumParamsExpr->getSourceRange();
2306 return;
2307 }
2308
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002309 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002310 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2311 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002312 return;
2313 }
2314
Sean Huntcf807c42010-08-18 23:23:40 +00002315 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2316 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002317}
2318
Sean Huntbbd37c62009-11-21 08:43:09 +00002319static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2320 // check the attribute arguments.
2321 if (Attr.getNumArgs() != 0) {
2322 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2323 return;
2324 }
2325
2326 if (!isa<CXXRecordDecl>(d)
2327 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2328 S.Diag(Attr.getLoc(),
2329 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2330 : diag::warn_attribute_wrong_decl_type)
2331 << Attr.getName() << 7 /*virtual method or class*/;
2332 return;
2333 }
Sean Hunt7725e672009-11-25 04:20:27 +00002334
2335 // FIXME: Conform to C++0x redeclaration rules.
2336
2337 if (d->getAttr<FinalAttr>()) {
2338 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2339 return;
2340 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002341
Sean Huntcf807c42010-08-18 23:23:40 +00002342 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002343}
2344
Chris Lattner0744e5f2008-06-29 00:23:49 +00002345//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002346// C++0x member checking attributes
2347//===----------------------------------------------------------------------===//
2348
2349static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2350 if (Attr.getNumArgs() != 0) {
2351 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2352 return;
2353 }
2354
2355 if (!isa<CXXRecordDecl>(d)) {
2356 S.Diag(Attr.getLoc(),
2357 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2358 : diag::warn_attribute_wrong_decl_type)
2359 << Attr.getName() << 9 /*class*/;
2360 return;
2361 }
2362
2363 if (d->getAttr<BaseCheckAttr>()) {
2364 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2365 return;
2366 }
2367
Sean Huntcf807c42010-08-18 23:23:40 +00002368 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002369}
2370
2371static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2372 if (Attr.getNumArgs() != 0) {
2373 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2374 return;
2375 }
2376
2377 if (!isa<RecordDecl>(d->getDeclContext())) {
2378 // FIXME: It's not the type that's the problem
2379 S.Diag(Attr.getLoc(),
2380 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2381 : diag::warn_attribute_wrong_decl_type)
2382 << Attr.getName() << 11 /*member*/;
2383 return;
2384 }
2385
2386 // FIXME: Conform to C++0x redeclaration rules.
2387
2388 if (d->getAttr<HidingAttr>()) {
2389 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2390 return;
2391 }
2392
Sean Huntcf807c42010-08-18 23:23:40 +00002393 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002394}
2395
2396static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2397 if (Attr.getNumArgs() != 0) {
2398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2399 return;
2400 }
2401
2402 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2403 // FIXME: It's not the type that's the problem
2404 S.Diag(Attr.getLoc(),
2405 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2406 : diag::warn_attribute_wrong_decl_type)
2407 << Attr.getName() << 10 /*virtual method*/;
2408 return;
2409 }
2410
2411 // FIXME: Conform to C++0x redeclaration rules.
2412
2413 if (d->getAttr<OverrideAttr>()) {
2414 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2415 return;
2416 }
2417
Sean Huntcf807c42010-08-18 23:23:40 +00002418 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002419}
2420
2421//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002422// Checker-specific attribute handlers.
2423//===----------------------------------------------------------------------===//
2424
2425static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2426 Sema &S) {
2427
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002428 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002429
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002430 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2431 RetTy = MD->getResultType();
2432 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2433 RetTy = FD->getResultType();
2434 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002435 SourceLocation L = Attr.getLoc();
2436 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2437 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002438 return;
2439 }
Mike Stumpbf916502009-07-24 19:02:52 +00002440
Ted Kremenek6217b802009-07-29 21:53:49 +00002441 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002442 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002443 SourceLocation L = Attr.getLoc();
2444 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2445 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002446 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002447 }
Mike Stumpbf916502009-07-24 19:02:52 +00002448
Ted Kremenekb71368d2009-05-09 02:44:38 +00002449 switch (Attr.getKind()) {
2450 default:
2451 assert(0 && "invalid ownership attribute");
2452 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002453 case AttributeList::AT_cf_returns_not_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002454 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
2455 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002456 return;
2457 case AttributeList::AT_ns_returns_not_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002458 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
2459 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002460 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002461 case AttributeList::AT_cf_returns_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002462 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
2463 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002464 return;
2465 case AttributeList::AT_ns_returns_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002466 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
2467 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002468 return;
2469 };
2470}
2471
Charles Davisf0122fe2010-02-16 18:27:26 +00002472static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2473 return Attr.getKind() == AttributeList::AT_dllimport ||
2474 Attr.getKind() == AttributeList::AT_dllexport;
2475}
2476
Ted Kremenekb71368d2009-05-09 02:44:38 +00002477//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002478// Top Level Sema Entry Points
2479//===----------------------------------------------------------------------===//
2480
Sebastian Redla89d82c2008-12-21 19:24:58 +00002481/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002482/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002483/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2484/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002485static void ProcessDeclAttribute(Scope *scope, Decl *D,
2486 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002487 if (Attr.isInvalid())
2488 return;
2489
Charles Davisf0122fe2010-02-16 18:27:26 +00002490 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2491 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002492 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002493 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002494 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002495 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2496 case AttributeList::AT_IBOutletCollection:
2497 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002498 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002499 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002500 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002501 case AttributeList::AT_neon_vector_type:
2502 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002503 // Ignore these, these are type attributes, handled by
2504 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002505 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002506 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2507 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002508 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002509 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002510 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002511 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002512 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2513 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002514 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002515 HandleDependencyAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002516 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002517 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002518 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2519 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2520 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002521 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002522 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002523 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002524 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002525 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2526 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2527 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002528 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002529 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2530 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002531 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002532 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2533 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002534 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002535 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002536 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002537 case AttributeList::AT_ownership_returns:
2538 case AttributeList::AT_ownership_takes:
2539 case AttributeList::AT_ownership_holds:
2540 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002541 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002542 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2543 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2544 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002545 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002546 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002547
2548 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002549 case AttributeList::AT_ns_returns_not_retained:
2550 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002551 case AttributeList::AT_ns_returns_retained:
2552 case AttributeList::AT_cf_returns_retained:
2553 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2554
Nate Begeman6f3d8382009-06-26 06:32:41 +00002555 case AttributeList::AT_reqd_wg_size:
2556 HandleReqdWorkGroupSize(D, Attr, S); break;
2557
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002558 case AttributeList::AT_init_priority:
2559 HandleInitPriorityAttr(D, Attr, S); break;
2560
Sean Hunt7725e672009-11-25 04:20:27 +00002561 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2562 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002563 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2564 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2565 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002566 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002567 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2568 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002569 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002570 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002571 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002572 case AttributeList::AT_transparent_union:
2573 HandleTransparentUnionAttr(D, Attr, S);
2574 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002575 case AttributeList::AT_objc_exception:
2576 HandleObjCExceptionAttr(D, Attr, S);
2577 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002578 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002579 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2580 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2581 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2582 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2583 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2584 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2585 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2586 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2587 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002588 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002589 // Just ignore
2590 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002591 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2592 HandleNoInstrumentFunctionAttr(D, Attr, S);
2593 break;
John McCall04a67a62010-02-05 21:31:56 +00002594 case AttributeList::AT_stdcall:
2595 case AttributeList::AT_cdecl:
2596 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002597 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002598 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002599 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002600 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002601 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002602 // Ask target about the attribute.
2603 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2604 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002605 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2606 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002607 break;
2608 }
2609}
2610
2611/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2612/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00002613void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
2614 const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002615 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2616 ProcessDeclAttribute(S, D, *l, *this);
2617 }
2618
2619 // GCC accepts
2620 // static int a9 __attribute__((weakref));
2621 // but that looks really pointless. We reject it.
2622 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2623 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002624 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002625 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002626 }
2627}
2628
Ryan Flynne25ff832009-07-30 03:15:39 +00002629/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2630/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002631NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002632 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002633 NamedDecl *NewD = 0;
2634 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2635 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2636 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002637 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002638 if (FD->getQualifier()) {
2639 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2640 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2641 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002642 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2643 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2644 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002645 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002646 VD->getStorageClass(),
2647 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002648 if (VD->getQualifier()) {
2649 VarDecl *NewVD = cast<VarDecl>(NewD);
2650 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2651 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002652 }
2653 return NewD;
2654}
2655
2656/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2657/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002658void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002659 if (W.getUsed()) return; // only do this once
2660 W.setUsed(true);
2661 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2662 IdentifierInfo *NDId = ND->getIdentifier();
2663 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002664 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2665 NDId->getName()));
2666 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002667 WeakTopLevelDecl.push_back(NewD);
2668 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2669 // to insert Decl at TU scope, sorry.
2670 DeclContext *SavedContext = CurContext;
2671 CurContext = Context.getTranslationUnitDecl();
2672 PushOnScopeChains(NewD, S);
2673 CurContext = SavedContext;
2674 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002675 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002676 }
2677}
2678
Chris Lattner0744e5f2008-06-29 00:23:49 +00002679/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2680/// it, apply them to D. This is a bit tricky because PD can have attributes
2681/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002682void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
John McCalld4aff0e2010-10-27 00:59:00 +00002683 // It's valid to "forward-declare" #pragma weak, in which case we
2684 // have to do this.
2685 if (!WeakUndeclaredIdentifiers.empty()) {
2686 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2687 if (IdentifierInfo *Id = ND->getIdentifier()) {
2688 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2689 = WeakUndeclaredIdentifiers.find(Id);
2690 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2691 WeakInfo W = I->second;
2692 DeclApplyPragmaWeak(S, ND, W);
2693 WeakUndeclaredIdentifiers[Id] = W;
2694 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002695 }
2696 }
2697 }
2698
Chris Lattner0744e5f2008-06-29 00:23:49 +00002699 // Apply decl attributes from the DeclSpec if present.
2700 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002701 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002702
Chris Lattner0744e5f2008-06-29 00:23:49 +00002703 // Walk the declarator structure, applying decl attributes that were in a type
2704 // position to the decl itself. This handles cases like:
2705 // int *__attr__(x)** D;
2706 // when X is a decl attribute.
2707 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2708 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002709 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002710
Chris Lattner0744e5f2008-06-29 00:23:49 +00002711 // Finally, apply any attributes on the decl itself.
2712 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002713 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002714}
John McCall54abf7d2009-11-04 02:18:39 +00002715
2716/// PushParsingDeclaration - Enter a new "scope" of deprecation
2717/// warnings.
2718///
2719/// The state token we use is the start index of this scope
2720/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002721Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002722 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002723 return (ParsingDeclStackState) DelayedDiagnostics.size();
2724}
2725
John McCalld226f652010-08-21 09:40:31 +00002726void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002727 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2728 ParsingDeclDepth--;
2729
2730 if (DelayedDiagnostics.empty())
2731 return;
2732
2733 unsigned SavedIndex = (unsigned) S;
2734 assert(SavedIndex <= DelayedDiagnostics.size() &&
2735 "saved index is out of bounds");
2736
John McCall58e6f342010-03-16 05:22:47 +00002737 unsigned E = DelayedDiagnostics.size();
2738
John McCall2f514482010-01-27 03:50:35 +00002739 // We only want to actually emit delayed diagnostics when we
2740 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002741 if (D) {
2742 // We really do want to start with 0 here. We get one push for a
2743 // decl spec and another for each declarator; in a decl group like:
2744 // deprecated_typedef foo, *bar, baz();
2745 // only the declarator pops will be passed decls. This is correct;
2746 // we really do need to consider delayed diagnostics from the decl spec
2747 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002748 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002749 if (DelayedDiagnostics[I].Triggered)
2750 continue;
2751
2752 switch (DelayedDiagnostics[I].Kind) {
2753 case DelayedDiagnostic::Deprecation:
2754 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2755 break;
2756
2757 case DelayedDiagnostic::Access:
2758 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2759 break;
2760 }
2761 }
2762 }
2763
John McCall58e6f342010-03-16 05:22:47 +00002764 // Destroy all the delayed diagnostics we're about to pop off.
2765 for (unsigned I = SavedIndex; I != E; ++I)
2766 DelayedDiagnostics[I].destroy();
2767
John McCall2f514482010-01-27 03:50:35 +00002768 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002769}
2770
2771static bool isDeclDeprecated(Decl *D) {
2772 do {
2773 if (D->hasAttr<DeprecatedAttr>())
2774 return true;
2775 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2776 return false;
2777}
2778
John McCall9c3087b2010-08-26 02:13:20 +00002779void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002780 Decl *Ctx) {
2781 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002782 return;
2783
John McCall2f514482010-01-27 03:50:35 +00002784 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002785 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002786 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002787 << DD.getDeprecationDecl()->getDeclName()
2788 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002789 else
2790 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002791 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002792}
2793
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002794void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002795 SourceLocation Loc) {
John McCall54abf7d2009-11-04 02:18:39 +00002796 // Delay if we're currently parsing a declaration.
2797 if (ParsingDeclDepth) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002798 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2799 Message));
John McCall54abf7d2009-11-04 02:18:39 +00002800 return;
2801 }
2802
2803 // Otherwise, don't warn if our current context is deprecated.
2804 if (isDeclDeprecated(cast<Decl>(CurContext)))
2805 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002806 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002807 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2808 << Message;
2809 else
2810 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002811}