blob: 682a430ee40638c217d24039338db0a6e608f769 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000024using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000026
Chris Lattner58418ff2008-06-29 00:16:31 +000027//===----------------------------------------------------------------------===//
28// Helper functions
29//===----------------------------------------------------------------------===//
30
Ted Kremenek527042b2009-08-14 20:49:40 +000031static const FunctionType *getFunctionType(const Decl *d,
32 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000034 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000036 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000037 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000038 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039 Ty = decl->getUnderlyingType();
40 else
41 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000042
Chris Lattner2c6fcf52008-06-26 18:38:35 +000043 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000044 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000045 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000046 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000047
John McCall9dd450b2009-09-21 23:43:11 +000048 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000049}
50
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000051// FIXME: We should provide an abstraction around a method or function
52// to provide the following bits of information.
53
Nuno Lopes518e3702009-12-20 23:11:08 +000054/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000055/// type (function or function-typed variable).
56static bool isFunction(const Decl *d) {
57 return getFunctionType(d, false) != NULL;
58}
59
60/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061/// type (function or function-typed variable) or an Objective-C
62/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000063static bool isFunctionOrMethod(const Decl *d) {
64 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000065}
66
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067/// isFunctionOrMethodOrBlock - Return true if the given decl has function
68/// type (function or function-typed variable) or an Objective-C
69/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000070static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000071 if (isFunctionOrMethod(d))
72 return true;
73 // check for block is more involved.
74 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
75 QualType Ty = V->getType();
76 return Ty->isBlockPointerType();
77 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000078 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000079}
80
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000081/// hasFunctionProto - Return true if the given decl has a argument
82/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000083/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000084static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000085 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000086 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000087 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000088 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000089 return true;
90 }
91}
92
93/// getFunctionOrMethodNumArgs - Return number of function or method
94/// arguments. It is an error to call this on a K&R function (use
95/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000096static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000097 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000098 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000099 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
100 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +0000101 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000102}
103
Ted Kremenek527042b2009-08-14 20:49:40 +0000104static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000105 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000106 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000107 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
108 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000109
Chris Lattnera4997152009-02-20 18:43:26 +0000110 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000111}
112
Ted Kremenek527042b2009-08-14 20:49:40 +0000113static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000114 if (const FunctionType *FnTy = getFunctionType(d))
115 return cast<FunctionProtoType>(FnTy)->getResultType();
116 return cast<ObjCMethodDecl>(d)->getResultType();
117}
118
Ted Kremenek527042b2009-08-14 20:49:40 +0000119static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000120 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000121 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000122 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000123 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000124 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000125 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126 return cast<ObjCMethodDecl>(d)->isVariadic();
127 }
128}
129
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000130static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000131 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000132 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000133 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000134
John McCall96fa4842010-05-17 21:00:27 +0000135 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
136 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000137 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
John McCall96fa4842010-05-17 21:00:27 +0000139 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000140
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000141 // FIXME: Should we walk the chain of classes?
142 return ClsName == &Ctx.Idents.get("NSString") ||
143 ClsName == &Ctx.Idents.get("NSMutableString");
144}
145
Daniel Dunbar980c6692008-09-26 03:32:58 +0000146static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000147 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000148 if (!PT)
149 return false;
150
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000151 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000152 if (!RT)
153 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000154
Daniel Dunbar980c6692008-09-26 03:32:58 +0000155 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000156 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000157 return false;
158
159 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
160}
161
Chris Lattner58418ff2008-06-29 00:16:31 +0000162//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000163// Attribute Implementations
164//===----------------------------------------------------------------------===//
165
Daniel Dunbar032db472008-07-31 22:40:48 +0000166// FIXME: All this manual attribute parsing code is gross. At the
167// least add some helper functions to check most argument patterns (#
168// and types of args).
169
Mike Stumpd3bb5572009-07-24 19:02:52 +0000170static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000171 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000172 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
173 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000174 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000175 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000177
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000178 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000179
180 Expr *sizeExpr;
181
182 // Special case where the argument is a template id.
183 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000184 CXXScopeSpec SS;
185 UnqualifiedId id;
186 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
187 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000188 } else {
189 // check the attribute arguments.
190 if (Attr.getNumArgs() != 1) {
191 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
192 return;
193 }
194 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000195 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000196
197 // Instantiate/Install the vector type, and let Sema build the type for us.
198 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000199 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000200 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000201 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000202 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000203
Douglas Gregor758a8692009-06-17 21:51:59 +0000204 // Remember this typedef decl, we will need it later for diagnostics.
205 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000206 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000207}
208
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000209static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000210 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000211 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 return;
214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000215
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000216 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000217 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000218 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
219 // If the alignment is less than or equal to 8 bits, the packed attribute
220 // has no effect.
221 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000222 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000223 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000224 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000225 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000226 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000227 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000228 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000229}
230
Ted Kremenek1f672822010-02-18 03:08:58 +0000231static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000232 // check the attribute arguments.
233 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000235 return;
236 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000237
Ted Kremenek1f672822010-02-18 03:08:58 +0000238 // The IBAction attributes only apply to instance methods.
239 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
240 if (MD->isInstanceMethod()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000241 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000242 return;
243 }
244
245 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
246}
247
248static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
249 // check the attribute arguments.
250 if (Attr.getNumArgs() > 0) {
251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
252 return;
253 }
254
255 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000256 // Objective-C classes.
257 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000258 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000259 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000260 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000261
262 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000263}
264
Ted Kremenek26bde772010-05-19 17:38:06 +0000265static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
266 Sema &S) {
267
268 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000269 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000270 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
271 return;
272 }
273
274 // The IBOutletCollection attributes only apply to instance variables of
275 // Objective-C classes.
276 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
277 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
278 return;
279 }
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000280 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
281 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
282 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
283 << VD->getType() << 0;
284 return;
285 }
286 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
287 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
288 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
289 << PD->getType() << 1;
290 return;
291 }
292
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000293 IdentifierInfo *II = Attr.getParameterName();
294 if (!II)
295 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000296
John McCallba7bf592010-08-24 05:47:05 +0000297 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000298 S.getScopeForContext(d->getDeclContext()->getParent()));
299 if (!TypeRep) {
300 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
301 return;
302 }
John McCallba7bf592010-08-24 05:47:05 +0000303 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000304 // Diagnose use of non-object type in iboutletcollection attribute.
305 // FIXME. Gnu attribute extension ignores use of builtin types in
306 // attributes. So, __attribute__((iboutletcollection(char))) will be
307 // treated as __attribute__((iboutletcollection())).
308 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
309 !QT->isObjCObjectType()) {
310 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
311 return;
312 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000313 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
314 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000315}
316
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000317static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000318 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
319 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000320 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000321 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000322 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000323 return;
324 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000325
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000326 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000327
328 // The nonnull attribute only applies to pointers.
329 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000330
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000331 for (AttributeList::arg_iterator I=Attr.arg_begin(),
332 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000333
334
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000335 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000336 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000337 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000338 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
339 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000340 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
341 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000342 return;
343 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000344
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000345 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000346
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000347 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000348 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000349 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000350 return;
351 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000352
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000353 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000354
355 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000356 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000357 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000358 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000359 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000360 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000361 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000362 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000363
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000364 NonNullArgs.push_back(x);
365 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000366
367 // If no arguments were specified to __attribute__((nonnull)) then all pointer
368 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000369 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000370 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
371 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000372 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000373 NonNullArgs.push_back(I);
Fariborz Jahanian3567c422010-09-27 22:42:37 +0000374 else if (const RecordType *UT = T->getAsUnionType()) {
375 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
376 RecordDecl *UD = UT->getDecl();
377 for (RecordDecl::field_iterator it = UD->field_begin(),
378 itend = UD->field_end(); it != itend; ++it) {
379 T = it->getType();
380 if (T->isAnyPointerType() || T->isBlockPointerType()) {
381 NonNullArgs.push_back(I);
382 break;
383 }
384 }
385 }
386 }
Ted Kremenek5fa50522008-11-18 06:52:58 +0000387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000388
Ted Kremenek0f107e42010-09-09 01:17:32 +0000389 // No pointer arguments? The attribute in this case is
390 // trivially satisfied.
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000391 if (NonNullArgs.empty()) {
392 // Warn the trivial case only if attribute is not coming from a
393 // macro instantiation.
394 if (Attr.getLoc().isFileID())
395 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000396 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000397 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000398 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000399
400 unsigned* start = &NonNullArgs[0];
401 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000402 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000403 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
404 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000405}
406
Ted Kremenekd21139a2010-07-31 01:52:11 +0000407static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
408 // This attribute must be applied to a function declaration.
409 // The first argument to the attribute must be a string,
410 // the name of the resource, for example "malloc".
411 // The following arguments must be argument indexes, the arguments must be
412 // of integer type for Returns, otherwise of pointer type.
413 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000414 // after being held. free() should be __attribute((ownership_takes)), whereas
415 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000416
417 if (!AL.getParameterName()) {
418 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
419 << AL.getName()->getName() << 1;
420 return;
421 }
422 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000423 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000424 switch (AL.getKind()) {
425 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000426 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000427 if (AL.getNumArgs() < 1) {
428 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
429 return;
430 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000431 break;
432 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000433 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000434 if (AL.getNumArgs() < 1) {
435 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
436 return;
437 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000438 break;
439 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000440 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000441 if (AL.getNumArgs() > 1) {
442 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
443 << AL.getNumArgs() + 1;
444 return;
445 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000446 break;
447 default:
448 // This should never happen given how we are called.
449 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000450 }
451
452 if (!isFunction(d) || !hasFunctionProto(d)) {
453 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
454 << 0 /*function*/;
455 return;
456 }
457
458 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
459
460 llvm::StringRef Module = AL.getParameterName()->getName();
461
462 // Normalize the argument, __foo__ becomes foo.
463 if (Module.startswith("__") && Module.endswith("__"))
464 Module = Module.substr(2, Module.size() - 4);
465
466 llvm::SmallVector<unsigned, 10> OwnershipArgs;
467
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000468 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
469 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000470
471 Expr *IdxExpr = static_cast<Expr *>(*I);
472 llvm::APSInt ArgNum(32);
473 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
474 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
475 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
476 << AL.getName()->getName() << IdxExpr->getSourceRange();
477 continue;
478 }
479
480 unsigned x = (unsigned) ArgNum.getZExtValue();
481
482 if (x > NumArgs || x < 1) {
483 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
484 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
485 continue;
486 }
487 --x;
488 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000489 case OwnershipAttr::Takes:
490 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000491 // Is the function argument a pointer type?
492 QualType T = getFunctionOrMethodArgType(d, x);
493 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
494 // FIXME: Should also highlight argument in decl.
495 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000496 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000497 << "pointer"
498 << IdxExpr->getSourceRange();
499 continue;
500 }
501 break;
502 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000503 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000504 if (AL.getNumArgs() > 1) {
505 // Is the function argument an integer type?
506 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
507 llvm::APSInt ArgNum(32);
508 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
509 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
510 S.Diag(AL.getLoc(), diag::err_ownership_type)
511 << "ownership_returns" << "integer"
512 << IdxExpr->getSourceRange();
513 return;
514 }
515 }
516 break;
517 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000518 default:
519 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000520 } // switch
521
522 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000523 for (specific_attr_iterator<OwnershipAttr>
524 i = d->specific_attr_begin<OwnershipAttr>(),
525 e = d->specific_attr_end<OwnershipAttr>();
526 i != e; ++i) {
527 if ((*i)->getOwnKind() != K) {
528 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
529 I!=E; ++I) {
530 if (x == *I) {
531 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
532 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000533 }
534 }
535 }
536 }
537 OwnershipArgs.push_back(x);
538 }
539
540 unsigned* start = OwnershipArgs.data();
541 unsigned size = OwnershipArgs.size();
542 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000543
544 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
545 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
546 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000547 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000548
549 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
550 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000551}
552
Rafael Espindolac18086a2010-02-23 22:00:30 +0000553static bool isStaticVarOrStaticFunciton(Decl *D) {
554 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000555 return VD->getStorageClass() == SC_Static;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000556 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000557 return FD->getStorageClass() == SC_Static;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000558 return false;
559}
560
561static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
562 // Check the attribute arguments.
563 if (Attr.getNumArgs() > 1) {
564 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
565 return;
566 }
567
568 // gcc rejects
569 // class c {
570 // static int a __attribute__((weakref ("v2")));
571 // static int b() __attribute__((weakref ("f3")));
572 // };
573 // and ignores the attributes of
574 // void f(void) {
575 // static int a __attribute__((weakref ("v2")));
576 // }
577 // we reject them
Sebastian Redl50c68252010-08-31 00:36:30 +0000578 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
579 if (!Ctx->isFileContext()) {
580 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
581 dyn_cast<NamedDecl>(d)->getNameAsString();
582 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000583 }
584
585 // The GCC manual says
586 //
587 // At present, a declaration to which `weakref' is attached can only
588 // be `static'.
589 //
590 // It also says
591 //
592 // Without a TARGET,
593 // given as an argument to `weakref' or to `alias', `weakref' is
594 // equivalent to `weak'.
595 //
596 // gcc 4.4.1 will accept
597 // int a7 __attribute__((weakref));
598 // as
599 // int a7 __attribute__((weak));
600 // This looks like a bug in gcc. We reject that for now. We should revisit
601 // it if this behaviour is actually used.
602
603 if (!isStaticVarOrStaticFunciton(d)) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
605 dyn_cast<NamedDecl>(d)->getNameAsString();
606 return;
607 }
608
609 // GCC rejects
610 // static ((alias ("y"), weakref)).
611 // Should we? How to check that weakref is before or after alias?
612
613 if (Attr.getNumArgs() == 1) {
614 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
615 Arg = Arg->IgnoreParenCasts();
616 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
617
618 if (Str == 0 || Str->isWide()) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
620 << "weakref" << 1;
621 return;
622 }
623 // GCC will accept anything as the argument of weakref. Should we
624 // check for an existing decl?
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000625 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000626 }
627
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000628 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000629}
630
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000631static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000632 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000633 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000635 return;
636 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000637
Chris Lattner4a927cb2008-06-28 23:36:30 +0000638 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000639 Arg = Arg->IgnoreParenCasts();
640 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000641
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000642 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000643 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000644 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000645 return;
646 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000647
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000648 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000649
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000650 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000651}
652
Mike Stumpd3bb5572009-07-24 19:02:52 +0000653static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000654 Sema &S) {
655 // check the attribute arguments.
656 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000658 return;
659 }
Anders Carlsson88097122009-02-19 19:16:48 +0000660
Chris Lattner4225e232009-04-14 17:02:11 +0000661 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000663 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000664 return;
665 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000666
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000667 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000668}
669
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000670static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
671 // check the attribute arguments.
672 if (Attr.getNumArgs() != 0) {
673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
674 return;
675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremenek08479ae2009-08-15 00:51:46 +0000677 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000678 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000679 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000680 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000681 return;
682 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000683 }
684
Ted Kremenek08479ae2009-08-15 00:51:46 +0000685 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000686}
687
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000688static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000689 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
690 assert(Attr.isInvalid() == false);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000691 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000692}
693
694static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
695 Sema &S) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000696
697 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
698 // because 'analyzer_noreturn' does not impact the type.
699
700 if (Attr.getNumArgs() != 0) {
701 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
702 return;
703 }
704
705 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
706 ValueDecl *VD = dyn_cast<ValueDecl>(d);
707 if (VD == 0 || (!VD->getType()->isBlockPointerType()
708 && !VD->getType()->isFunctionPointerType())) {
709 S.Diag(Attr.getLoc(),
710 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
711 : diag::warn_attribute_wrong_decl_type)
712 << Attr.getName() << 0 /*function*/;
713 return;
714 }
715 }
716
717 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000718}
719
John Thompsoncdb847ba2010-08-09 21:53:52 +0000720// PS3 PPU-specific.
721static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
722 Sema &S) {
723/*
724 Returning a Vector Class in Registers
725
726 According to the PPU ABI specifications, a class with a single member of vector type is returned in
727 memory when used as the return value of a function. This results in inefficient code when implementing
728 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
729 definition. This attribute is also applicable to struct types.
730
731 Example:
732
733 struct Vector
734 {
735 __vector float xyzw;
736 } __attribute__((vecreturn));
737
738 Vector Add(Vector lhs, Vector rhs)
739 {
740 Vector result;
741 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
742 return result; // This will be returned in a register
743 }
744*/
John Thompson9a587aaa2010-09-18 01:12:07 +0000745 if (!isa<RecordDecl>(d)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
747 << Attr.getName() << 9 /*class*/;
748 return;
749 }
750
751 if (d->getAttr<VecReturnAttr>()) {
752 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
753 return;
754 }
755
John Thompson9a587aaa2010-09-18 01:12:07 +0000756 RecordDecl *record = cast<RecordDecl>(d);
757 int count = 0;
758
759 if (!isa<CXXRecordDecl>(record)) {
760 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
761 return;
762 }
763
764 if (!cast<CXXRecordDecl>(record)->isPOD()) {
765 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
766 return;
767 }
768
769 for (RecordDecl::field_iterator iter = record->field_begin(); iter != record->field_end(); iter++) {
770 if ((count == 1) || !iter->getType()->isVectorType()) {
771 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
772 return;
773 }
774 count++;
775 }
776
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000777 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000778}
779
Alexis Hunt96d5c762009-11-21 08:43:09 +0000780static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
781 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000783 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000784 return;
785 }
786 // FIXME: Actually store the attribute on the declaration
787}
788
Ted Kremenek39c59a82008-07-25 04:39:19 +0000789static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
790 // check the attribute arguments.
791 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000792 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000793 return;
794 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000795
John McCallcef15822010-03-31 02:47:45 +0000796 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
797 !isa<TypeDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000798 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000799 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000800 return;
801 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000802
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000803 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000804}
805
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000806static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
807 // check the attribute arguments.
808 if (Attr.getNumArgs() != 0) {
809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
810 return;
811 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000812
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000813 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000814 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000815 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
816 return;
817 }
818 } else if (!isFunctionOrMethod(d)) {
819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000820 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000821 return;
822 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000823
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000824 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000825}
826
Daniel Dunbar032db472008-07-31 22:40:48 +0000827static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
828 // check the attribute arguments.
829 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000830 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
831 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000832 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000833 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000834
835 int priority = 65535; // FIXME: Do not hardcode such constants.
836 if (Attr.getNumArgs() > 0) {
837 Expr *E = static_cast<Expr *>(Attr.getArg(0));
838 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000839 if (E->isTypeDependent() || E->isValueDependent() ||
840 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000841 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000842 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000843 return;
844 }
845 priority = Idx.getZExtValue();
846 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000847
Chris Lattner4225e232009-04-14 17:02:11 +0000848 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000849 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000850 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000851 return;
852 }
853
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000854 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000855}
856
857static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
858 // check the attribute arguments.
859 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
861 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000862 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000863 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000864
865 int priority = 65535; // FIXME: Do not hardcode such constants.
866 if (Attr.getNumArgs() > 0) {
867 Expr *E = static_cast<Expr *>(Attr.getArg(0));
868 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000869 if (E->isTypeDependent() || E->isValueDependent() ||
870 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000871 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000872 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000873 return;
874 }
875 priority = Idx.getZExtValue();
876 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000877
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000878 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000879 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000880 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000881 return;
882 }
883
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000884 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000885}
886
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000887static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000888 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000889 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000890 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000891 return;
892 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000893
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000894 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000895}
896
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000897static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
898 // check the attribute arguments.
899 if (Attr.getNumArgs() != 0) {
900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
901 return;
902 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000903
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000904 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000905}
906
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000907static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000908 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000909 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000910 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000911 return;
912 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000913
Chris Lattner4a927cb2008-06-28 23:36:30 +0000914 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000915 Arg = Arg->IgnoreParenCasts();
916 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000917
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000918 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000919 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000920 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000921 return;
922 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000923
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000924 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000925 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000926
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000927 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000928 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000929 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000930 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000931 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000932 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000933 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000934 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000935 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000936 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000937 return;
938 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000939
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000940 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000941}
942
Chris Lattner677a3582009-02-14 08:09:34 +0000943static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
944 Sema &S) {
945 if (Attr.getNumArgs() != 0) {
946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
947 return;
948 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000949
Chris Lattner677a3582009-02-14 08:09:34 +0000950 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
951 if (OCI == 0) {
952 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
953 return;
954 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000955
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000956 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +0000957}
958
959static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000960 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +0000961 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000962 return;
963 }
Chris Lattner677a3582009-02-14 08:09:34 +0000964 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000965 QualType T = TD->getUnderlyingType();
966 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000967 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000968 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
969 return;
970 }
971 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000972 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000973}
974
Mike Stumpd3bb5572009-07-24 19:02:52 +0000975static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000976HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
977 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +0000978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000979 return;
980 }
981
982 if (!isa<FunctionDecl>(D)) {
983 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
984 return;
985 }
986
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000987 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000988}
989
Steve Naroff3405a732008-09-18 16:44:58 +0000990static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000991 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000993 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000994 return;
995 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000996
Steve Naroff3405a732008-09-18 16:44:58 +0000997 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000998 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000999 return;
1000 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001001
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001002 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001003 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001004 type = BlocksAttr::ByRef;
1005 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001006 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001007 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001008 return;
1009 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001010
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001011 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001012}
1013
Anders Carlssonc181b012008-10-05 18:05:59 +00001014static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1015 // check the attribute arguments.
1016 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +00001017 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1018 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +00001019 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001020 }
1021
Anders Carlssonc181b012008-10-05 18:05:59 +00001022 int sentinel = 0;
1023 if (Attr.getNumArgs() > 0) {
1024 Expr *E = static_cast<Expr *>(Attr.getArg(0));
1025 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001026 if (E->isTypeDependent() || E->isValueDependent() ||
1027 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001028 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001029 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001030 return;
1031 }
1032 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001033
Anders Carlssonc181b012008-10-05 18:05:59 +00001034 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1036 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001037 return;
1038 }
1039 }
1040
1041 int nullPos = 0;
1042 if (Attr.getNumArgs() > 1) {
1043 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1044 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001045 if (E->isTypeDependent() || E->isValueDependent() ||
1046 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001047 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001048 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001049 return;
1050 }
1051 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001052
Anders Carlssonc181b012008-10-05 18:05:59 +00001053 if (nullPos > 1 || nullPos < 0) {
1054 // FIXME: This error message could be improved, it would be nice
1055 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001056 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1057 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001058 return;
1059 }
1060 }
1061
1062 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001063 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001064 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001065
Chris Lattner9363e312009-03-17 23:03:47 +00001066 if (isa<FunctionNoProtoType>(FT)) {
1067 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1068 return;
1069 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001070
Chris Lattner9363e312009-03-17 23:03:47 +00001071 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001072 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001073 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001074 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001075 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1076 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001077 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001078 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001079 }
1080 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001081 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1082 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001083 ;
1084 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001085 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001086 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001087 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +00001088 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001089 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001090 int m = Ty->isFunctionPointerType() ? 0 : 1;
1091 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001092 return;
1093 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001094 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001095 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001096 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001097 return;
1098 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001099 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001100 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001101 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +00001102 return;
1103 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001104 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001105}
1106
Chris Lattner237f2752009-02-14 07:37:35 +00001107static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1108 // check the attribute arguments.
1109 if (Attr.getNumArgs() != 0) {
1110 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1111 return;
1112 }
1113
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001114 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001115 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001116 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +00001117 return;
1118 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001119
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001120 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1121 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1122 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001123 return;
1124 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001125 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1126 if (MD->getResultType()->isVoidType()) {
1127 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1128 << Attr.getName() << 1;
1129 return;
1130 }
1131
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001132 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001133}
1134
1135static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001136 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001137 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001138 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001139 return;
1140 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001141
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001142 /* weak only applies to non-static declarations */
Rafael Espindolac18086a2010-02-23 22:00:30 +00001143 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001144 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1145 dyn_cast<NamedDecl>(D)->getNameAsString();
1146 return;
1147 }
1148
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001149 // TODO: could also be applied to methods?
1150 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1151 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001152 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001153 return;
1154 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001156 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001157}
1158
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001159static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1160 // check the attribute arguments.
1161 if (Attr.getNumArgs() != 0) {
1162 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1163 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001164 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001165
1166 // weak_import only applies to variable & function declarations.
1167 bool isDef = false;
1168 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1169 isDef = (!VD->hasExternalStorage() || VD->getInit());
1170 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001171 isDef = FD->hasBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +00001172 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1173 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +00001174 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +00001175 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001176 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001177 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001178 !isa<ObjCInterfaceDecl>(D))
1179 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001180 << Attr.getName() << 2 /*variable and function*/;
1181 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001182 }
1183
1184 // Merge should handle any subsequent violations.
1185 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001186 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001187 diag::warn_attribute_weak_import_invalid_on_definition)
1188 << "weak_import" << 2 /*variable and function*/;
1189 return;
1190 }
1191
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001192 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001193}
1194
Nate Begemanf2758702009-06-26 06:32:41 +00001195static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1196 Sema &S) {
1197 // Attribute has 3 arguments.
1198 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001200 return;
1201 }
1202
1203 unsigned WGSize[3];
1204 for (unsigned i = 0; i < 3; ++i) {
1205 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1206 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001207 if (E->isTypeDependent() || E->isValueDependent() ||
1208 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001209 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1210 << "reqd_work_group_size" << E->getSourceRange();
1211 return;
1212 }
1213 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1214 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001215 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1216 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001217 WGSize[2]));
1218}
1219
Chris Lattner237f2752009-02-14 07:37:35 +00001220static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001221 // Attribute has no arguments.
1222 if (Attr.getNumArgs() != 1) {
1223 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1224 return;
1225 }
1226
1227 // Make sure that there is a string literal as the sections's single
1228 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +00001229 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1230 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001231 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001232 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001233 return;
1234 }
Mike Stump11289f42009-09-09 15:08:12 +00001235
Chris Lattner30ba6742009-08-10 19:03:04 +00001236 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001237 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001238 if (!Error.empty()) {
1239 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1240 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001241 return;
1242 }
Mike Stump11289f42009-09-09 15:08:12 +00001243
Chris Lattner20aee9b2010-01-12 20:58:53 +00001244 // This attribute cannot be applied to local variables.
1245 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1246 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1247 return;
1248 }
1249
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001250 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001251}
1252
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001253
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001254static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001255 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001256 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001258 return;
1259 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001260
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001261 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001262}
1263
Anders Carlssonb8316282008-10-05 23:32:53 +00001264static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1265 // check the attribute arguments.
1266 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001268 return;
1269 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001270
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001271 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001272}
1273
1274static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1275 // check the attribute arguments.
1276 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001278 return;
1279 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001280
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001281 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001282}
1283
Anders Carlssond277d792009-01-31 01:16:18 +00001284static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001285 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1287 return;
1288 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001289
Anders Carlssond277d792009-01-31 01:16:18 +00001290 if (Attr.getNumArgs() != 0) {
1291 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1292 return;
1293 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001294
Anders Carlssond277d792009-01-31 01:16:18 +00001295 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001296
Anders Carlssond277d792009-01-31 01:16:18 +00001297 if (!VD || !VD->hasLocalStorage()) {
1298 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1299 return;
1300 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001301
Anders Carlssond277d792009-01-31 01:16:18 +00001302 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001303 // FIXME: Lookup probably isn't looking in the right place
1304 // FIXME: The lookup source location should be in the attribute, not the
1305 // start of the attribute.
John McCall9f3059a2009-10-09 21:13:30 +00001306 NamedDecl *CleanupDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001307 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCall9f3059a2009-10-09 21:13:30 +00001308 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001309 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001310 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001311 Attr.getParameterName();
1312 return;
1313 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001314
Anders Carlssond277d792009-01-31 01:16:18 +00001315 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1316 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001317 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001318 Attr.getParameterName();
1319 return;
1320 }
1321
Anders Carlssond277d792009-01-31 01:16:18 +00001322 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001323 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001324 Attr.getParameterName();
1325 return;
1326 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001327
Anders Carlsson723f55d2009-02-07 23:16:50 +00001328 // We're currently more strict than GCC about what function types we accept.
1329 // If this ever proves to be a problem it should be easy to fix.
1330 QualType Ty = S.Context.getPointerType(VD->getType());
1331 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001332 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001333 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001334 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1335 Attr.getParameterName() << ParamTy << Ty;
1336 return;
1337 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001338
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001339 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001340}
1341
Mike Stumpd3bb5572009-07-24 19:02:52 +00001342/// Handle __attribute__((format_arg((idx)))) attribute based on
1343/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1344static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001345 if (Attr.getNumArgs() != 1) {
1346 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1347 return;
1348 }
1349 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1350 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1351 << Attr.getName() << 0 /*function*/;
1352 return;
1353 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001354 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1355 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001356 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1357 unsigned FirstIdx = 1;
1358 // checks for the 2nd argument
1359 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1360 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001361 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1362 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001363 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1364 << "format" << 2 << IdxExpr->getSourceRange();
1365 return;
1366 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001367
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001368 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1369 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1370 << "format" << 2 << IdxExpr->getSourceRange();
1371 return;
1372 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001373
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001374 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001375
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001376 // make sure the format string is really a string
1377 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001378
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001379 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1380 if (not_nsstring_type &&
1381 !isCFStringType(Ty, S.Context) &&
1382 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001383 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001384 // FIXME: Should highlight the actual expression that has the wrong type.
1385 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001386 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001387 << IdxExpr->getSourceRange();
1388 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001389 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001390 Ty = getFunctionOrMethodResultType(d);
1391 if (!isNSStringType(Ty, S.Context) &&
1392 !isCFStringType(Ty, S.Context) &&
1393 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001394 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001395 // FIXME: Should highlight the actual expression that has the wrong type.
1396 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001397 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001398 << IdxExpr->getSourceRange();
1399 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001400 }
1401
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001402 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001403}
1404
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001405enum FormatAttrKind {
1406 CFStringFormat,
1407 NSStringFormat,
1408 StrftimeFormat,
1409 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001410 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001411 InvalidFormat
1412};
1413
1414/// getFormatAttrKind - Map from format attribute names to supported format
1415/// types.
1416static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1417 // Check for formats that get handled specially.
1418 if (Format == "NSString")
1419 return NSStringFormat;
1420 if (Format == "CFString")
1421 return CFStringFormat;
1422 if (Format == "strftime")
1423 return StrftimeFormat;
1424
1425 // Otherwise, check for supported formats.
1426 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1427 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1428 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1429 Format == "zcmn_err")
1430 return SupportedFormat;
1431
Duncan Sandsde4fe352010-03-23 14:44:19 +00001432 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1433 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001434 return IgnoredFormat;
1435
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001436 return InvalidFormat;
1437}
1438
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001439/// Handle __attribute__((init_priority(priority))) attributes based on
1440/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1441static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1442 Sema &S) {
1443 if (!S.getLangOptions().CPlusPlus) {
1444 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1445 return;
1446 }
1447
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001448 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1449 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1450 Attr.setInvalid();
1451 return;
1452 }
1453 QualType T = dyn_cast<VarDecl>(d)->getType();
1454 if (S.Context.getAsArrayType(T))
1455 T = S.Context.getBaseElementType(T);
1456 if (!T->getAs<RecordType>()) {
1457 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1458 Attr.setInvalid();
1459 return;
1460 }
1461
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001462 if (Attr.getNumArgs() != 1) {
1463 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1464 Attr.setInvalid();
1465 return;
1466 }
1467 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001468
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001469 llvm::APSInt priority(32);
1470 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1471 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1472 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1473 << "init_priority" << priorityExpr->getSourceRange();
1474 Attr.setInvalid();
1475 return;
1476 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001477 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001478 if (prioritynum < 101 || prioritynum > 65535) {
1479 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1480 << priorityExpr->getSourceRange();
1481 Attr.setInvalid();
1482 return;
1483 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001484 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001485}
1486
Mike Stumpd3bb5572009-07-24 19:02:52 +00001487/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1488/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001489static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001490
Chris Lattner4a927cb2008-06-28 23:36:30 +00001491 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001492 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001493 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001494 return;
1495 }
1496
Chris Lattner4a927cb2008-06-28 23:36:30 +00001497 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001498 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001499 return;
1500 }
1501
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001502 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001503 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001504 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001505 return;
1506 }
1507
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001508 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001509 unsigned FirstIdx = 1;
1510
Daniel Dunbar07d07852009-10-18 21:17:35 +00001511 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001512
1513 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001514 if (Format.startswith("__") && Format.endswith("__"))
1515 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001516
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001517 // Check for supported formats.
1518 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001519
1520 if (Kind == IgnoredFormat)
1521 return;
1522
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001523 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001524 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001525 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001526 return;
1527 }
1528
1529 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001530 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001531 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001532 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1533 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001534 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001535 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001536 return;
1537 }
1538
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001539 // FIXME: We should handle the implicit 'this' parameter in a more generic
1540 // way that can be used for other arguments.
1541 bool HasImplicitThisParam = false;
1542 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1543 if (MD->isInstance()) {
1544 HasImplicitThisParam = true;
1545 NumArgs++;
1546 }
1547 }
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001549 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001550 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001551 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001552 return;
1553 }
1554
1555 // FIXME: Do we need to bounds check?
1556 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001557
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001558 if (HasImplicitThisParam) {
1559 if (ArgIdx == 0) {
1560 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1561 << "a string type" << IdxExpr->getSourceRange();
1562 return;
1563 }
1564 ArgIdx--;
1565 }
Mike Stump11289f42009-09-09 15:08:12 +00001566
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001567 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001568 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001569
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001570 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001571 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001572 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1573 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001574 return;
1575 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001576 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001577 // FIXME: do we need to check if the type is NSString*? What are the
1578 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001579 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001580 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001581 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1582 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001583 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001584 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001585 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001586 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001587 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001588 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1589 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001590 return;
1591 }
1592
1593 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001594 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001595 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001596 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1597 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001598 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001599 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001600 return;
1601 }
1602
1603 // check if the function is variadic if the 3rd argument non-zero
1604 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001605 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001606 ++NumArgs; // +1 for ...
1607 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001608 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001609 return;
1610 }
1611 }
1612
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001613 // strftime requires FirstArg to be 0 because it doesn't read from any
1614 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001615 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001616 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001617 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1618 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001619 return;
1620 }
1621 // if 0 it disables parameter checking (to use with e.g. va_list)
1622 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001623 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001624 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001625 return;
1626 }
1627
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001628 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1629 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001630 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001631}
1632
Chris Lattnera663a0a2008-06-29 00:28:59 +00001633static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1634 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001635 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001636 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001638 return;
1639 }
1640
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001641 // Try to find the underlying union declaration.
1642 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001643 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001644 if (TD && TD->getUnderlyingType()->isUnionType())
1645 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1646 else
1647 RD = dyn_cast<RecordDecl>(d);
1648
1649 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001650 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001651 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001652 return;
1653 }
1654
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001655 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001656 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001657 diag::warn_transparent_union_attribute_not_definition);
1658 return;
1659 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001660
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001661 RecordDecl::field_iterator Field = RD->field_begin(),
1662 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001663 if (Field == FieldEnd) {
1664 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1665 return;
1666 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001667
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001668 FieldDecl *FirstField = *Field;
1669 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001670 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001671 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001672 diag::warn_transparent_union_attribute_floating)
1673 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001674 return;
1675 }
1676
1677 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1678 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1679 for (; Field != FieldEnd; ++Field) {
1680 QualType FieldType = Field->getType();
1681 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1682 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1683 // Warn if we drop the attribute.
1684 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001685 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001686 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001687 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001688 diag::warn_transparent_union_attribute_field_size_align)
1689 << isSize << Field->getDeclName() << FieldBits;
1690 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001691 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001692 diag::note_transparent_union_first_field_size_align)
1693 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001694 return;
1695 }
1696 }
1697
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001698 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001699}
1700
Chris Lattnera663a0a2008-06-29 00:28:59 +00001701static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001702 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001703 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001704 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001705 return;
1706 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001707 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1708 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001709
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001710 // Make sure that there is a string literal as the annotation's single
1711 // argument.
1712 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001713 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001714 return;
1715 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001716 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001717}
1718
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001719static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001720 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001721 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001723 return;
1724 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001725
1726 //FIXME: The C++0x version of this attribute has more limited applicabilty
1727 // than GNU's, and should error out when it is used to specify a
1728 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001729
Chris Lattner4a927cb2008-06-28 23:36:30 +00001730 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001731 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001732 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001733 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001734
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001735 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1736}
1737
1738void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1739 if (E->isTypeDependent() || E->isValueDependent()) {
1740 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001741 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001742 return;
1743 }
1744
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001745 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00001746 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001747 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1748 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1749 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001750 return;
1751 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001752 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001753 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1754 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001755 return;
1756 }
1757
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001758 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1759}
1760
1761void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1762 // FIXME: Cache the number on the Attr object if non-dependent?
1763 // FIXME: Perform checking of type validity
1764 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1765 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001766}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001767
Mike Stumpd3bb5572009-07-24 19:02:52 +00001768/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1769/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001770///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001771/// Despite what would be logical, the mode attribute is a decl attribute, not a
1772/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1773/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001774static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001775 // This attribute isn't documented, but glibc uses it. It changes
1776 // the width of an int or unsigned int to the specified size.
1777
1778 // Check that there aren't any arguments
1779 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001780 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001781 return;
1782 }
1783
1784 IdentifierInfo *Name = Attr.getParameterName();
1785 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001786 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001787 return;
1788 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001789
Daniel Dunbar07d07852009-10-18 21:17:35 +00001790 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001791
1792 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001793 if (Str.startswith("__") && Str.endswith("__"))
1794 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001795
1796 unsigned DestWidth = 0;
1797 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001798 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001799 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001800 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001801 switch (Str[0]) {
1802 case 'Q': DestWidth = 8; break;
1803 case 'H': DestWidth = 16; break;
1804 case 'S': DestWidth = 32; break;
1805 case 'D': DestWidth = 64; break;
1806 case 'X': DestWidth = 96; break;
1807 case 'T': DestWidth = 128; break;
1808 }
1809 if (Str[1] == 'F') {
1810 IntegerMode = false;
1811 } else if (Str[1] == 'C') {
1812 IntegerMode = false;
1813 ComplexMode = true;
1814 } else if (Str[1] != 'I') {
1815 DestWidth = 0;
1816 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001817 break;
1818 case 4:
1819 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1820 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001821 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001822 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001823 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001824 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001825 break;
1826 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001827 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001828 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001829 break;
1830 }
1831
1832 QualType OldTy;
1833 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1834 OldTy = TD->getUnderlyingType();
1835 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1836 OldTy = VD->getType();
1837 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001838 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1839 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001840 return;
1841 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001842
John McCall9dd450b2009-09-21 23:43:11 +00001843 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001844 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1845 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00001846 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001847 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1848 } else if (ComplexMode) {
1849 if (!OldTy->isComplexType())
1850 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1851 } else {
1852 if (!OldTy->isFloatingType())
1853 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1854 }
1855
Mike Stump87c57ac2009-05-16 07:39:55 +00001856 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1857 // and friends, at least with glibc.
1858 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1859 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001860 // FIXME: Make sure floating-point mappings are accurate
1861 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001862 QualType NewTy;
1863 switch (DestWidth) {
1864 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001865 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001866 return;
1867 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001868 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001869 return;
1870 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001871 if (!IntegerMode) {
1872 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1873 return;
1874 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001875 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001876 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001877 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001878 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001879 break;
1880 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001881 if (!IntegerMode) {
1882 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1883 return;
1884 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001885 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001886 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001887 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001888 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001889 break;
1890 case 32:
1891 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001892 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001893 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001894 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001895 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001896 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001897 break;
1898 case 64:
1899 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001900 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001901 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00001902 if (S.Context.Target.getLongWidth() == 64)
1903 NewTy = S.Context.LongTy;
1904 else
1905 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001906 else
Chandler Carruth72343702010-01-26 06:39:24 +00001907 if (S.Context.Target.getLongWidth() == 64)
1908 NewTy = S.Context.UnsignedLongTy;
1909 else
1910 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001911 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001912 case 96:
1913 NewTy = S.Context.LongDoubleTy;
1914 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001915 case 128:
1916 if (!IntegerMode) {
1917 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1918 return;
1919 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00001920 if (OldTy->isSignedIntegerType())
1921 NewTy = S.Context.Int128Ty;
1922 else
1923 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001924 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001925 }
1926
Eli Friedman4735374e2009-03-03 06:41:03 +00001927 if (ComplexMode) {
1928 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001929 }
1930
1931 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001932 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1933 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001934 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001935 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001936 cast<ValueDecl>(D)->setType(NewTy);
1937}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001938
Mike Stump3722f582009-08-26 22:31:08 +00001939static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001940 // check the attribute arguments.
1941 if (Attr.getNumArgs() > 0) {
1942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1943 return;
1944 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001945
Anders Carlsson88097122009-02-19 19:16:48 +00001946 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001947 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001948 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001949 return;
1950 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001951
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001952 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00001953}
1954
Mike Stump3722f582009-08-26 22:31:08 +00001955static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001956 // check the attribute arguments.
1957 if (Attr.getNumArgs() != 0) {
1958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1959 return;
1960 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001961
Chris Lattner4225e232009-04-14 17:02:11 +00001962 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001963 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001964 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001965 return;
1966 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001967
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001968 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00001969}
1970
Chris Lattner3c77a352010-06-22 00:03:40 +00001971static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1972 Sema &S) {
1973 // check the attribute arguments.
1974 if (Attr.getNumArgs() != 0) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1976 return;
1977 }
1978
1979 if (!isa<FunctionDecl>(d)) {
1980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1981 << Attr.getName() << 0 /*function*/;
1982 return;
1983 }
1984
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001985 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00001986}
1987
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001988static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001989 // check the attribute arguments.
1990 if (Attr.getNumArgs() != 0) {
1991 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1992 return;
1993 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001994
Chris Lattner4225e232009-04-14 17:02:11 +00001995 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1996 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001998 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001999 return;
2000 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002001
Douglas Gregor35b57532009-10-27 21:01:01 +00002002 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002003 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002004 return;
2005 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002006
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002007 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002008}
2009
Abramo Bagnara50099372010-04-30 13:10:51 +00002010static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2011 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2012 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2013 assert(Attr.isInvalid() == false);
2014
2015 switch (Attr.getKind()) {
2016 case AttributeList::AT_fastcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002017 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002018 return;
2019 case AttributeList::AT_stdcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002020 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002021 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002022 case AttributeList::AT_thiscall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002023 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002024 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002025 case AttributeList::AT_cdecl:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002026 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002027 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002028 case AttributeList::AT_pascal:
2029 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2030 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002031 default:
2032 llvm_unreachable("unexpected attribute kind");
2033 return;
2034 }
2035}
2036
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002037static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2038 // check the attribute arguments.
2039 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00002040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002041 return;
2042 }
Eli Friedman7044b762009-03-27 21:06:47 +00002043
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002044 if (!isFunctionOrMethod(d)) {
2045 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002046 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002047 return;
2048 }
Eli Friedman7044b762009-03-27 21:06:47 +00002049
2050 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2051 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002052 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2053 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman7044b762009-03-27 21:06:47 +00002054 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2055 << "regparm" << NumParamsExpr->getSourceRange();
2056 return;
2057 }
2058
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002059 if (S.Context.Target.getRegParmMax() == 0) {
2060 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002061 << NumParamsExpr->getSourceRange();
2062 return;
2063 }
2064
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00002065 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002066 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2067 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00002068 return;
2069 }
2070
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002071 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2072 NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002073}
2074
Alexis Hunt96d5c762009-11-21 08:43:09 +00002075static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2076 // check the attribute arguments.
2077 if (Attr.getNumArgs() != 0) {
2078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2079 return;
2080 }
2081
2082 if (!isa<CXXRecordDecl>(d)
2083 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2084 S.Diag(Attr.getLoc(),
2085 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2086 : diag::warn_attribute_wrong_decl_type)
2087 << Attr.getName() << 7 /*virtual method or class*/;
2088 return;
2089 }
Alexis Hunt54a02542009-11-25 04:20:27 +00002090
2091 // FIXME: Conform to C++0x redeclaration rules.
2092
2093 if (d->getAttr<FinalAttr>()) {
2094 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2095 return;
2096 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002097
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002098 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002099}
2100
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002101//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00002102// C++0x member checking attributes
2103//===----------------------------------------------------------------------===//
2104
2105static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2106 if (Attr.getNumArgs() != 0) {
2107 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2108 return;
2109 }
2110
2111 if (!isa<CXXRecordDecl>(d)) {
2112 S.Diag(Attr.getLoc(),
2113 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2114 : diag::warn_attribute_wrong_decl_type)
2115 << Attr.getName() << 9 /*class*/;
2116 return;
2117 }
2118
2119 if (d->getAttr<BaseCheckAttr>()) {
2120 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2121 return;
2122 }
2123
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002124 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002125}
2126
2127static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2128 if (Attr.getNumArgs() != 0) {
2129 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2130 return;
2131 }
2132
2133 if (!isa<RecordDecl>(d->getDeclContext())) {
2134 // FIXME: It's not the type that's the problem
2135 S.Diag(Attr.getLoc(),
2136 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2137 : diag::warn_attribute_wrong_decl_type)
2138 << Attr.getName() << 11 /*member*/;
2139 return;
2140 }
2141
2142 // FIXME: Conform to C++0x redeclaration rules.
2143
2144 if (d->getAttr<HidingAttr>()) {
2145 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2146 return;
2147 }
2148
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002149 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002150}
2151
2152static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2153 if (Attr.getNumArgs() != 0) {
2154 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2155 return;
2156 }
2157
2158 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2159 // FIXME: It's not the type that's the problem
2160 S.Diag(Attr.getLoc(),
2161 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2162 : diag::warn_attribute_wrong_decl_type)
2163 << Attr.getName() << 10 /*virtual method*/;
2164 return;
2165 }
2166
2167 // FIXME: Conform to C++0x redeclaration rules.
2168
2169 if (d->getAttr<OverrideAttr>()) {
2170 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2171 return;
2172 }
2173
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002174 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002175}
2176
2177//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002178// Checker-specific attribute handlers.
2179//===----------------------------------------------------------------------===//
2180
2181static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2182 Sema &S) {
2183
Ted Kremenek3b204e42009-05-13 21:07:32 +00002184 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002185
Ted Kremenek3b204e42009-05-13 21:07:32 +00002186 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2187 RetTy = MD->getResultType();
2188 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2189 RetTy = FD->getResultType();
2190 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002191 SourceLocation L = Attr.getLoc();
2192 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2193 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002194 return;
2195 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002196
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002197 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00002198 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002199 SourceLocation L = Attr.getLoc();
2200 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2201 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002202 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002203 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002204
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002205 switch (Attr.getKind()) {
2206 default:
2207 assert(0 && "invalid ownership attribute");
2208 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002209 case AttributeList::AT_cf_returns_not_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002210 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002211 return;
2212 case AttributeList::AT_ns_returns_not_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002213 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002214 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002215 case AttributeList::AT_cf_returns_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002216 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002217 return;
2218 case AttributeList::AT_ns_returns_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002219 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002220 return;
2221 };
2222}
2223
Charles Davis163855f2010-02-16 18:27:26 +00002224static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2225 return Attr.getKind() == AttributeList::AT_dllimport ||
2226 Attr.getKind() == AttributeList::AT_dllexport;
2227}
2228
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002229//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002230// Top Level Sema Entry Points
2231//===----------------------------------------------------------------------===//
2232
Sebastian Redlfc24b632008-12-21 19:24:58 +00002233/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002234/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00002235/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2236/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00002237static void ProcessDeclAttribute(Scope *scope, Decl *D,
2238 const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002239 if (Attr.isInvalid())
2240 return;
2241
Charles Davis163855f2010-02-16 18:27:26 +00002242 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2243 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00002244 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002245 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002246 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002247 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2248 case AttributeList::AT_IBOutletCollection:
2249 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002250 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002251 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002252 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002253 // Ignore these, these are type attributes, handled by
2254 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002255 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002256 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2257 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002258 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002259 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002260 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002261 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002262 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2263 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002264 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002265 HandleDependencyAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002266 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2267 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2268 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002269 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002270 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002271 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002272 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2273 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2274 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2275 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2276 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2277 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2278 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2279 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002280 case AttributeList::AT_ownership_returns:
2281 case AttributeList::AT_ownership_takes:
2282 case AttributeList::AT_ownership_holds:
2283 HandleOwnershipAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002284 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2285 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2286 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002287 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002288
2289 // Checker-specific.
Ted Kremenekd9c66632010-02-18 00:05:45 +00002290 case AttributeList::AT_ns_returns_not_retained:
2291 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002292 case AttributeList::AT_ns_returns_retained:
2293 case AttributeList::AT_cf_returns_retained:
2294 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2295
Nate Begemanf2758702009-06-26 06:32:41 +00002296 case AttributeList::AT_reqd_wg_size:
2297 HandleReqdWorkGroupSize(D, Attr, S); break;
2298
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002299 case AttributeList::AT_init_priority:
2300 HandleInitPriorityAttr(D, Attr, S); break;
2301
Alexis Hunt54a02542009-11-25 04:20:27 +00002302 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2303 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002304 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2305 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2306 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002307 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002308 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2309 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002310 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002311 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002312 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002313 case AttributeList::AT_transparent_union:
2314 HandleTransparentUnionAttr(D, Attr, S);
2315 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002316 case AttributeList::AT_objc_exception:
2317 HandleObjCExceptionAttr(D, Attr, S);
2318 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002319 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002320 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2321 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2322 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2323 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2324 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2325 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2326 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2327 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2328 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002329 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002330 // Just ignore
2331 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002332 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2333 HandleNoInstrumentFunctionAttr(D, Attr, S);
2334 break;
John McCallab26cfa2010-02-05 21:31:56 +00002335 case AttributeList::AT_stdcall:
2336 case AttributeList::AT_cdecl:
2337 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002338 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002339 case AttributeList::AT_pascal:
Abramo Bagnara50099372010-04-30 13:10:51 +00002340 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002341 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002342 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002343 // Ask target about the attribute.
2344 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2345 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002346 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2347 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002348 break;
2349 }
2350}
2351
2352/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2353/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00002354void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002355 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2356 ProcessDeclAttribute(S, D, *l, *this);
2357 }
2358
2359 // GCC accepts
2360 // static int a9 __attribute__((weakref));
2361 // but that looks really pointless. We reject it.
2362 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2363 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002364 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002365 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002366 }
2367}
2368
Ryan Flynn7d470f32009-07-30 03:15:39 +00002369/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2370/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002371NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002372 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002373 NamedDecl *NewD = 0;
2374 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2375 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2376 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002377 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002378 if (FD->getQualifier()) {
2379 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2380 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2381 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002382 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2383 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2384 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002385 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002386 VD->getStorageClass(),
2387 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002388 if (VD->getQualifier()) {
2389 VarDecl *NewVD = cast<VarDecl>(NewD);
2390 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2391 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002392 }
2393 return NewD;
2394}
2395
2396/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2397/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002398void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002399 if (W.getUsed()) return; // only do this once
2400 W.setUsed(true);
2401 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2402 IdentifierInfo *NDId = ND->getIdentifier();
2403 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002404 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2405 NDId->getName()));
2406 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00002407 WeakTopLevelDecl.push_back(NewD);
2408 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2409 // to insert Decl at TU scope, sorry.
2410 DeclContext *SavedContext = CurContext;
2411 CurContext = Context.getTranslationUnitDecl();
2412 PushOnScopeChains(NewD, S);
2413 CurContext = SavedContext;
2414 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002415 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002416 }
2417}
2418
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002419/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2420/// it, apply them to D. This is a bit tricky because PD can have attributes
2421/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002422void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002423 // Handle #pragma weak
2424 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2425 if (ND->hasLinkage()) {
2426 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2427 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002428 // Identifier referenced by #pragma weak before it was declared
2429 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002430 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2431 }
2432 }
2433 }
2434
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002435 // Apply decl attributes from the DeclSpec if present.
2436 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002437 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002438
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002439 // Walk the declarator structure, applying decl attributes that were in a type
2440 // position to the decl itself. This handles cases like:
2441 // int *__attr__(x)** D;
2442 // when X is a decl attribute.
2443 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2444 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002445 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002446
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002447 // Finally, apply any attributes on the decl itself.
2448 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002449 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002450}
John McCall28a6aea2009-11-04 02:18:39 +00002451
2452/// PushParsingDeclaration - Enter a new "scope" of deprecation
2453/// warnings.
2454///
2455/// The state token we use is the start index of this scope
2456/// on the warning stack.
John McCallfaf5fb42010-08-26 23:41:50 +00002457Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall28a6aea2009-11-04 02:18:39 +00002458 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002459 return (ParsingDeclStackState) DelayedDiagnostics.size();
2460}
2461
John McCall48871652010-08-21 09:40:31 +00002462void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall86121512010-01-27 03:50:35 +00002463 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2464 ParsingDeclDepth--;
2465
2466 if (DelayedDiagnostics.empty())
2467 return;
2468
2469 unsigned SavedIndex = (unsigned) S;
2470 assert(SavedIndex <= DelayedDiagnostics.size() &&
2471 "saved index is out of bounds");
2472
John McCall1064d7e2010-03-16 05:22:47 +00002473 unsigned E = DelayedDiagnostics.size();
2474
John McCall86121512010-01-27 03:50:35 +00002475 // We only want to actually emit delayed diagnostics when we
2476 // successfully parsed a decl.
John McCall86121512010-01-27 03:50:35 +00002477 if (D) {
2478 // We really do want to start with 0 here. We get one push for a
2479 // decl spec and another for each declarator; in a decl group like:
2480 // deprecated_typedef foo, *bar, baz();
2481 // only the declarator pops will be passed decls. This is correct;
2482 // we really do need to consider delayed diagnostics from the decl spec
2483 // for each of the different declarations.
John McCall1064d7e2010-03-16 05:22:47 +00002484 for (unsigned I = 0; I != E; ++I) {
John McCall86121512010-01-27 03:50:35 +00002485 if (DelayedDiagnostics[I].Triggered)
2486 continue;
2487
2488 switch (DelayedDiagnostics[I].Kind) {
2489 case DelayedDiagnostic::Deprecation:
2490 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2491 break;
2492
2493 case DelayedDiagnostic::Access:
2494 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2495 break;
2496 }
2497 }
2498 }
2499
John McCall1064d7e2010-03-16 05:22:47 +00002500 // Destroy all the delayed diagnostics we're about to pop off.
2501 for (unsigned I = SavedIndex; I != E; ++I)
2502 DelayedDiagnostics[I].destroy();
2503
John McCall86121512010-01-27 03:50:35 +00002504 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002505}
2506
2507static bool isDeclDeprecated(Decl *D) {
2508 do {
2509 if (D->hasAttr<DeprecatedAttr>())
2510 return true;
2511 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2512 return false;
2513}
2514
John McCallb45a1e72010-08-26 02:13:20 +00002515void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00002516 Decl *Ctx) {
2517 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002518 return;
2519
John McCall86121512010-01-27 03:50:35 +00002520 DD.Triggered = true;
2521 Diag(DD.Loc, diag::warn_deprecated)
2522 << DD.DeprecationData.Decl->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002523}
2524
2525void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2526 // Delay if we're currently parsing a declaration.
2527 if (ParsingDeclDepth) {
John McCall86121512010-01-27 03:50:35 +00002528 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall28a6aea2009-11-04 02:18:39 +00002529 return;
2530 }
2531
2532 // Otherwise, don't warn if our current context is deprecated.
2533 if (isDeclDeprecated(cast<Decl>(CurContext)))
2534 return;
2535
2536 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2537}