blob: 1776d46bd76dd67d153099273bff07b4737d6e60 [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
Douglas Gregore737f502010-08-12 20:07:10 +000014#include "clang/Sema/Sema.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"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremeneka18d7d82009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000039
Chris Lattner6b6b5372008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000044
John McCall183700f2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000046}
47
Daniel Dunbar35682492008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopesd20254f2009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000062}
63
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076}
77
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000084 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000106
Chris Lattner89951a82009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000108}
109
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000121 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000122 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner6b6b5372008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000131
John McCall506b57e2010-05-17 21:00:27 +0000132 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
133 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000134 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000135
John McCall506b57e2010-05-17 21:00:27 +0000136 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000137
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000153 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpbf916502009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000172 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 }
Mike Stumpbf916502009-07-24 19:02:52 +0000174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204}
205
Chris Lattner803d0802008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Sean Huntcf807c42010-08-18 23:23:40 +0000214 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 else
Sean Huntcf807c42010-08-18 23:23:40 +0000223 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226}
227
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpbf916502009-07-24 19:02:52 +0000234
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000238 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000255 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000256 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000257 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000260}
261
Ted Kremenek857e9182010-05-19 17:38:06 +0000262static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
263 Sema &S) {
264
265 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000266 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
268 return;
269 }
270
271 // The IBOutletCollection attributes only apply to instance variables of
272 // Objective-C classes.
273 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
274 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
275 return;
276 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000277 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
278 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
279 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
280 << VD->getType() << 0;
281 return;
282 }
283 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
284 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
285 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
286 << PD->getType() << 1;
287 return;
288 }
289
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000290 IdentifierInfo *II = Attr.getParameterName();
291 if (!II)
292 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000293
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000294 Sema::TypeTy *TypeRep = S.getTypeName(*II, Attr.getLoc(),
295 S.getScopeForContext(d->getDeclContext()->getParent()));
296 if (!TypeRep) {
297 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
298 return;
299 }
300 QualType QT(QualType::getFromOpaquePtr(TypeRep));
301 // Diagnose use of non-object type in iboutletcollection attribute.
302 // FIXME. Gnu attribute extension ignores use of builtin types in
303 // attributes. So, __attribute__((iboutletcollection(char))) will be
304 // treated as __attribute__((iboutletcollection())).
305 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
306 !QT->isObjCObjectType()) {
307 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
308 return;
309 }
Sean Huntcf807c42010-08-18 23:23:40 +0000310 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
311 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000312}
313
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000314static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000315 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
316 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000317 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000318 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000319 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000320 return;
321 }
Mike Stumpbf916502009-07-24 19:02:52 +0000322
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000323 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000324
325 // The nonnull attribute only applies to pointers.
326 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000327
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000328 for (AttributeList::arg_iterator I=Attr.arg_begin(),
329 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000330
331
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000333 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000334 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000335 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
336 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000339 return;
340 }
Mike Stumpbf916502009-07-24 19:02:52 +0000341
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000342 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000343
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000344 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000346 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000347 return;
348 }
Mike Stumpbf916502009-07-24 19:02:52 +0000349
Ted Kremenek465172f2008-07-21 22:09:15 +0000350 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351
352 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000353 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000354 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000355 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000356 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000357 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000358 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000359 }
Mike Stumpbf916502009-07-24 19:02:52 +0000360
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361 NonNullArgs.push_back(x);
362 }
Mike Stumpbf916502009-07-24 19:02:52 +0000363
364 // If no arguments were specified to __attribute__((nonnull)) then all pointer
365 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000366 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000367 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
368 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000369 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000370 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000371 }
Mike Stumpbf916502009-07-24 19:02:52 +0000372
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000373 if (NonNullArgs.empty()) {
374 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
375 return;
376 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000377 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000378
379 unsigned* start = &NonNullArgs[0];
380 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000381 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000382 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
383 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000384}
385
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000386static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
387 // This attribute must be applied to a function declaration.
388 // The first argument to the attribute must be a string,
389 // the name of the resource, for example "malloc".
390 // The following arguments must be argument indexes, the arguments must be
391 // of integer type for Returns, otherwise of pointer type.
392 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000393 // after being held. free() should be __attribute((ownership_takes)), whereas
394 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000395
396 if (!AL.getParameterName()) {
397 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
398 << AL.getName()->getName() << 1;
399 return;
400 }
401 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000402 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000403 switch (AL.getKind()) {
404 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000405 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000406 if (AL.getNumArgs() < 1) {
407 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
408 return;
409 }
Jordy Rose2a479922010-08-12 08:54:03 +0000410 break;
411 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000412 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000413 if (AL.getNumArgs() < 1) {
414 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
415 return;
416 }
Jordy Rose2a479922010-08-12 08:54:03 +0000417 break;
418 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000419 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000420 if (AL.getNumArgs() > 1) {
421 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
422 << AL.getNumArgs() + 1;
423 return;
424 }
Jordy Rose2a479922010-08-12 08:54:03 +0000425 break;
426 default:
427 // This should never happen given how we are called.
428 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000429 }
430
431 if (!isFunction(d) || !hasFunctionProto(d)) {
432 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
433 << 0 /*function*/;
434 return;
435 }
436
437 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
438
439 llvm::StringRef Module = AL.getParameterName()->getName();
440
441 // Normalize the argument, __foo__ becomes foo.
442 if (Module.startswith("__") && Module.endswith("__"))
443 Module = Module.substr(2, Module.size() - 4);
444
445 llvm::SmallVector<unsigned, 10> OwnershipArgs;
446
Jordy Rose2a479922010-08-12 08:54:03 +0000447 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
448 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000449
450 Expr *IdxExpr = static_cast<Expr *>(*I);
451 llvm::APSInt ArgNum(32);
452 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
453 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
454 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
455 << AL.getName()->getName() << IdxExpr->getSourceRange();
456 continue;
457 }
458
459 unsigned x = (unsigned) ArgNum.getZExtValue();
460
461 if (x > NumArgs || x < 1) {
462 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
463 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
464 continue;
465 }
466 --x;
467 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000468 case OwnershipAttr::Takes:
469 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000470 // Is the function argument a pointer type?
471 QualType T = getFunctionOrMethodArgType(d, x);
472 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
473 // FIXME: Should also highlight argument in decl.
474 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000475 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000476 << "pointer"
477 << IdxExpr->getSourceRange();
478 continue;
479 }
480 break;
481 }
Sean Huntcf807c42010-08-18 23:23:40 +0000482 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000483 if (AL.getNumArgs() > 1) {
484 // Is the function argument an integer type?
485 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
486 llvm::APSInt ArgNum(32);
487 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
488 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
489 S.Diag(AL.getLoc(), diag::err_ownership_type)
490 << "ownership_returns" << "integer"
491 << IdxExpr->getSourceRange();
492 return;
493 }
494 }
495 break;
496 }
Jordy Rose2a479922010-08-12 08:54:03 +0000497 default:
498 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000499 } // switch
500
501 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000502 for (specific_attr_iterator<OwnershipAttr>
503 i = d->specific_attr_begin<OwnershipAttr>(),
504 e = d->specific_attr_end<OwnershipAttr>();
505 i != e; ++i) {
506 if ((*i)->getOwnKind() != K) {
507 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
508 I!=E; ++I) {
509 if (x == *I) {
510 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
511 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000512 }
513 }
514 }
515 }
516 OwnershipArgs.push_back(x);
517 }
518
519 unsigned* start = OwnershipArgs.data();
520 unsigned size = OwnershipArgs.size();
521 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000522
523 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
524 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
525 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000526 }
Sean Huntcf807c42010-08-18 23:23:40 +0000527
528 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
529 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000530}
531
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000532static bool isStaticVarOrStaticFunciton(Decl *D) {
533 if (VarDecl *VD = dyn_cast<VarDecl>(D))
534 return VD->getStorageClass() == VarDecl::Static;
535 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
536 return FD->getStorageClass() == FunctionDecl::Static;
537 return false;
538}
539
540static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
541 // Check the attribute arguments.
542 if (Attr.getNumArgs() > 1) {
543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
544 return;
545 }
546
547 // gcc rejects
548 // class c {
549 // static int a __attribute__((weakref ("v2")));
550 // static int b() __attribute__((weakref ("f3")));
551 // };
552 // and ignores the attributes of
553 // void f(void) {
554 // static int a __attribute__((weakref ("v2")));
555 // }
556 // we reject them
557 if (const DeclContext *Ctx = d->getDeclContext()) {
558 Ctx = Ctx->getLookupContext();
559 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
560 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000561 dyn_cast<NamedDecl>(d)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000562 return;
563 }
564 }
565
566 // The GCC manual says
567 //
568 // At present, a declaration to which `weakref' is attached can only
569 // be `static'.
570 //
571 // It also says
572 //
573 // Without a TARGET,
574 // given as an argument to `weakref' or to `alias', `weakref' is
575 // equivalent to `weak'.
576 //
577 // gcc 4.4.1 will accept
578 // int a7 __attribute__((weakref));
579 // as
580 // int a7 __attribute__((weak));
581 // This looks like a bug in gcc. We reject that for now. We should revisit
582 // it if this behaviour is actually used.
583
584 if (!isStaticVarOrStaticFunciton(d)) {
585 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
586 dyn_cast<NamedDecl>(d)->getNameAsString();
587 return;
588 }
589
590 // GCC rejects
591 // static ((alias ("y"), weakref)).
592 // Should we? How to check that weakref is before or after alias?
593
594 if (Attr.getNumArgs() == 1) {
595 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
596 Arg = Arg->IgnoreParenCasts();
597 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
598
599 if (Str == 0 || Str->isWide()) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
601 << "weakref" << 1;
602 return;
603 }
604 // GCC will accept anything as the argument of weakref. Should we
605 // check for an existing decl?
Sean Huntcf807c42010-08-18 23:23:40 +0000606 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000607 }
608
Sean Huntcf807c42010-08-18 23:23:40 +0000609 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000610}
611
Chris Lattner803d0802008-06-29 00:43:07 +0000612static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000613 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000614 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000616 return;
617 }
Mike Stumpbf916502009-07-24 19:02:52 +0000618
Chris Lattner545dd342008-06-28 23:36:30 +0000619 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000620 Arg = Arg->IgnoreParenCasts();
621 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Chris Lattner6b6b5372008-06-26 18:38:35 +0000623 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000624 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000625 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000626 return;
627 }
Mike Stumpbf916502009-07-24 19:02:52 +0000628
Chris Lattner6b6b5372008-06-26 18:38:35 +0000629 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000630
Sean Huntcf807c42010-08-18 23:23:40 +0000631 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000632}
633
Mike Stumpbf916502009-07-24 19:02:52 +0000634static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000635 Sema &S) {
636 // check the attribute arguments.
637 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000639 return;
640 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000641
Chris Lattnerc5197432009-04-14 17:02:11 +0000642 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000643 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000644 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000645 return;
646 }
Mike Stumpbf916502009-07-24 19:02:52 +0000647
Sean Huntcf807c42010-08-18 23:23:40 +0000648 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000649}
650
Ryan Flynn76168e22009-08-09 20:07:29 +0000651static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
652 // check the attribute arguments.
653 if (Attr.getNumArgs() != 0) {
654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
655 return;
656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000658 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000659 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000660 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000661 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000662 return;
663 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000664 }
665
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000666 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000667}
668
Ted Kremenekb7252322009-04-10 00:01:14 +0000669static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnarae215f722010-04-30 13:10:51 +0000670 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000671 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000672 if (Attr.getNumArgs() != 0) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000674 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000675 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000676
Mike Stump19c30c02009-04-29 19:03:13 +0000677 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
678 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000679 if (VD == 0 || (!VD->getType()->isBlockPointerType()
680 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000681 S.Diag(Attr.getLoc(),
682 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
683 : diag::warn_attribute_wrong_decl_type)
684 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000685 return false;
686 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000687 }
Mike Stumpbf916502009-07-24 19:02:52 +0000688
Ted Kremenekb7252322009-04-10 00:01:14 +0000689 return true;
690}
691
692static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000693 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
694 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000695 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000696}
697
698static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
699 Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000700 if (HandleCommonNoReturnAttr(d, Attr, S))
Sean Huntcf807c42010-08-18 23:23:40 +0000701 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000702}
703
John Thompson35cc9622010-08-09 21:53:52 +0000704// PS3 PPU-specific.
705static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
706 Sema &S) {
707/*
708 Returning a Vector Class in Registers
709
710 According to the PPU ABI specifications, a class with a single member of vector type is returned in
711 memory when used as the return value of a function. This results in inefficient code when implementing
712 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
713 definition. This attribute is also applicable to struct types.
714
715 Example:
716
717 struct Vector
718 {
719 __vector float xyzw;
720 } __attribute__((vecreturn));
721
722 Vector Add(Vector lhs, Vector rhs)
723 {
724 Vector result;
725 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
726 return result; // This will be returned in a register
727 }
728*/
729 if (!isa<CXXRecordDecl>(d)) {
730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
731 << Attr.getName() << 9 /*class*/;
732 return;
733 }
734
735 if (d->getAttr<VecReturnAttr>()) {
736 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
737 return;
738 }
739
Sean Huntcf807c42010-08-18 23:23:40 +0000740 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000741}
742
Sean Huntbbd37c62009-11-21 08:43:09 +0000743static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
744 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
745 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000746 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000747 return;
748 }
749 // FIXME: Actually store the attribute on the declaration
750}
751
Ted Kremenek73798892008-07-25 04:39:19 +0000752static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
753 // check the attribute arguments.
754 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000755 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000756 return;
757 }
Mike Stumpbf916502009-07-24 19:02:52 +0000758
John McCallaec58602010-03-31 02:47:45 +0000759 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
760 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000761 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000762 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000763 return;
764 }
Mike Stumpbf916502009-07-24 19:02:52 +0000765
Sean Huntcf807c42010-08-18 23:23:40 +0000766 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000767}
768
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000769static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
770 // check the attribute arguments.
771 if (Attr.getNumArgs() != 0) {
772 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
773 return;
774 }
Mike Stumpbf916502009-07-24 19:02:52 +0000775
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000776 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000777 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000778 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
779 return;
780 }
781 } else if (!isFunctionOrMethod(d)) {
782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000783 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000784 return;
785 }
Mike Stumpbf916502009-07-24 19:02:52 +0000786
Sean Huntcf807c42010-08-18 23:23:40 +0000787 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000788}
789
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000790static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
791 // check the attribute arguments.
792 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000793 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
794 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000795 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000796 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000797
798 int priority = 65535; // FIXME: Do not hardcode such constants.
799 if (Attr.getNumArgs() > 0) {
800 Expr *E = static_cast<Expr *>(Attr.getArg(0));
801 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000802 if (E->isTypeDependent() || E->isValueDependent() ||
803 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000804 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000805 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000806 return;
807 }
808 priority = Idx.getZExtValue();
809 }
Mike Stumpbf916502009-07-24 19:02:52 +0000810
Chris Lattnerc5197432009-04-14 17:02:11 +0000811 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000812 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000813 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000814 return;
815 }
816
Sean Huntcf807c42010-08-18 23:23:40 +0000817 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000818}
819
820static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
821 // check the attribute arguments.
822 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000823 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
824 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000825 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000826 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000827
828 int priority = 65535; // FIXME: Do not hardcode such constants.
829 if (Attr.getNumArgs() > 0) {
830 Expr *E = static_cast<Expr *>(Attr.getArg(0));
831 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000832 if (E->isTypeDependent() || E->isValueDependent() ||
833 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000834 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000835 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000836 return;
837 }
838 priority = Idx.getZExtValue();
839 }
Mike Stumpbf916502009-07-24 19:02:52 +0000840
Anders Carlsson6782fc62008-08-22 22:10:48 +0000841 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000843 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000844 return;
845 }
846
Sean Huntcf807c42010-08-18 23:23:40 +0000847 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000848}
849
Chris Lattner803d0802008-06-29 00:43:07 +0000850static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000851 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000852 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000854 return;
855 }
Mike Stumpbf916502009-07-24 19:02:52 +0000856
Sean Huntcf807c42010-08-18 23:23:40 +0000857 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000858}
859
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000860static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
861 // check the attribute arguments.
862 if (Attr.getNumArgs() != 0) {
863 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
864 return;
865 }
Mike Stumpbf916502009-07-24 19:02:52 +0000866
Sean Huntcf807c42010-08-18 23:23:40 +0000867 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000868}
869
Chris Lattner803d0802008-06-29 00:43:07 +0000870static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000871 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000872 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000874 return;
875 }
Mike Stumpbf916502009-07-24 19:02:52 +0000876
Chris Lattner545dd342008-06-28 23:36:30 +0000877 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000878 Arg = Arg->IgnoreParenCasts();
879 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000880
Chris Lattner6b6b5372008-06-26 18:38:35 +0000881 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000882 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000883 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000884 return;
885 }
Mike Stumpbf916502009-07-24 19:02:52 +0000886
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000887 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +0000888 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +0000889
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000890 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +0000891 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000892 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +0000893 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000894 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +0000895 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000896 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +0000897 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000898 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000899 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000900 return;
901 }
Mike Stumpbf916502009-07-24 19:02:52 +0000902
Sean Huntcf807c42010-08-18 23:23:40 +0000903 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000904}
905
Chris Lattner0db29ec2009-02-14 08:09:34 +0000906static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
907 Sema &S) {
908 if (Attr.getNumArgs() != 0) {
909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
910 return;
911 }
Mike Stumpbf916502009-07-24 19:02:52 +0000912
Chris Lattner0db29ec2009-02-14 08:09:34 +0000913 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
914 if (OCI == 0) {
915 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
916 return;
917 }
Mike Stumpbf916502009-07-24 19:02:52 +0000918
Sean Huntcf807c42010-08-18 23:23:40 +0000919 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +0000920}
921
922static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000923 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000924 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000925 return;
926 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000927 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000928 QualType T = TD->getUnderlyingType();
929 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000930 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000931 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
932 return;
933 }
934 }
Sean Huntcf807c42010-08-18 23:23:40 +0000935 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000936}
937
Mike Stumpbf916502009-07-24 19:02:52 +0000938static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000939HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
940 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000942 return;
943 }
944
945 if (!isa<FunctionDecl>(D)) {
946 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
947 return;
948 }
949
Sean Huntcf807c42010-08-18 23:23:40 +0000950 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +0000951}
952
Steve Naroff9eae5762008-09-18 16:44:58 +0000953static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000954 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000955 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000956 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000957 return;
958 }
Mike Stumpbf916502009-07-24 19:02:52 +0000959
Steve Naroff9eae5762008-09-18 16:44:58 +0000960 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000961 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000962 return;
963 }
Mike Stumpbf916502009-07-24 19:02:52 +0000964
Sean Huntcf807c42010-08-18 23:23:40 +0000965 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000966 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000967 type = BlocksAttr::ByRef;
968 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000969 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000970 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000971 return;
972 }
Mike Stumpbf916502009-07-24 19:02:52 +0000973
Sean Huntcf807c42010-08-18 23:23:40 +0000974 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000975}
976
Anders Carlsson77091822008-10-05 18:05:59 +0000977static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
978 // check the attribute arguments.
979 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000980 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
981 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000982 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000983 }
984
Anders Carlsson77091822008-10-05 18:05:59 +0000985 int sentinel = 0;
986 if (Attr.getNumArgs() > 0) {
987 Expr *E = static_cast<Expr *>(Attr.getArg(0));
988 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000989 if (E->isTypeDependent() || E->isValueDependent() ||
990 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000991 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000992 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000993 return;
994 }
995 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000996
Anders Carlsson77091822008-10-05 18:05:59 +0000997 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000998 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
999 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001000 return;
1001 }
1002 }
1003
1004 int nullPos = 0;
1005 if (Attr.getNumArgs() > 1) {
1006 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1007 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001008 if (E->isTypeDependent() || E->isValueDependent() ||
1009 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001011 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001012 return;
1013 }
1014 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001015
Anders Carlsson77091822008-10-05 18:05:59 +00001016 if (nullPos > 1 || nullPos < 0) {
1017 // FIXME: This error message could be improved, it would be nice
1018 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001019 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1020 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001021 return;
1022 }
1023 }
1024
1025 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001026 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001027 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001028
Chris Lattner897cd902009-03-17 23:03:47 +00001029 if (isa<FunctionNoProtoType>(FT)) {
1030 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1031 return;
1032 }
Mike Stumpbf916502009-07-24 19:02:52 +00001033
Chris Lattner897cd902009-03-17 23:03:47 +00001034 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001035 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001036 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001037 }
Anders Carlsson77091822008-10-05 18:05:59 +00001038 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1039 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001040 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001041 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001042 }
1043 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001044 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1045 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001046 ;
1047 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001048 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001049 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001050 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001051 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001052 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001053 int m = Ty->isFunctionPointerType() ? 0 : 1;
1054 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001055 return;
1056 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001057 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001058 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001059 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001060 return;
1061 }
Anders Carlsson77091822008-10-05 18:05:59 +00001062 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001063 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001064 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001065 return;
1066 }
Sean Huntcf807c42010-08-18 23:23:40 +00001067 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001068}
1069
Chris Lattner026dc962009-02-14 07:37:35 +00001070static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1071 // check the attribute arguments.
1072 if (Attr.getNumArgs() != 0) {
1073 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1074 return;
1075 }
1076
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001077 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001078 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001079 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001080 return;
1081 }
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001083 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1084 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1085 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001086 return;
1087 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001088 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1089 if (MD->getResultType()->isVoidType()) {
1090 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1091 << Attr.getName() << 1;
1092 return;
1093 }
1094
Sean Huntcf807c42010-08-18 23:23:40 +00001095 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001096}
1097
1098static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001099 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001100 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001102 return;
1103 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001104
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001105 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001106 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001107 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1108 dyn_cast<NamedDecl>(D)->getNameAsString();
1109 return;
1110 }
1111
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001112 // TODO: could also be applied to methods?
1113 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1114 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001115 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001116 return;
1117 }
Mike Stumpbf916502009-07-24 19:02:52 +00001118
Sean Huntcf807c42010-08-18 23:23:40 +00001119 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001120}
1121
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001122static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1123 // check the attribute arguments.
1124 if (Attr.getNumArgs() != 0) {
1125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1126 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001127 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001128
1129 // weak_import only applies to variable & function declarations.
1130 bool isDef = false;
1131 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1132 isDef = (!VD->hasExternalStorage() || VD->getInit());
1133 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001134 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001135 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1136 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001137 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001138 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001139 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001140 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001141 !isa<ObjCInterfaceDecl>(D))
1142 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001143 << Attr.getName() << 2 /*variable and function*/;
1144 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001145 }
1146
1147 // Merge should handle any subsequent violations.
1148 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001149 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001150 diag::warn_attribute_weak_import_invalid_on_definition)
1151 << "weak_import" << 2 /*variable and function*/;
1152 return;
1153 }
1154
Sean Huntcf807c42010-08-18 23:23:40 +00001155 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001156}
1157
Nate Begeman6f3d8382009-06-26 06:32:41 +00001158static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1159 Sema &S) {
1160 // Attribute has 3 arguments.
1161 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001162 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001163 return;
1164 }
1165
1166 unsigned WGSize[3];
1167 for (unsigned i = 0; i < 3; ++i) {
1168 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1169 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001170 if (E->isTypeDependent() || E->isValueDependent() ||
1171 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001172 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1173 << "reqd_work_group_size" << E->getSourceRange();
1174 return;
1175 }
1176 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1177 }
Sean Huntcf807c42010-08-18 23:23:40 +00001178 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1179 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001180 WGSize[2]));
1181}
1182
Chris Lattner026dc962009-02-14 07:37:35 +00001183static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001184 // Attribute has no arguments.
1185 if (Attr.getNumArgs() != 1) {
1186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1187 return;
1188 }
1189
1190 // Make sure that there is a string literal as the sections's single
1191 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001192 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1193 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001194 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001195 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001196 return;
1197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Chris Lattner797c3c42009-08-10 19:03:04 +00001199 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001200 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001201 if (!Error.empty()) {
1202 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1203 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001204 return;
1205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001207 // This attribute cannot be applied to local variables.
1208 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1209 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1210 return;
1211 }
1212
Sean Huntcf807c42010-08-18 23:23:40 +00001213 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001214}
1215
Chris Lattner6b6b5372008-06-26 18:38:35 +00001216
Chris Lattner803d0802008-06-29 00:43:07 +00001217static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001218 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001219 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001220 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001221 return;
1222 }
Mike Stumpbf916502009-07-24 19:02:52 +00001223
Sean Huntcf807c42010-08-18 23:23:40 +00001224 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001225}
1226
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001227static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1228 // check the attribute arguments.
1229 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001230 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001231 return;
1232 }
Mike Stumpbf916502009-07-24 19:02:52 +00001233
Sean Huntcf807c42010-08-18 23:23:40 +00001234 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001235}
1236
1237static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1238 // check the attribute arguments.
1239 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001240 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001241 return;
1242 }
Mike Stumpbf916502009-07-24 19:02:52 +00001243
Sean Huntcf807c42010-08-18 23:23:40 +00001244 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001245}
1246
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001247static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001248 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001249 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1250 return;
1251 }
Mike Stumpbf916502009-07-24 19:02:52 +00001252
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001253 if (Attr.getNumArgs() != 0) {
1254 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1255 return;
1256 }
Mike Stumpbf916502009-07-24 19:02:52 +00001257
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001258 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001259
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001260 if (!VD || !VD->hasLocalStorage()) {
1261 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1262 return;
1263 }
Mike Stumpbf916502009-07-24 19:02:52 +00001264
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001265 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001266 // FIXME: Lookup probably isn't looking in the right place
1267 // FIXME: The lookup source location should be in the attribute, not the
1268 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001269 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001270 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001271 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001272 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001274 Attr.getParameterName();
1275 return;
1276 }
Mike Stumpbf916502009-07-24 19:02:52 +00001277
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001278 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1279 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001280 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001281 Attr.getParameterName();
1282 return;
1283 }
1284
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001285 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001287 Attr.getParameterName();
1288 return;
1289 }
Mike Stumpbf916502009-07-24 19:02:52 +00001290
Anders Carlsson89941c12009-02-07 23:16:50 +00001291 // We're currently more strict than GCC about what function types we accept.
1292 // If this ever proves to be a problem it should be easy to fix.
1293 QualType Ty = S.Context.getPointerType(VD->getType());
1294 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001295 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001296 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001297 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1298 Attr.getParameterName() << ParamTy << Ty;
1299 return;
1300 }
Mike Stumpbf916502009-07-24 19:02:52 +00001301
Sean Huntcf807c42010-08-18 23:23:40 +00001302 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001303}
1304
Mike Stumpbf916502009-07-24 19:02:52 +00001305/// Handle __attribute__((format_arg((idx)))) attribute based on
1306/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1307static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001308 if (Attr.getNumArgs() != 1) {
1309 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1310 return;
1311 }
1312 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1313 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1314 << Attr.getName() << 0 /*function*/;
1315 return;
1316 }
Mike Stumpbf916502009-07-24 19:02:52 +00001317 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1318 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001319 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1320 unsigned FirstIdx = 1;
1321 // checks for the 2nd argument
1322 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1323 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001324 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1325 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1327 << "format" << 2 << IdxExpr->getSourceRange();
1328 return;
1329 }
Mike Stumpbf916502009-07-24 19:02:52 +00001330
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001331 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1332 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1333 << "format" << 2 << IdxExpr->getSourceRange();
1334 return;
1335 }
Mike Stumpbf916502009-07-24 19:02:52 +00001336
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001337 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001338
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001339 // make sure the format string is really a string
1340 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001341
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001342 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1343 if (not_nsstring_type &&
1344 !isCFStringType(Ty, S.Context) &&
1345 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001346 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001347 // FIXME: Should highlight the actual expression that has the wrong type.
1348 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001349 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001350 << IdxExpr->getSourceRange();
1351 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001352 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001353 Ty = getFunctionOrMethodResultType(d);
1354 if (!isNSStringType(Ty, S.Context) &&
1355 !isCFStringType(Ty, S.Context) &&
1356 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001357 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001358 // FIXME: Should highlight the actual expression that has the wrong type.
1359 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001360 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001361 << IdxExpr->getSourceRange();
1362 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001363 }
1364
Sean Huntcf807c42010-08-18 23:23:40 +00001365 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001366}
1367
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001368enum FormatAttrKind {
1369 CFStringFormat,
1370 NSStringFormat,
1371 StrftimeFormat,
1372 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001373 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001374 InvalidFormat
1375};
1376
1377/// getFormatAttrKind - Map from format attribute names to supported format
1378/// types.
1379static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1380 // Check for formats that get handled specially.
1381 if (Format == "NSString")
1382 return NSStringFormat;
1383 if (Format == "CFString")
1384 return CFStringFormat;
1385 if (Format == "strftime")
1386 return StrftimeFormat;
1387
1388 // Otherwise, check for supported formats.
1389 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1390 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1391 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1392 Format == "zcmn_err")
1393 return SupportedFormat;
1394
Duncan Sandsbc525952010-03-23 14:44:19 +00001395 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1396 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001397 return IgnoredFormat;
1398
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001399 return InvalidFormat;
1400}
1401
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001402/// Handle __attribute__((init_priority(priority))) attributes based on
1403/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1404static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1405 Sema &S) {
1406 if (!S.getLangOptions().CPlusPlus) {
1407 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1408 return;
1409 }
1410
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001411 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1412 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1413 Attr.setInvalid();
1414 return;
1415 }
1416 QualType T = dyn_cast<VarDecl>(d)->getType();
1417 if (S.Context.getAsArrayType(T))
1418 T = S.Context.getBaseElementType(T);
1419 if (!T->getAs<RecordType>()) {
1420 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1421 Attr.setInvalid();
1422 return;
1423 }
1424
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001425 if (Attr.getNumArgs() != 1) {
1426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1427 Attr.setInvalid();
1428 return;
1429 }
1430 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001431
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001432 llvm::APSInt priority(32);
1433 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1434 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1435 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1436 << "init_priority" << priorityExpr->getSourceRange();
1437 Attr.setInvalid();
1438 return;
1439 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001440 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001441 if (prioritynum < 101 || prioritynum > 65535) {
1442 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1443 << priorityExpr->getSourceRange();
1444 Attr.setInvalid();
1445 return;
1446 }
Sean Huntcf807c42010-08-18 23:23:40 +00001447 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001448}
1449
Mike Stumpbf916502009-07-24 19:02:52 +00001450/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1451/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001452static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453
Chris Lattner545dd342008-06-28 23:36:30 +00001454 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001455 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001456 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001457 return;
1458 }
1459
Chris Lattner545dd342008-06-28 23:36:30 +00001460 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001462 return;
1463 }
1464
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001465 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001467 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001468 return;
1469 }
1470
Daniel Dunbar35682492008-09-26 04:12:28 +00001471 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001472 unsigned FirstIdx = 1;
1473
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001474 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001475
1476 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001477 if (Format.startswith("__") && Format.endswith("__"))
1478 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001479
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001480 // Check for supported formats.
1481 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001482
1483 if (Kind == IgnoredFormat)
1484 return;
1485
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001486 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001487 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001488 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001489 return;
1490 }
1491
1492 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001493 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001494 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001495 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1496 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001497 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001498 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001499 return;
1500 }
1501
Anders Carlsson4fb77202009-08-25 14:12:34 +00001502 // FIXME: We should handle the implicit 'this' parameter in a more generic
1503 // way that can be used for other arguments.
1504 bool HasImplicitThisParam = false;
1505 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1506 if (MD->isInstance()) {
1507 HasImplicitThisParam = true;
1508 NumArgs++;
1509 }
1510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Chris Lattner6b6b5372008-06-26 18:38:35 +00001512 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001514 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001515 return;
1516 }
1517
1518 // FIXME: Do we need to bounds check?
1519 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001520
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001521 if (HasImplicitThisParam) {
1522 if (ArgIdx == 0) {
1523 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1524 << "a string type" << IdxExpr->getSourceRange();
1525 return;
1526 }
1527 ArgIdx--;
1528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Chris Lattner6b6b5372008-06-26 18:38:35 +00001530 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001531 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001532
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001533 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001534 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001535 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1536 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001537 return;
1538 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001539 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001540 // FIXME: do we need to check if the type is NSString*? What are the
1541 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001542 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001543 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001544 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1545 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001546 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001547 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001548 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001549 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001550 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001551 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1552 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001553 return;
1554 }
1555
1556 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001557 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001558 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001559 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1560 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001561 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001562 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001563 return;
1564 }
1565
1566 // check if the function is variadic if the 3rd argument non-zero
1567 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001568 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001569 ++NumArgs; // +1 for ...
1570 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001571 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001572 return;
1573 }
1574 }
1575
Chris Lattner3c73c412008-11-19 08:23:25 +00001576 // strftime requires FirstArg to be 0 because it doesn't read from any
1577 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001578 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001579 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001580 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1581 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001582 return;
1583 }
1584 // if 0 it disables parameter checking (to use with e.g. va_list)
1585 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001587 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001588 return;
1589 }
1590
Sean Huntcf807c42010-08-18 23:23:40 +00001591 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1592 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001593 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001594}
1595
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001596static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1597 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001598 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001599 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001600 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001601 return;
1602 }
1603
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001604 // Try to find the underlying union declaration.
1605 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001606 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001607 if (TD && TD->getUnderlyingType()->isUnionType())
1608 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1609 else
1610 RD = dyn_cast<RecordDecl>(d);
1611
1612 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001614 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001615 return;
1616 }
1617
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001618 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001619 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001620 diag::warn_transparent_union_attribute_not_definition);
1621 return;
1622 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001623
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001624 RecordDecl::field_iterator Field = RD->field_begin(),
1625 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001626 if (Field == FieldEnd) {
1627 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1628 return;
1629 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001630
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001631 FieldDecl *FirstField = *Field;
1632 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001633 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001634 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001635 diag::warn_transparent_union_attribute_floating)
1636 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001637 return;
1638 }
1639
1640 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1641 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1642 for (; Field != FieldEnd; ++Field) {
1643 QualType FieldType = Field->getType();
1644 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1645 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1646 // Warn if we drop the attribute.
1647 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001648 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001649 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001650 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001651 diag::warn_transparent_union_attribute_field_size_align)
1652 << isSize << Field->getDeclName() << FieldBits;
1653 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001654 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001655 diag::note_transparent_union_first_field_size_align)
1656 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001657 return;
1658 }
1659 }
1660
Sean Huntcf807c42010-08-18 23:23:40 +00001661 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001662}
1663
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001664static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001665 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001666 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001668 return;
1669 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001670 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1671 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001672
Chris Lattner6b6b5372008-06-26 18:38:35 +00001673 // Make sure that there is a string literal as the annotation's single
1674 // argument.
1675 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001676 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001677 return;
1678 }
Sean Huntcf807c42010-08-18 23:23:40 +00001679 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001680}
1681
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001682static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001683 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001684 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001686 return;
1687 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001688
1689 //FIXME: The C++0x version of this attribute has more limited applicabilty
1690 // than GNU's, and should error out when it is used to specify a
1691 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001692
Chris Lattner545dd342008-06-28 23:36:30 +00001693 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001694 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001695 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001696 }
Mike Stumpbf916502009-07-24 19:02:52 +00001697
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001698 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1699}
1700
1701void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1702 if (E->isTypeDependent() || E->isValueDependent()) {
1703 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001704 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001705 return;
1706 }
1707
Sean Huntcf807c42010-08-18 23:23:40 +00001708 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001709 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001710 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1711 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1712 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001713 return;
1714 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001715 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001716 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1717 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001718 return;
1719 }
1720
Sean Huntcf807c42010-08-18 23:23:40 +00001721 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1722}
1723
1724void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1725 // FIXME: Cache the number on the Attr object if non-dependent?
1726 // FIXME: Perform checking of type validity
1727 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1728 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001729}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001730
Mike Stumpbf916502009-07-24 19:02:52 +00001731/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1732/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001733///
Mike Stumpbf916502009-07-24 19:02:52 +00001734/// Despite what would be logical, the mode attribute is a decl attribute, not a
1735/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1736/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001737static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001738 // This attribute isn't documented, but glibc uses it. It changes
1739 // the width of an int or unsigned int to the specified size.
1740
1741 // Check that there aren't any arguments
1742 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001743 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001744 return;
1745 }
1746
1747 IdentifierInfo *Name = Attr.getParameterName();
1748 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001749 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001750 return;
1751 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001752
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001753 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001754
1755 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001756 if (Str.startswith("__") && Str.endswith("__"))
1757 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001758
1759 unsigned DestWidth = 0;
1760 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001761 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001762 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001763 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001764 switch (Str[0]) {
1765 case 'Q': DestWidth = 8; break;
1766 case 'H': DestWidth = 16; break;
1767 case 'S': DestWidth = 32; break;
1768 case 'D': DestWidth = 64; break;
1769 case 'X': DestWidth = 96; break;
1770 case 'T': DestWidth = 128; break;
1771 }
1772 if (Str[1] == 'F') {
1773 IntegerMode = false;
1774 } else if (Str[1] == 'C') {
1775 IntegerMode = false;
1776 ComplexMode = true;
1777 } else if (Str[1] != 'I') {
1778 DestWidth = 0;
1779 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001780 break;
1781 case 4:
1782 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1783 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001784 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001785 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001786 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001787 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001788 break;
1789 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001790 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001791 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001792 break;
1793 }
1794
1795 QualType OldTy;
1796 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1797 OldTy = TD->getUnderlyingType();
1798 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1799 OldTy = VD->getType();
1800 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001801 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1802 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001803 return;
1804 }
Eli Friedman73397492009-03-03 06:41:03 +00001805
John McCall183700f2009-09-21 23:43:11 +00001806 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001807 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1808 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001809 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001810 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1811 } else if (ComplexMode) {
1812 if (!OldTy->isComplexType())
1813 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1814 } else {
1815 if (!OldTy->isFloatingType())
1816 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1817 }
1818
Mike Stump390b4cc2009-05-16 07:39:55 +00001819 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1820 // and friends, at least with glibc.
1821 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1822 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001823 // FIXME: Make sure floating-point mappings are accurate
1824 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001825 QualType NewTy;
1826 switch (DestWidth) {
1827 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001828 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001829 return;
1830 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001831 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001832 return;
1833 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001834 if (!IntegerMode) {
1835 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1836 return;
1837 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001838 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001839 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001840 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001841 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001842 break;
1843 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001844 if (!IntegerMode) {
1845 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1846 return;
1847 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001848 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001849 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001850 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001851 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001852 break;
1853 case 32:
1854 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001855 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001856 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001857 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001858 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001859 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001860 break;
1861 case 64:
1862 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001863 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001864 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001865 if (S.Context.Target.getLongWidth() == 64)
1866 NewTy = S.Context.LongTy;
1867 else
1868 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001869 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001870 if (S.Context.Target.getLongWidth() == 64)
1871 NewTy = S.Context.UnsignedLongTy;
1872 else
1873 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001874 break;
Eli Friedman73397492009-03-03 06:41:03 +00001875 case 96:
1876 NewTy = S.Context.LongDoubleTy;
1877 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001878 case 128:
1879 if (!IntegerMode) {
1880 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1881 return;
1882 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001883 if (OldTy->isSignedIntegerType())
1884 NewTy = S.Context.Int128Ty;
1885 else
1886 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001887 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001888 }
1889
Eli Friedman73397492009-03-03 06:41:03 +00001890 if (ComplexMode) {
1891 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001892 }
1893
1894 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001895 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1896 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001897 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001898 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001899 cast<ValueDecl>(D)->setType(NewTy);
1900}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001901
Mike Stump1feade82009-08-26 22:31:08 +00001902static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001903 // check the attribute arguments.
1904 if (Attr.getNumArgs() > 0) {
1905 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1906 return;
1907 }
Anders Carlssone896d982009-02-13 08:11:52 +00001908
Anders Carlsson5bab7882009-02-19 19:16:48 +00001909 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001910 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001911 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001912 return;
1913 }
Mike Stumpbf916502009-07-24 19:02:52 +00001914
Sean Huntcf807c42010-08-18 23:23:40 +00001915 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00001916}
1917
Mike Stump1feade82009-08-26 22:31:08 +00001918static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001919 // check the attribute arguments.
1920 if (Attr.getNumArgs() != 0) {
1921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1922 return;
1923 }
Mike Stumpbf916502009-07-24 19:02:52 +00001924
Chris Lattnerc5197432009-04-14 17:02:11 +00001925 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001926 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001927 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001928 return;
1929 }
Mike Stumpbf916502009-07-24 19:02:52 +00001930
Sean Huntcf807c42010-08-18 23:23:40 +00001931 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00001932}
1933
Chris Lattner7255a2d2010-06-22 00:03:40 +00001934static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1935 Sema &S) {
1936 // check the attribute arguments.
1937 if (Attr.getNumArgs() != 0) {
1938 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1939 return;
1940 }
1941
1942 if (!isa<FunctionDecl>(d)) {
1943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1944 << Attr.getName() << 0 /*function*/;
1945 return;
1946 }
1947
Sean Huntcf807c42010-08-18 23:23:40 +00001948 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00001949}
1950
Chris Lattnercf2a7212009-04-20 19:12:28 +00001951static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001952 // check the attribute arguments.
1953 if (Attr.getNumArgs() != 0) {
1954 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1955 return;
1956 }
Mike Stumpbf916502009-07-24 19:02:52 +00001957
Chris Lattnerc5197432009-04-14 17:02:11 +00001958 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1959 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001960 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001961 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001962 return;
1963 }
Mike Stumpbf916502009-07-24 19:02:52 +00001964
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001965 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001966 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001967 return;
1968 }
Mike Stumpbf916502009-07-24 19:02:52 +00001969
Sean Huntcf807c42010-08-18 23:23:40 +00001970 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00001971}
1972
Abramo Bagnarae215f722010-04-30 13:10:51 +00001973static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1974 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1975 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1976 assert(Attr.isInvalid() == false);
1977
1978 switch (Attr.getKind()) {
1979 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00001980 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001981 return;
1982 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00001983 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001984 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001985 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00001986 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001987 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00001988 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001989 return;
1990 default:
1991 llvm_unreachable("unexpected attribute kind");
1992 return;
1993 }
1994}
1995
Fariborz Jahanianee760332009-03-27 18:38:55 +00001996static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1997 // check the attribute arguments.
1998 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001999 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002000 return;
2001 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002002
Fariborz Jahanianee760332009-03-27 18:38:55 +00002003 if (!isFunctionOrMethod(d)) {
2004 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002005 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002006 return;
2007 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002008
2009 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2010 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002011 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2012 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002013 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2014 << "regparm" << NumParamsExpr->getSourceRange();
2015 return;
2016 }
2017
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002018 if (S.Context.Target.getRegParmMax() == 0) {
2019 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002020 << NumParamsExpr->getSourceRange();
2021 return;
2022 }
2023
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002024 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002025 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2026 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002027 return;
2028 }
2029
Sean Huntcf807c42010-08-18 23:23:40 +00002030 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2031 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002032}
2033
Sean Huntbbd37c62009-11-21 08:43:09 +00002034static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2035 // check the attribute arguments.
2036 if (Attr.getNumArgs() != 0) {
2037 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2038 return;
2039 }
2040
2041 if (!isa<CXXRecordDecl>(d)
2042 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2043 S.Diag(Attr.getLoc(),
2044 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2045 : diag::warn_attribute_wrong_decl_type)
2046 << Attr.getName() << 7 /*virtual method or class*/;
2047 return;
2048 }
Sean Hunt7725e672009-11-25 04:20:27 +00002049
2050 // FIXME: Conform to C++0x redeclaration rules.
2051
2052 if (d->getAttr<FinalAttr>()) {
2053 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2054 return;
2055 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002056
Sean Huntcf807c42010-08-18 23:23:40 +00002057 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002058}
2059
Chris Lattner0744e5f2008-06-29 00:23:49 +00002060//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002061// C++0x member checking attributes
2062//===----------------------------------------------------------------------===//
2063
2064static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2065 if (Attr.getNumArgs() != 0) {
2066 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2067 return;
2068 }
2069
2070 if (!isa<CXXRecordDecl>(d)) {
2071 S.Diag(Attr.getLoc(),
2072 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2073 : diag::warn_attribute_wrong_decl_type)
2074 << Attr.getName() << 9 /*class*/;
2075 return;
2076 }
2077
2078 if (d->getAttr<BaseCheckAttr>()) {
2079 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2080 return;
2081 }
2082
Sean Huntcf807c42010-08-18 23:23:40 +00002083 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002084}
2085
2086static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2087 if (Attr.getNumArgs() != 0) {
2088 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2089 return;
2090 }
2091
2092 if (!isa<RecordDecl>(d->getDeclContext())) {
2093 // FIXME: It's not the type that's the problem
2094 S.Diag(Attr.getLoc(),
2095 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2096 : diag::warn_attribute_wrong_decl_type)
2097 << Attr.getName() << 11 /*member*/;
2098 return;
2099 }
2100
2101 // FIXME: Conform to C++0x redeclaration rules.
2102
2103 if (d->getAttr<HidingAttr>()) {
2104 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2105 return;
2106 }
2107
Sean Huntcf807c42010-08-18 23:23:40 +00002108 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002109}
2110
2111static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2112 if (Attr.getNumArgs() != 0) {
2113 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2114 return;
2115 }
2116
2117 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2118 // FIXME: It's not the type that's the problem
2119 S.Diag(Attr.getLoc(),
2120 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2121 : diag::warn_attribute_wrong_decl_type)
2122 << Attr.getName() << 10 /*virtual method*/;
2123 return;
2124 }
2125
2126 // FIXME: Conform to C++0x redeclaration rules.
2127
2128 if (d->getAttr<OverrideAttr>()) {
2129 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2130 return;
2131 }
2132
Sean Huntcf807c42010-08-18 23:23:40 +00002133 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002134}
2135
2136//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002137// Checker-specific attribute handlers.
2138//===----------------------------------------------------------------------===//
2139
2140static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2141 Sema &S) {
2142
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002143 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002144
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002145 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2146 RetTy = MD->getResultType();
2147 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2148 RetTy = FD->getResultType();
2149 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002150 SourceLocation L = Attr.getLoc();
2151 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2152 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002153 return;
2154 }
Mike Stumpbf916502009-07-24 19:02:52 +00002155
Ted Kremenek6217b802009-07-29 21:53:49 +00002156 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002157 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002158 SourceLocation L = Attr.getLoc();
2159 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2160 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002161 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002162 }
Mike Stumpbf916502009-07-24 19:02:52 +00002163
Ted Kremenekb71368d2009-05-09 02:44:38 +00002164 switch (Attr.getKind()) {
2165 default:
2166 assert(0 && "invalid ownership attribute");
2167 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002168 case AttributeList::AT_cf_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002169 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002170 return;
2171 case AttributeList::AT_ns_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002172 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002173 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002174 case AttributeList::AT_cf_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002175 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002176 return;
2177 case AttributeList::AT_ns_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002178 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002179 return;
2180 };
2181}
2182
Charles Davisf0122fe2010-02-16 18:27:26 +00002183static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2184 return Attr.getKind() == AttributeList::AT_dllimport ||
2185 Attr.getKind() == AttributeList::AT_dllexport;
2186}
2187
Ted Kremenekb71368d2009-05-09 02:44:38 +00002188//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002189// Top Level Sema Entry Points
2190//===----------------------------------------------------------------------===//
2191
Sebastian Redla89d82c2008-12-21 19:24:58 +00002192/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002193/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002194/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2195/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002196static void ProcessDeclAttribute(Scope *scope, Decl *D,
2197 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002198 if (Attr.isInvalid())
2199 return;
2200
Charles Davisf0122fe2010-02-16 18:27:26 +00002201 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2202 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002203 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002204 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002205 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002206 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2207 case AttributeList::AT_IBOutletCollection:
2208 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002209 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002210 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002211 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002212 // Ignore these, these are type attributes, handled by
2213 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002214 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002215 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2216 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002217 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002218 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002219 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002220 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002221 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2222 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002223 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002224 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002225 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2226 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2227 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002228 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002229 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002230 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002231 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2232 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2233 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2234 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2235 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2236 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2237 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2238 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002239 case AttributeList::AT_ownership_returns:
2240 case AttributeList::AT_ownership_takes:
2241 case AttributeList::AT_ownership_holds:
2242 HandleOwnershipAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002243 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2244 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2245 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002246 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002247
2248 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002249 case AttributeList::AT_ns_returns_not_retained:
2250 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002251 case AttributeList::AT_ns_returns_retained:
2252 case AttributeList::AT_cf_returns_retained:
2253 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2254
Nate Begeman6f3d8382009-06-26 06:32:41 +00002255 case AttributeList::AT_reqd_wg_size:
2256 HandleReqdWorkGroupSize(D, Attr, S); break;
2257
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002258 case AttributeList::AT_init_priority:
2259 HandleInitPriorityAttr(D, Attr, S); break;
2260
Sean Hunt7725e672009-11-25 04:20:27 +00002261 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2262 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002263 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2264 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2265 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002266 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002267 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2268 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002269 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002270 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002271 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002272 case AttributeList::AT_transparent_union:
2273 HandleTransparentUnionAttr(D, Attr, S);
2274 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002275 case AttributeList::AT_objc_exception:
2276 HandleObjCExceptionAttr(D, Attr, S);
2277 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002278 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002279 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2280 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2281 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2282 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2283 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2284 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2285 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2286 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2287 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002288 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002289 // Just ignore
2290 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002291 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2292 HandleNoInstrumentFunctionAttr(D, Attr, S);
2293 break;
John McCall04a67a62010-02-05 21:31:56 +00002294 case AttributeList::AT_stdcall:
2295 case AttributeList::AT_cdecl:
2296 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002297 case AttributeList::AT_thiscall:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002298 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002299 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002300 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002301 // Ask target about the attribute.
2302 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2303 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002304 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2305 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002306 break;
2307 }
2308}
2309
2310/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2311/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002312void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002313 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2314 ProcessDeclAttribute(S, D, *l, *this);
2315 }
2316
2317 // GCC accepts
2318 // static int a9 __attribute__((weakref));
2319 // but that looks really pointless. We reject it.
2320 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2321 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002322 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002323 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002324 }
2325}
2326
Ryan Flynne25ff832009-07-30 03:15:39 +00002327/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2328/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002329NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002330 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002331 NamedDecl *NewD = 0;
2332 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2333 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2334 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002335 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002336 if (FD->getQualifier()) {
2337 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2338 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2339 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002340 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2341 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2342 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002343 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002344 VD->getStorageClass(),
2345 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002346 if (VD->getQualifier()) {
2347 VarDecl *NewVD = cast<VarDecl>(NewD);
2348 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2349 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002350 }
2351 return NewD;
2352}
2353
2354/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2355/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002356void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002357 if (W.getUsed()) return; // only do this once
2358 W.setUsed(true);
2359 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2360 IdentifierInfo *NDId = ND->getIdentifier();
2361 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002362 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2363 NDId->getName()));
2364 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002365 WeakTopLevelDecl.push_back(NewD);
2366 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2367 // to insert Decl at TU scope, sorry.
2368 DeclContext *SavedContext = CurContext;
2369 CurContext = Context.getTranslationUnitDecl();
2370 PushOnScopeChains(NewD, S);
2371 CurContext = SavedContext;
2372 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002373 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002374 }
2375}
2376
Chris Lattner0744e5f2008-06-29 00:23:49 +00002377/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2378/// it, apply them to D. This is a bit tricky because PD can have attributes
2379/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002380void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002381 // Handle #pragma weak
2382 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2383 if (ND->hasLinkage()) {
2384 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2385 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002386 // Identifier referenced by #pragma weak before it was declared
2387 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002388 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2389 }
2390 }
2391 }
2392
Chris Lattner0744e5f2008-06-29 00:23:49 +00002393 // Apply decl attributes from the DeclSpec if present.
2394 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002395 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002396
Chris Lattner0744e5f2008-06-29 00:23:49 +00002397 // Walk the declarator structure, applying decl attributes that were in a type
2398 // position to the decl itself. This handles cases like:
2399 // int *__attr__(x)** D;
2400 // when X is a decl attribute.
2401 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2402 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002403 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002404
Chris Lattner0744e5f2008-06-29 00:23:49 +00002405 // Finally, apply any attributes on the decl itself.
2406 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002407 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002408}
John McCall54abf7d2009-11-04 02:18:39 +00002409
2410/// PushParsingDeclaration - Enter a new "scope" of deprecation
2411/// warnings.
2412///
2413/// The state token we use is the start index of this scope
2414/// on the warning stack.
2415Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2416 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002417 return (ParsingDeclStackState) DelayedDiagnostics.size();
2418}
2419
2420void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2421 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2422 ParsingDeclDepth--;
2423
2424 if (DelayedDiagnostics.empty())
2425 return;
2426
2427 unsigned SavedIndex = (unsigned) S;
2428 assert(SavedIndex <= DelayedDiagnostics.size() &&
2429 "saved index is out of bounds");
2430
John McCall58e6f342010-03-16 05:22:47 +00002431 unsigned E = DelayedDiagnostics.size();
2432
John McCall2f514482010-01-27 03:50:35 +00002433 // We only want to actually emit delayed diagnostics when we
2434 // successfully parsed a decl.
2435 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2436 if (D) {
2437 // We really do want to start with 0 here. We get one push for a
2438 // decl spec and another for each declarator; in a decl group like:
2439 // deprecated_typedef foo, *bar, baz();
2440 // only the declarator pops will be passed decls. This is correct;
2441 // we really do need to consider delayed diagnostics from the decl spec
2442 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002443 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002444 if (DelayedDiagnostics[I].Triggered)
2445 continue;
2446
2447 switch (DelayedDiagnostics[I].Kind) {
2448 case DelayedDiagnostic::Deprecation:
2449 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2450 break;
2451
2452 case DelayedDiagnostic::Access:
2453 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2454 break;
2455 }
2456 }
2457 }
2458
John McCall58e6f342010-03-16 05:22:47 +00002459 // Destroy all the delayed diagnostics we're about to pop off.
2460 for (unsigned I = SavedIndex; I != E; ++I)
2461 DelayedDiagnostics[I].destroy();
2462
John McCall2f514482010-01-27 03:50:35 +00002463 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002464}
2465
2466static bool isDeclDeprecated(Decl *D) {
2467 do {
2468 if (D->hasAttr<DeprecatedAttr>())
2469 return true;
2470 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2471 return false;
2472}
2473
John McCall2f514482010-01-27 03:50:35 +00002474void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2475 Decl *Ctx) {
2476 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002477 return;
2478
John McCall2f514482010-01-27 03:50:35 +00002479 DD.Triggered = true;
2480 Diag(DD.Loc, diag::warn_deprecated)
2481 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002482}
2483
2484void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2485 // Delay if we're currently parsing a declaration.
2486 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002487 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002488 return;
2489 }
2490
2491 // Otherwise, don't warn if our current context is deprecated.
2492 if (isDeclDeprecated(cast<Decl>(CurContext)))
2493 return;
2494
2495 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2496}