blob: cbc940f2f0ac4c1ed543c3b004b328881574c486 [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
John McCall3882ace2011-01-05 12:14:39 +000081/// Return true if the given decl has a declarator that should have
82/// been processed by Sema::GetTypeForDeclarator.
83static bool hasDeclarator(const Decl *d) {
84 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
85 return isa<DeclaratorDecl>(d) || isa<BlockDecl>(d) || isa<TypedefDecl>(d);
86}
87
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000088/// hasFunctionProto - Return true if the given decl has a argument
89/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000090/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000091static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000092 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000093 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000094 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000095 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000096 return true;
97 }
98}
99
100/// getFunctionOrMethodNumArgs - Return number of function or method
101/// arguments. It is an error to call this on a K&R function (use
102/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +0000103static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +0000104 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000105 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000106 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
107 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +0000108 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000109}
110
Ted Kremenek527042b2009-08-14 20:49:40 +0000111static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000112 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000113 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000114 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
115 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000116
Chris Lattnera4997152009-02-20 18:43:26 +0000117 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000118}
119
Ted Kremenek527042b2009-08-14 20:49:40 +0000120static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000121 if (const FunctionType *FnTy = getFunctionType(d))
122 return cast<FunctionProtoType>(FnTy)->getResultType();
123 return cast<ObjCMethodDecl>(d)->getResultType();
124}
125
Ted Kremenek527042b2009-08-14 20:49:40 +0000126static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000127 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000128 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000129 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000130 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000131 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000132 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000133 return cast<ObjCMethodDecl>(d)->isVariadic();
134 }
135}
136
Chandler Carruth743682b2010-11-16 08:35:43 +0000137static bool isInstanceMethod(const Decl *d) {
138 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d))
139 return MethodDecl->isInstance();
140 return false;
141}
142
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000144 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000145 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000146 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000147
John McCall96fa4842010-05-17 21:00:27 +0000148 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
149 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
John McCall96fa4842010-05-17 21:00:27 +0000152 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000153
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000154 // FIXME: Should we walk the chain of classes?
155 return ClsName == &Ctx.Idents.get("NSString") ||
156 ClsName == &Ctx.Idents.get("NSMutableString");
157}
158
Daniel Dunbar980c6692008-09-26 03:32:58 +0000159static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000160 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 if (!PT)
162 return false;
163
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000164 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165 if (!RT)
166 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
Daniel Dunbar980c6692008-09-26 03:32:58 +0000168 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000169 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000170 return false;
171
172 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
173}
174
Chris Lattner58418ff2008-06-29 00:16:31 +0000175//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000176// Attribute Implementations
177//===----------------------------------------------------------------------===//
178
Daniel Dunbar032db472008-07-31 22:40:48 +0000179// FIXME: All this manual attribute parsing code is gross. At the
180// least add some helper functions to check most argument patterns (#
181// and types of args).
182
Mike Stumpd3bb5572009-07-24 19:02:52 +0000183static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000184 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000185 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
186 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000187 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000188 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000189 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000190
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000191 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000192
193 Expr *sizeExpr;
194
195 // Special case where the argument is a template id.
196 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000197 CXXScopeSpec SS;
198 UnqualifiedId id;
199 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
200 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000201 } else {
202 // check the attribute arguments.
203 if (Attr.getNumArgs() != 1) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
205 return;
206 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000207 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000208 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000209
210 // Instantiate/Install the vector type, and let Sema build the type for us.
211 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000212 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000213 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000214 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000215 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000216
Douglas Gregor758a8692009-06-17 21:51:59 +0000217 // Remember this typedef decl, we will need it later for diagnostics.
218 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000219 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000220}
221
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000222static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000223 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000224 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000226 return;
227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000228
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000229 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000230 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000231 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
232 // If the alignment is less than or equal to 8 bits, the packed attribute
233 // has no effect.
234 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000235 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000236 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000237 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000238 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000239 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000240 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000241 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000242}
243
Ted Kremenek1f672822010-02-18 03:08:58 +0000244static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000245 // check the attribute arguments.
246 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000248 return;
249 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000250
Ted Kremenek1f672822010-02-18 03:08:58 +0000251 // The IBAction attributes only apply to instance methods.
252 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
253 if (MD->isInstanceMethod()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000254 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000255 return;
256 }
257
Ted Kremenekd68ec812011-02-04 06:54:16 +0000258 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000259}
260
261static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
262 // check the attribute arguments.
263 if (Attr.getNumArgs() > 0) {
264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
265 return;
266 }
267
268 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000269 // Objective-C classes.
270 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000271 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000272 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000273 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000274
Ted Kremenekd68ec812011-02-04 06:54:16 +0000275 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000276}
277
Ted Kremenek26bde772010-05-19 17:38:06 +0000278static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
279 Sema &S) {
280
281 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000282 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
284 return;
285 }
286
287 // The IBOutletCollection attributes only apply to instance variables of
288 // Objective-C classes.
289 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
Ted Kremenekd68ec812011-02-04 06:54:16 +0000290 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek26bde772010-05-19 17:38:06 +0000291 return;
292 }
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000293 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
294 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
295 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
296 << VD->getType() << 0;
297 return;
298 }
299 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
300 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
301 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
302 << PD->getType() << 1;
303 return;
304 }
305
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000306 IdentifierInfo *II = Attr.getParameterName();
307 if (!II)
308 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000309
John McCallba7bf592010-08-24 05:47:05 +0000310 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000311 S.getScopeForContext(d->getDeclContext()->getParent()));
312 if (!TypeRep) {
313 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
314 return;
315 }
John McCallba7bf592010-08-24 05:47:05 +0000316 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000317 // Diagnose use of non-object type in iboutletcollection attribute.
318 // FIXME. Gnu attribute extension ignores use of builtin types in
319 // attributes. So, __attribute__((iboutletcollection(char))) will be
320 // treated as __attribute__((iboutletcollection())).
321 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
322 !QT->isObjCObjectType()) {
323 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
324 return;
325 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000326 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
327 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000328}
329
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000330static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000331 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
332 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000333 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000334 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000335 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000336 return;
337 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000338
Chandler Carruth743682b2010-11-16 08:35:43 +0000339 // In C++ the implicit 'this' function parameter also counts, and they are
340 // counted from one.
341 bool HasImplicitThisParam = isInstanceMethod(d);
342 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000343
344 // The nonnull attribute only applies to pointers.
345 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000346
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000347 for (AttributeList::arg_iterator I=Attr.arg_begin(),
348 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000349
350
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000351 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000352 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000353 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000354 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
355 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000356 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
357 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000358 return;
359 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000360
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000361 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000362
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000363 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000364 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000365 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000366 return;
367 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000368
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000369 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000370 if (HasImplicitThisParam) {
371 if (x == 0) {
372 S.Diag(Attr.getLoc(),
373 diag::err_attribute_invalid_implicit_this_argument)
374 << "nonnull" << Ex->getSourceRange();
375 return;
376 }
377 --x;
378 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000379
380 // Is the function argument a pointer type?
Douglas Gregor1e2cdf52010-12-15 15:41:46 +0000381 QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType();
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000382 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000383 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000384 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000385 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000386 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000388
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000389 NonNullArgs.push_back(x);
390 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000391
392 // If no arguments were specified to __attribute__((nonnull)) then all pointer
393 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000394 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000395 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
Douglas Gregor1e2cdf52010-12-15 15:41:46 +0000396 QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType();
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000397 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000398 NonNullArgs.push_back(I);
Fariborz Jahanian3567c422010-09-27 22:42:37 +0000399 else if (const RecordType *UT = T->getAsUnionType()) {
400 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
401 RecordDecl *UD = UT->getDecl();
402 for (RecordDecl::field_iterator it = UD->field_begin(),
403 itend = UD->field_end(); it != itend; ++it) {
404 T = it->getType();
405 if (T->isAnyPointerType() || T->isBlockPointerType()) {
406 NonNullArgs.push_back(I);
407 break;
408 }
409 }
410 }
411 }
Ted Kremenek5fa50522008-11-18 06:52:58 +0000412 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000413
Ted Kremenek22813f42010-10-21 18:49:36 +0000414 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000415 if (NonNullArgs.empty()) {
416 // Warn the trivial case only if attribute is not coming from a
417 // macro instantiation.
418 if (Attr.getLoc().isFileID())
419 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000420 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000421 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000422 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000423
424 unsigned* start = &NonNullArgs[0];
425 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000426 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000427 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
428 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000429}
430
Ted Kremenekd21139a2010-07-31 01:52:11 +0000431static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
432 // This attribute must be applied to a function declaration.
433 // The first argument to the attribute must be a string,
434 // the name of the resource, for example "malloc".
435 // The following arguments must be argument indexes, the arguments must be
436 // of integer type for Returns, otherwise of pointer type.
437 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000438 // after being held. free() should be __attribute((ownership_takes)), whereas
439 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000440
441 if (!AL.getParameterName()) {
442 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
443 << AL.getName()->getName() << 1;
444 return;
445 }
446 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000447 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000448 switch (AL.getKind()) {
449 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000450 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000451 if (AL.getNumArgs() < 1) {
452 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
453 return;
454 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000455 break;
456 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000457 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000458 if (AL.getNumArgs() < 1) {
459 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
460 return;
461 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000462 break;
463 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000464 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000465 if (AL.getNumArgs() > 1) {
466 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
467 << AL.getNumArgs() + 1;
468 return;
469 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000470 break;
471 default:
472 // This should never happen given how we are called.
473 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000474 }
475
476 if (!isFunction(d) || !hasFunctionProto(d)) {
477 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
478 << 0 /*function*/;
479 return;
480 }
481
Chandler Carruth743682b2010-11-16 08:35:43 +0000482 // In C++ the implicit 'this' function parameter also counts, and they are
483 // counted from one.
484 bool HasImplicitThisParam = isInstanceMethod(d);
485 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000486
487 llvm::StringRef Module = AL.getParameterName()->getName();
488
489 // Normalize the argument, __foo__ becomes foo.
490 if (Module.startswith("__") && Module.endswith("__"))
491 Module = Module.substr(2, Module.size() - 4);
492
493 llvm::SmallVector<unsigned, 10> OwnershipArgs;
494
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000495 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
496 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000497
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000498 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000499 llvm::APSInt ArgNum(32);
500 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
501 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
502 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
503 << AL.getName()->getName() << IdxExpr->getSourceRange();
504 continue;
505 }
506
507 unsigned x = (unsigned) ArgNum.getZExtValue();
508
509 if (x > NumArgs || x < 1) {
510 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
511 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
512 continue;
513 }
514 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000515 if (HasImplicitThisParam) {
516 if (x == 0) {
517 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
518 << "ownership" << IdxExpr->getSourceRange();
519 return;
520 }
521 --x;
522 }
523
Ted Kremenekd21139a2010-07-31 01:52:11 +0000524 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000525 case OwnershipAttr::Takes:
526 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000527 // Is the function argument a pointer type?
528 QualType T = getFunctionOrMethodArgType(d, x);
529 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
530 // FIXME: Should also highlight argument in decl.
531 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000532 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000533 << "pointer"
534 << IdxExpr->getSourceRange();
535 continue;
536 }
537 break;
538 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000539 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000540 if (AL.getNumArgs() > 1) {
541 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000542 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000543 llvm::APSInt ArgNum(32);
544 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
545 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
546 S.Diag(AL.getLoc(), diag::err_ownership_type)
547 << "ownership_returns" << "integer"
548 << IdxExpr->getSourceRange();
549 return;
550 }
551 }
552 break;
553 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000554 default:
555 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000556 } // switch
557
558 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000559 for (specific_attr_iterator<OwnershipAttr>
560 i = d->specific_attr_begin<OwnershipAttr>(),
561 e = d->specific_attr_end<OwnershipAttr>();
562 i != e; ++i) {
563 if ((*i)->getOwnKind() != K) {
564 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
565 I!=E; ++I) {
566 if (x == *I) {
567 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
568 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000569 }
570 }
571 }
572 }
573 OwnershipArgs.push_back(x);
574 }
575
576 unsigned* start = OwnershipArgs.data();
577 unsigned size = OwnershipArgs.size();
578 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000579
580 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
581 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
582 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000583 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000584
585 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
586 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000587}
588
John McCall7a198ce2011-02-08 22:35:49 +0000589/// Whether this declaration has internal linkage for the purposes of
590/// things that want to complain about things not have internal linkage.
591static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
592 switch (D->getLinkage()) {
593 case NoLinkage:
594 case InternalLinkage:
595 return true;
596
597 // Template instantiations that go from external to unique-external
598 // shouldn't get diagnosed.
599 case UniqueExternalLinkage:
600 return true;
601
602 case ExternalLinkage:
603 return false;
604 }
605 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +0000606 return false;
607}
608
609static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
610 // Check the attribute arguments.
611 if (Attr.getNumArgs() > 1) {
612 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
613 return;
614 }
615
John McCall7a198ce2011-02-08 22:35:49 +0000616 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
617 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
618 << Attr.getName() << 2 /*variables and functions*/;
619 return;
620 }
621
622 NamedDecl *nd = cast<NamedDecl>(d);
623
Rafael Espindolac18086a2010-02-23 22:00:30 +0000624 // gcc rejects
625 // class c {
626 // static int a __attribute__((weakref ("v2")));
627 // static int b() __attribute__((weakref ("f3")));
628 // };
629 // and ignores the attributes of
630 // void f(void) {
631 // static int a __attribute__((weakref ("v2")));
632 // }
633 // we reject them
Sebastian Redl50c68252010-08-31 00:36:30 +0000634 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
635 if (!Ctx->isFileContext()) {
636 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +0000637 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +0000638 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000639 }
640
641 // The GCC manual says
642 //
643 // At present, a declaration to which `weakref' is attached can only
644 // be `static'.
645 //
646 // It also says
647 //
648 // Without a TARGET,
649 // given as an argument to `weakref' or to `alias', `weakref' is
650 // equivalent to `weak'.
651 //
652 // gcc 4.4.1 will accept
653 // int a7 __attribute__((weakref));
654 // as
655 // int a7 __attribute__((weak));
656 // This looks like a bug in gcc. We reject that for now. We should revisit
657 // it if this behaviour is actually used.
658
John McCall7a198ce2011-02-08 22:35:49 +0000659 if (!hasEffectivelyInternalLinkage(nd)) {
660 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000661 return;
662 }
663
664 // GCC rejects
665 // static ((alias ("y"), weakref)).
666 // Should we? How to check that weakref is before or after alias?
667
668 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000669 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000670 Arg = Arg->IgnoreParenCasts();
671 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
672
673 if (Str == 0 || Str->isWide()) {
674 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
675 << "weakref" << 1;
676 return;
677 }
678 // GCC will accept anything as the argument of weakref. Should we
679 // check for an existing decl?
Eric Christopherbc638a82010-12-01 22:13:54 +0000680 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
681 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000682 }
683
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000684 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000685}
686
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000687static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000688 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000689 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000691 return;
692 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000693
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000694 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000695 Arg = Arg->IgnoreParenCasts();
696 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000697
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000698 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000699 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000700 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000701 return;
702 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000703
Rafael Espindola0017c5f2010-12-07 15:23:23 +0000704 if (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin) {
705 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
706 return;
707 }
708
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000709 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000710
Eric Christopherbc638a82010-12-01 22:13:54 +0000711 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
712 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000713}
714
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000715static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000716 Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000717 // Check the attribute arguments.
Daniel Dunbar03a38442008-10-28 00:17:57 +0000718 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000720 return;
721 }
Anders Carlsson88097122009-02-19 19:16:48 +0000722
Chris Lattner4225e232009-04-14 17:02:11 +0000723 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000725 << Attr.getName() << 0 /*function*/;
726 return;
727 }
728
729 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
730}
731
732static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
733 Sema &S) {
734 // Check the attribute arguments.
735 if (Attr.getNumArgs() != 0) {
736 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
737 return;
738 }
739
740 if (!isa<FunctionDecl>(d)) {
741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
742 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000743 return;
744 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000745
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000746 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000747}
748
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000749static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000750 // Check the attribute arguments.
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000751 if (Attr.getNumArgs() != 0) {
752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
753 return;
754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Ted Kremenek08479ae2009-08-15 00:51:46 +0000756 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000757 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000758 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000759 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000760 return;
761 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000762 }
763
Ted Kremenek08479ae2009-08-15 00:51:46 +0000764 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000765}
766
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000767static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
768 // check the attribute arguments.
769 if (Attr.getNumArgs() != 0) {
770 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
771 return;
772 }
773
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000774 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
775}
776
Eric Christopher8a2ee392010-12-02 02:45:55 +0000777static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
778 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000779 if (isa<VarDecl>(d))
780 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
781 else
782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
783 << Attr.getName() << 12 /* variable */;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000784}
785
786static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
787 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000788 if (isa<VarDecl>(d))
789 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
790 else
791 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
792 << Attr.getName() << 12 /* variable */;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000793}
794
John McCall3882ace2011-01-05 12:14:39 +0000795static void HandleNoReturnAttr(Decl *d, const AttributeList &attr, Sema &S) {
796 if (hasDeclarator(d)) return;
797
798 if (S.CheckNoReturnAttr(attr)) return;
799
800 if (!isa<ObjCMethodDecl>(d)) {
801 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
802 << attr.getName() << 0 /*function*/;
803 return;
804 }
805
806 d->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
807}
808
809bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
810 if (attr.getNumArgs() != 0) {
811 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
812 attr.setInvalid();
813 return true;
814 }
815
816 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000817}
818
819static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
820 Sema &S) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000821
822 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
823 // because 'analyzer_noreturn' does not impact the type.
824
825 if (Attr.getNumArgs() != 0) {
826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
827 return;
828 }
829
830 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
831 ValueDecl *VD = dyn_cast<ValueDecl>(d);
832 if (VD == 0 || (!VD->getType()->isBlockPointerType()
833 && !VD->getType()->isFunctionPointerType())) {
834 S.Diag(Attr.getLoc(),
835 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
836 : diag::warn_attribute_wrong_decl_type)
837 << Attr.getName() << 0 /*function*/;
838 return;
839 }
840 }
841
842 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000843}
844
John Thompsoncdb847ba2010-08-09 21:53:52 +0000845// PS3 PPU-specific.
846static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
847 Sema &S) {
848/*
849 Returning a Vector Class in Registers
850
Eric Christopherbc638a82010-12-01 22:13:54 +0000851 According to the PPU ABI specifications, a class with a single member of
852 vector type is returned in memory when used as the return value of a function.
853 This results in inefficient code when implementing vector classes. To return
854 the value in a single vector register, add the vecreturn attribute to the
855 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +0000856
857 Example:
858
859 struct Vector
860 {
861 __vector float xyzw;
862 } __attribute__((vecreturn));
863
864 Vector Add(Vector lhs, Vector rhs)
865 {
866 Vector result;
867 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
868 return result; // This will be returned in a register
869 }
870*/
John Thompson9a587aaa2010-09-18 01:12:07 +0000871 if (!isa<RecordDecl>(d)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000872 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
873 << Attr.getName() << 9 /*class*/;
874 return;
875 }
876
877 if (d->getAttr<VecReturnAttr>()) {
878 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
879 return;
880 }
881
John Thompson9a587aaa2010-09-18 01:12:07 +0000882 RecordDecl *record = cast<RecordDecl>(d);
883 int count = 0;
884
885 if (!isa<CXXRecordDecl>(record)) {
886 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
887 return;
888 }
889
890 if (!cast<CXXRecordDecl>(record)->isPOD()) {
891 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
892 return;
893 }
894
Eric Christopherbc638a82010-12-01 22:13:54 +0000895 for (RecordDecl::field_iterator iter = record->field_begin();
896 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +0000897 if ((count == 1) || !iter->getType()->isVectorType()) {
898 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
899 return;
900 }
901 count++;
902 }
903
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000904 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000905}
906
Alexis Hunt96d5c762009-11-21 08:43:09 +0000907static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
908 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000910 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000911 return;
912 }
913 // FIXME: Actually store the attribute on the declaration
914}
915
Ted Kremenek39c59a82008-07-25 04:39:19 +0000916static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
917 // check the attribute arguments.
918 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000919 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000920 return;
921 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000922
John McCallcef15822010-03-31 02:47:45 +0000923 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
Chris Lattnercab02a62011-02-17 20:34:02 +0000924 !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000925 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnercab02a62011-02-17 20:34:02 +0000926 << Attr.getName() << 14 /*variable, function, labels*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000927 return;
928 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000929
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000930 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000931}
932
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000933static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
934 // check the attribute arguments.
935 if (Attr.getNumArgs() != 0) {
936 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
937 return;
938 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000939
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000940 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000941 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000942 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
943 return;
944 }
945 } else if (!isFunctionOrMethod(d)) {
946 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000947 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000948 return;
949 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000950
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000951 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000952}
953
Daniel Dunbar032db472008-07-31 22:40:48 +0000954static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
955 // check the attribute arguments.
956 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000957 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
958 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000959 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000960 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000961
962 int priority = 65535; // FIXME: Do not hardcode such constants.
963 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000964 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000965 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000966 if (E->isTypeDependent() || E->isValueDependent() ||
967 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000968 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000969 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000970 return;
971 }
972 priority = Idx.getZExtValue();
973 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000974
Chris Lattner4225e232009-04-14 17:02:11 +0000975 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000976 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000977 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000978 return;
979 }
980
Eric Christopherbc638a82010-12-01 22:13:54 +0000981 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
982 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000983}
984
985static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
986 // check the attribute arguments.
987 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000988 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
989 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000990 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000991 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000992
993 int priority = 65535; // FIXME: Do not hardcode such constants.
994 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000995 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000996 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000997 if (E->isTypeDependent() || E->isValueDependent() ||
998 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000999 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001000 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001001 return;
1002 }
1003 priority = Idx.getZExtValue();
1004 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001005
Anders Carlsson8e82b7f2008-08-22 22:10:48 +00001006 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001008 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +00001009 return;
1010 }
1011
Eric Christopherbc638a82010-12-01 22:13:54 +00001012 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
1013 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001014}
1015
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001016static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001017 // check the attribute arguments.
Fariborz Jahanian551063102010-10-06 21:18:44 +00001018 int noArgs = Attr.getNumArgs();
1019 if (noArgs > 1) {
1020 S.Diag(Attr.getLoc(),
1021 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001022 return;
1023 }
Fariborz Jahanian551063102010-10-06 21:18:44 +00001024 // Handle the case where deprecated attribute has a text message.
1025 StringLiteral *SE;
1026 if (noArgs == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001027 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanian551063102010-10-06 21:18:44 +00001028 SE = dyn_cast<StringLiteral>(ArgExpr);
1029 if (!SE) {
1030 S.Diag(ArgExpr->getLocStart(),
1031 diag::err_attribute_not_string) << "deprecated";
1032 return;
1033 }
1034 }
1035 else
1036 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001037
Fariborz Jahanian551063102010-10-06 21:18:44 +00001038 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
1039 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001040}
1041
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001042static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1043 // check the attribute arguments.
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001044 int noArgs = Attr.getNumArgs();
1045 if (noArgs > 1) {
Eric Christopherbc638a82010-12-01 22:13:54 +00001046 S.Diag(Attr.getLoc(),
1047 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001048 return;
1049 }
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001050 // Handle the case where unavailable attribute has a text message.
1051 StringLiteral *SE;
1052 if (noArgs == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001053 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001054 SE = dyn_cast<StringLiteral>(ArgExpr);
1055 if (!SE) {
1056 S.Diag(ArgExpr->getLocStart(),
1057 diag::err_attribute_not_string) << "unavailable";
1058 return;
1059 }
1060 }
1061 else
1062 SE = StringLiteral::CreateEmpty(S.Context, 1);
1063 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
1064 SE->getString()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001065}
1066
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001067static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001068 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001069 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001071 return;
1072 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001073
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001074 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001075 Arg = Arg->IgnoreParenCasts();
1076 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001077
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001078 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001079 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001080 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001081 return;
1082 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001083
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001084 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001085 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001086
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001087 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001088 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001089 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001090 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001091 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001092 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001093 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001094 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001095 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001096 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001097 return;
1098 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001099
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001100 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001101}
1102
Chris Lattner677a3582009-02-14 08:09:34 +00001103static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1104 Sema &S) {
1105 if (Attr.getNumArgs() != 0) {
1106 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1107 return;
1108 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001109
Chris Lattner677a3582009-02-14 08:09:34 +00001110 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1111 if (OCI == 0) {
1112 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1113 return;
1114 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001115
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001116 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001117}
1118
1119static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001120 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001121 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001122 return;
1123 }
Chris Lattner677a3582009-02-14 08:09:34 +00001124 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001125 QualType T = TD->getUnderlyingType();
1126 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001127 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001128 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1129 return;
1130 }
1131 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001132 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001133}
1134
Mike Stumpd3bb5572009-07-24 19:02:52 +00001135static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001136HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1137 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001138 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001139 return;
1140 }
1141
1142 if (!isa<FunctionDecl>(D)) {
1143 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1144 return;
1145 }
1146
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001147 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001148}
1149
Steve Naroff3405a732008-09-18 16:44:58 +00001150static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001151 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001153 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001154 return;
1155 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001156
Steve Naroff3405a732008-09-18 16:44:58 +00001157 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001158 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001159 return;
1160 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001161
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001162 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001163 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001164 type = BlocksAttr::ByRef;
1165 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001166 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001167 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001168 return;
1169 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001170
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001171 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001172}
1173
Anders Carlssonc181b012008-10-05 18:05:59 +00001174static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1175 // check the attribute arguments.
1176 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1178 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +00001179 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001180 }
1181
Anders Carlssonc181b012008-10-05 18:05:59 +00001182 int sentinel = 0;
1183 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001184 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001185 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001186 if (E->isTypeDependent() || E->isValueDependent() ||
1187 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001189 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001190 return;
1191 }
1192 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001193
Anders Carlssonc181b012008-10-05 18:05:59 +00001194 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001195 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1196 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001197 return;
1198 }
1199 }
1200
1201 int nullPos = 0;
1202 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001203 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001204 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001205 if (E->isTypeDependent() || E->isValueDependent() ||
1206 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001208 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001209 return;
1210 }
1211 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001212
Anders Carlssonc181b012008-10-05 18:05:59 +00001213 if (nullPos > 1 || nullPos < 0) {
1214 // FIXME: This error message could be improved, it would be nice
1215 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001216 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1217 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001218 return;
1219 }
1220 }
1221
1222 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001223 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001224 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001225
Chris Lattner9363e312009-03-17 23:03:47 +00001226 if (isa<FunctionNoProtoType>(FT)) {
1227 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1228 return;
1229 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001230
Chris Lattner9363e312009-03-17 23:03:47 +00001231 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001232 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001233 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001234 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001235 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1236 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001237 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001238 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001239 }
1240 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001241 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1242 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001243 ;
1244 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001245 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001246 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001247 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherbc638a82010-12-01 22:13:54 +00001248 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001249 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001250 int m = Ty->isFunctionPointerType() ? 0 : 1;
1251 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001252 return;
1253 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001254 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001255 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001256 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001257 return;
1258 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001259 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001260 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001261 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +00001262 return;
1263 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001264 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1265 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001266}
1267
Chris Lattner237f2752009-02-14 07:37:35 +00001268static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1269 // check the attribute arguments.
1270 if (Attr.getNumArgs() != 0) {
1271 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1272 return;
1273 }
1274
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001275 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001276 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001277 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +00001278 return;
1279 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001280
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001281 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1282 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1283 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001284 return;
1285 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001286 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1287 if (MD->getResultType()->isVoidType()) {
1288 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1289 << Attr.getName() << 1;
1290 return;
1291 }
1292
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001293 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001294}
1295
John McCall7a198ce2011-02-08 22:35:49 +00001296static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001297 // check the attribute arguments.
John McCall7a198ce2011-02-08 22:35:49 +00001298 if (attr.getNumArgs() != 0) {
1299 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001300 return;
1301 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001302
John McCall7a198ce2011-02-08 22:35:49 +00001303 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
1304 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1305 << attr.getName() << 2 /*variables and functions*/;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001306 return;
1307 }
1308
John McCall7a198ce2011-02-08 22:35:49 +00001309 NamedDecl *nd = cast<NamedDecl>(d);
1310
1311 // 'weak' only applies to declarations with external linkage.
1312 if (hasEffectivelyInternalLinkage(nd)) {
1313 S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001314 return;
1315 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001316
John McCall7a198ce2011-02-08 22:35:49 +00001317 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001318}
1319
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001320static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1321 // check the attribute arguments.
1322 if (Attr.getNumArgs() != 0) {
1323 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1324 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001325 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001326
1327 // weak_import only applies to variable & function declarations.
1328 bool isDef = false;
1329 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1330 isDef = (!VD->hasExternalStorage() || VD->getInit());
1331 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001332 isDef = FD->hasBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +00001333 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1334 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +00001335 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +00001336 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001337 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001338 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001339 !isa<ObjCInterfaceDecl>(D))
1340 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001341 << Attr.getName() << 2 /*variable and function*/;
1342 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001343 }
1344
1345 // Merge should handle any subsequent violations.
1346 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001347 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001348 diag::warn_attribute_weak_import_invalid_on_definition)
1349 << "weak_import" << 2 /*variable and function*/;
1350 return;
1351 }
1352
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001353 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001354}
1355
Nate Begemanf2758702009-06-26 06:32:41 +00001356static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1357 Sema &S) {
1358 // Attribute has 3 arguments.
1359 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001361 return;
1362 }
1363
1364 unsigned WGSize[3];
1365 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001366 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001367 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001368 if (E->isTypeDependent() || E->isValueDependent() ||
1369 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001370 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1371 << "reqd_work_group_size" << E->getSourceRange();
1372 return;
1373 }
1374 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1375 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001376 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1377 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001378 WGSize[2]));
1379}
1380
Chris Lattner237f2752009-02-14 07:37:35 +00001381static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001382 // Attribute has no arguments.
1383 if (Attr.getNumArgs() != 1) {
1384 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1385 return;
1386 }
1387
1388 // Make sure that there is a string literal as the sections's single
1389 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001390 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001391 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001392 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001393 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001394 return;
1395 }
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattner30ba6742009-08-10 19:03:04 +00001397 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001398 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001399 if (!Error.empty()) {
1400 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1401 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001402 return;
1403 }
Mike Stump11289f42009-09-09 15:08:12 +00001404
Chris Lattner20aee9b2010-01-12 20:58:53 +00001405 // This attribute cannot be applied to local variables.
1406 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1407 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1408 return;
1409 }
1410
Eric Christopherbc638a82010-12-01 22:13:54 +00001411 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1412 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001413}
1414
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001415
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001416static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001417 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001418 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001419 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001420 return;
1421 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001422
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001423 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001424}
1425
Anders Carlssonb8316282008-10-05 23:32:53 +00001426static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1427 // check the attribute arguments.
1428 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001430 return;
1431 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001432
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001433 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001434}
1435
1436static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1437 // check the attribute arguments.
1438 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001440 return;
1441 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001442
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001443 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001444}
1445
Anders Carlssond277d792009-01-31 01:16:18 +00001446static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001447 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1449 return;
1450 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001451
Anders Carlssond277d792009-01-31 01:16:18 +00001452 if (Attr.getNumArgs() != 0) {
1453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1454 return;
1455 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001456
Anders Carlssond277d792009-01-31 01:16:18 +00001457 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001458
Anders Carlssond277d792009-01-31 01:16:18 +00001459 if (!VD || !VD->hasLocalStorage()) {
1460 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1461 return;
1462 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001463
Anders Carlssond277d792009-01-31 01:16:18 +00001464 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001465 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001466 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001467 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1468 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001469 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001470 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001471 Attr.getParameterName();
1472 return;
1473 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001474
Anders Carlssond277d792009-01-31 01:16:18 +00001475 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1476 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001477 S.Diag(Attr.getParameterLoc(),
1478 diag::err_attribute_cleanup_arg_not_function)
1479 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001480 return;
1481 }
1482
Anders Carlssond277d792009-01-31 01:16:18 +00001483 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001484 S.Diag(Attr.getParameterLoc(),
1485 diag::err_attribute_cleanup_func_must_take_one_arg)
1486 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001487 return;
1488 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001489
Anders Carlsson723f55d2009-02-07 23:16:50 +00001490 // We're currently more strict than GCC about what function types we accept.
1491 // If this ever proves to be a problem it should be easy to fix.
1492 QualType Ty = S.Context.getPointerType(VD->getType());
1493 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00001494 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1495 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001496 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001497 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1498 Attr.getParameterName() << ParamTy << Ty;
1499 return;
1500 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001501
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001502 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001503 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001504}
1505
Mike Stumpd3bb5572009-07-24 19:02:52 +00001506/// Handle __attribute__((format_arg((idx)))) attribute based on
1507/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1508static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001509 if (Attr.getNumArgs() != 1) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1511 return;
1512 }
1513 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1514 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1515 << Attr.getName() << 0 /*function*/;
1516 return;
1517 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001518
1519 // In C++ the implicit 'this' function parameter also counts, and they are
1520 // counted from one.
1521 bool HasImplicitThisParam = isInstanceMethod(d);
1522 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001523 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001524
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001525 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001526 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001527 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001528 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1529 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001530 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1531 << "format" << 2 << IdxExpr->getSourceRange();
1532 return;
1533 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001534
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001535 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1536 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1537 << "format" << 2 << IdxExpr->getSourceRange();
1538 return;
1539 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001540
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001541 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001542
Chandler Carruth743682b2010-11-16 08:35:43 +00001543 if (HasImplicitThisParam) {
1544 if (ArgIdx == 0) {
1545 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1546 << "format_arg" << IdxExpr->getSourceRange();
1547 return;
1548 }
1549 ArgIdx--;
1550 }
1551
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001552 // make sure the format string is really a string
1553 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001554
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001555 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1556 if (not_nsstring_type &&
1557 !isCFStringType(Ty, S.Context) &&
1558 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001559 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001560 // FIXME: Should highlight the actual expression that has the wrong type.
1561 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001562 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001563 << IdxExpr->getSourceRange();
1564 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001565 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001566 Ty = getFunctionOrMethodResultType(d);
1567 if (!isNSStringType(Ty, S.Context) &&
1568 !isCFStringType(Ty, S.Context) &&
1569 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001570 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001571 // FIXME: Should highlight the actual expression that has the wrong type.
1572 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001573 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001574 << IdxExpr->getSourceRange();
1575 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001576 }
1577
Chandler Carruth743682b2010-11-16 08:35:43 +00001578 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1579 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001580}
1581
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001582enum FormatAttrKind {
1583 CFStringFormat,
1584 NSStringFormat,
1585 StrftimeFormat,
1586 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001587 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001588 InvalidFormat
1589};
1590
1591/// getFormatAttrKind - Map from format attribute names to supported format
1592/// types.
1593static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1594 // Check for formats that get handled specially.
1595 if (Format == "NSString")
1596 return NSStringFormat;
1597 if (Format == "CFString")
1598 return CFStringFormat;
1599 if (Format == "strftime")
1600 return StrftimeFormat;
1601
1602 // Otherwise, check for supported formats.
1603 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1604 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1605 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00001606 Format == "zcmn_err" ||
1607 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001608 return SupportedFormat;
1609
Duncan Sandsde4fe352010-03-23 14:44:19 +00001610 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1611 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001612 return IgnoredFormat;
1613
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001614 return InvalidFormat;
1615}
1616
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001617/// Handle __attribute__((init_priority(priority))) attributes based on
1618/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1619static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1620 Sema &S) {
1621 if (!S.getLangOptions().CPlusPlus) {
1622 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1623 return;
1624 }
1625
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001626 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1627 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1628 Attr.setInvalid();
1629 return;
1630 }
1631 QualType T = dyn_cast<VarDecl>(d)->getType();
1632 if (S.Context.getAsArrayType(T))
1633 T = S.Context.getBaseElementType(T);
1634 if (!T->getAs<RecordType>()) {
1635 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1636 Attr.setInvalid();
1637 return;
1638 }
1639
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001640 if (Attr.getNumArgs() != 1) {
1641 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1642 Attr.setInvalid();
1643 return;
1644 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001645 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001646
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001647 llvm::APSInt priority(32);
1648 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1649 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1650 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1651 << "init_priority" << priorityExpr->getSourceRange();
1652 Attr.setInvalid();
1653 return;
1654 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001655 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001656 if (prioritynum < 101 || prioritynum > 65535) {
1657 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1658 << priorityExpr->getSourceRange();
1659 Attr.setInvalid();
1660 return;
1661 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001662 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1663 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001664}
1665
Mike Stumpd3bb5572009-07-24 19:02:52 +00001666/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1667/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001668static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001669
Chris Lattner4a927cb2008-06-28 23:36:30 +00001670 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001671 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001672 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001673 return;
1674 }
1675
Chris Lattner4a927cb2008-06-28 23:36:30 +00001676 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001678 return;
1679 }
1680
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001681 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001682 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001683 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001684 return;
1685 }
1686
Chandler Carruth743682b2010-11-16 08:35:43 +00001687 // In C++ the implicit 'this' function parameter also counts, and they are
1688 // counted from one.
1689 bool HasImplicitThisParam = isInstanceMethod(d);
1690 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001691 unsigned FirstIdx = 1;
1692
Daniel Dunbar07d07852009-10-18 21:17:35 +00001693 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001694
1695 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001696 if (Format.startswith("__") && Format.endswith("__"))
1697 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001698
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001699 // Check for supported formats.
1700 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001701
1702 if (Kind == IgnoredFormat)
1703 return;
1704
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001705 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001706 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001707 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001708 return;
1709 }
1710
1711 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001712 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001713 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001714 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1715 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001716 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001717 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001718 return;
1719 }
1720
1721 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001722 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001723 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001724 return;
1725 }
1726
1727 // FIXME: Do we need to bounds check?
1728 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001729
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001730 if (HasImplicitThisParam) {
1731 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001732 S.Diag(Attr.getLoc(),
1733 diag::err_format_attribute_implicit_this_format_string)
1734 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001735 return;
1736 }
1737 ArgIdx--;
1738 }
Mike Stump11289f42009-09-09 15:08:12 +00001739
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001740 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001741 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001742
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001743 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001744 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001745 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1746 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001747 return;
1748 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001749 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001750 // FIXME: do we need to check if the type is NSString*? What are the
1751 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001752 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001753 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001754 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1755 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001756 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001757 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001758 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001759 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001760 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001761 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1762 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001763 return;
1764 }
1765
1766 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001767 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001768 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001769 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1770 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001771 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001772 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001773 return;
1774 }
1775
1776 // check if the function is variadic if the 3rd argument non-zero
1777 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001778 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001779 ++NumArgs; // +1 for ...
1780 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001781 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001782 return;
1783 }
1784 }
1785
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001786 // strftime requires FirstArg to be 0 because it doesn't read from any
1787 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001788 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001789 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001790 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1791 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001792 return;
1793 }
1794 // if 0 it disables parameter checking (to use with e.g. va_list)
1795 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001796 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001797 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001798 return;
1799 }
1800
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001801 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1802 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001803 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001804}
1805
Chris Lattnera663a0a2008-06-29 00:28:59 +00001806static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1807 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001808 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001809 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001810 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001811 return;
1812 }
1813
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001814 // Try to find the underlying union declaration.
1815 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001816 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001817 if (TD && TD->getUnderlyingType()->isUnionType())
1818 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1819 else
1820 RD = dyn_cast<RecordDecl>(d);
1821
1822 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001823 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001824 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001825 return;
1826 }
1827
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001828 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001829 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001830 diag::warn_transparent_union_attribute_not_definition);
1831 return;
1832 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001833
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001834 RecordDecl::field_iterator Field = RD->field_begin(),
1835 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001836 if (Field == FieldEnd) {
1837 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1838 return;
1839 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001840
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001841 FieldDecl *FirstField = *Field;
1842 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001843 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001844 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001845 diag::warn_transparent_union_attribute_floating)
1846 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001847 return;
1848 }
1849
1850 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1851 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1852 for (; Field != FieldEnd; ++Field) {
1853 QualType FieldType = Field->getType();
1854 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1855 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1856 // Warn if we drop the attribute.
1857 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001858 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001859 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001860 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001861 diag::warn_transparent_union_attribute_field_size_align)
1862 << isSize << Field->getDeclName() << FieldBits;
1863 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001864 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001865 diag::note_transparent_union_first_field_size_align)
1866 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001867 return;
1868 }
1869 }
1870
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001871 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001872}
1873
Chris Lattnera663a0a2008-06-29 00:28:59 +00001874static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001875 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001876 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001877 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001878 return;
1879 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001880 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001881 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001882
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001883 // Make sure that there is a string literal as the annotation's single
1884 // argument.
1885 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001886 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001887 return;
1888 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001889 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1890 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001891}
1892
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001893static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001894 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001895 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001897 return;
1898 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001899
1900 //FIXME: The C++0x version of this attribute has more limited applicabilty
1901 // than GNU's, and should error out when it is used to specify a
1902 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001903
Chris Lattner4a927cb2008-06-28 23:36:30 +00001904 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001905 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001906 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001907 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001908
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001909 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001910}
1911
1912void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1913 if (E->isTypeDependent() || E->isValueDependent()) {
1914 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001915 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001916 return;
1917 }
1918
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001919 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00001920 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001921 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1922 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1923 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001924 return;
1925 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001926 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001927 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1928 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001929 return;
1930 }
1931
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001932 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1933}
1934
1935void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1936 // FIXME: Cache the number on the Attr object if non-dependent?
1937 // FIXME: Perform checking of type validity
1938 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1939 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001940}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001941
Mike Stumpd3bb5572009-07-24 19:02:52 +00001942/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1943/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001944///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001945/// Despite what would be logical, the mode attribute is a decl attribute, not a
1946/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1947/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001948static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001949 // This attribute isn't documented, but glibc uses it. It changes
1950 // the width of an int or unsigned int to the specified size.
1951
1952 // Check that there aren't any arguments
1953 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001954 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001955 return;
1956 }
1957
1958 IdentifierInfo *Name = Attr.getParameterName();
1959 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001960 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001961 return;
1962 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001963
Daniel Dunbar07d07852009-10-18 21:17:35 +00001964 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001965
1966 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001967 if (Str.startswith("__") && Str.endswith("__"))
1968 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001969
1970 unsigned DestWidth = 0;
1971 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001972 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001973 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001974 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001975 switch (Str[0]) {
1976 case 'Q': DestWidth = 8; break;
1977 case 'H': DestWidth = 16; break;
1978 case 'S': DestWidth = 32; break;
1979 case 'D': DestWidth = 64; break;
1980 case 'X': DestWidth = 96; break;
1981 case 'T': DestWidth = 128; break;
1982 }
1983 if (Str[1] == 'F') {
1984 IntegerMode = false;
1985 } else if (Str[1] == 'C') {
1986 IntegerMode = false;
1987 ComplexMode = true;
1988 } else if (Str[1] != 'I') {
1989 DestWidth = 0;
1990 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001991 break;
1992 case 4:
1993 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1994 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001995 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001996 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001997 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001998 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001999 break;
2000 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002001 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002002 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002003 break;
2004 }
2005
2006 QualType OldTy;
2007 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
2008 OldTy = TD->getUnderlyingType();
2009 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2010 OldTy = VD->getType();
2011 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002012 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2013 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00002014 return;
2015 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002016
John McCall9dd450b2009-09-21 23:43:11 +00002017 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002018 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2019 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002020 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002021 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2022 } else if (ComplexMode) {
2023 if (!OldTy->isComplexType())
2024 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2025 } else {
2026 if (!OldTy->isFloatingType())
2027 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2028 }
2029
Mike Stump87c57ac2009-05-16 07:39:55 +00002030 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2031 // and friends, at least with glibc.
2032 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2033 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002034 // FIXME: Make sure floating-point mappings are accurate
2035 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002036 QualType NewTy;
2037 switch (DestWidth) {
2038 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002039 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002040 return;
2041 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002042 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002043 return;
2044 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002045 if (!IntegerMode) {
2046 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2047 return;
2048 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002049 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002050 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002051 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002052 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002053 break;
2054 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002055 if (!IntegerMode) {
2056 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2057 return;
2058 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002059 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002060 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002061 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002062 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002063 break;
2064 case 32:
2065 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002066 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002067 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002068 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002069 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002070 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002071 break;
2072 case 64:
2073 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002074 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002075 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002076 if (S.Context.Target.getLongWidth() == 64)
2077 NewTy = S.Context.LongTy;
2078 else
2079 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002080 else
Chandler Carruth72343702010-01-26 06:39:24 +00002081 if (S.Context.Target.getLongWidth() == 64)
2082 NewTy = S.Context.UnsignedLongTy;
2083 else
2084 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002085 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002086 case 96:
2087 NewTy = S.Context.LongDoubleTy;
2088 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002089 case 128:
2090 if (!IntegerMode) {
2091 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2092 return;
2093 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002094 if (OldTy->isSignedIntegerType())
2095 NewTy = S.Context.Int128Ty;
2096 else
2097 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002098 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002099 }
2100
Eli Friedman4735374e2009-03-03 06:41:03 +00002101 if (ComplexMode) {
2102 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002103 }
2104
2105 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00002106 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2107 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002108 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002109 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002110 cast<ValueDecl>(D)->setType(NewTy);
2111}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002112
Mike Stump3722f582009-08-26 22:31:08 +00002113static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002114 // check the attribute arguments.
2115 if (Attr.getNumArgs() > 0) {
2116 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2117 return;
2118 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002119
Anders Carlsson88097122009-02-19 19:16:48 +00002120 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002121 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002122 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00002123 return;
2124 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002125
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002126 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002127}
2128
Mike Stump3722f582009-08-26 22:31:08 +00002129static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00002130 // check the attribute arguments.
2131 if (Attr.getNumArgs() != 0) {
2132 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2133 return;
2134 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002135
Chris Lattner4225e232009-04-14 17:02:11 +00002136 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002137 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002138 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00002139 return;
2140 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002141
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002142 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002143}
2144
Chris Lattner3c77a352010-06-22 00:03:40 +00002145static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2146 Sema &S) {
2147 // check the attribute arguments.
2148 if (Attr.getNumArgs() != 0) {
2149 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2150 return;
2151 }
2152
2153 if (!isa<FunctionDecl>(d)) {
2154 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2155 << Attr.getName() << 0 /*function*/;
2156 return;
2157 }
2158
Eric Christopherbc638a82010-12-01 22:13:54 +00002159 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2160 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002161}
2162
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002163static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2164 if (S.LangOpts.CUDA) {
2165 // check the attribute arguments.
2166 if (Attr.getNumArgs() != 0) {
2167 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2168 return;
2169 }
2170
2171 if (!isa<VarDecl>(d)) {
2172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2173 << Attr.getName() << 12 /*variable*/;
2174 return;
2175 }
2176
2177 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2178 } else {
2179 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2180 }
2181}
2182
2183static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2184 if (S.LangOpts.CUDA) {
2185 // check the attribute arguments.
2186 if (Attr.getNumArgs() != 0) {
2187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2188 return;
2189 }
2190
2191 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2193 << Attr.getName() << 2 /*variable and function*/;
2194 return;
2195 }
2196
2197 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2198 } else {
2199 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2200 }
2201}
2202
2203static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2204 if (S.LangOpts.CUDA) {
2205 // check the attribute arguments.
2206 if (Attr.getNumArgs() != 0) {
2207 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2208 return;
2209 }
2210
2211 if (!isa<FunctionDecl>(d)) {
2212 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2213 << Attr.getName() << 0 /*function*/;
2214 return;
2215 }
2216
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002217 FunctionDecl *FD = cast<FunctionDecl>(d);
2218 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002219 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002220 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2221 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2222 << FD->getType()
2223 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2224 "void");
2225 } else {
2226 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2227 << FD->getType();
2228 }
2229 return;
2230 }
2231
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002232 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2233 } else {
2234 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2235 }
2236}
2237
2238static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2239 if (S.LangOpts.CUDA) {
2240 // check the attribute arguments.
2241 if (Attr.getNumArgs() != 0) {
2242 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2243 return;
2244 }
2245
2246 if (!isa<FunctionDecl>(d)) {
2247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2248 << Attr.getName() << 0 /*function*/;
2249 return;
2250 }
2251
2252 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2253 } else {
2254 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2255 }
2256}
2257
2258static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2259 if (S.LangOpts.CUDA) {
2260 // check the attribute arguments.
2261 if (Attr.getNumArgs() != 0) {
2262 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2263 return;
2264 }
2265
2266 if (!isa<VarDecl>(d)) {
2267 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2268 << Attr.getName() << 12 /*variable*/;
2269 return;
2270 }
2271
2272 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2273 } else {
2274 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2275 }
2276}
2277
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002278static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002279 // check the attribute arguments.
2280 if (Attr.getNumArgs() != 0) {
2281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2282 return;
2283 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002284
Chris Lattner4225e232009-04-14 17:02:11 +00002285 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2286 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002287 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002288 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002289 return;
2290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002291
Douglas Gregor35b57532009-10-27 21:01:01 +00002292 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002293 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002294 return;
2295 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002296
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002297 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002298}
2299
John McCall3882ace2011-01-05 12:14:39 +00002300static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2301 if (hasDeclarator(d)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002302
John McCall3882ace2011-01-05 12:14:39 +00002303 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2304 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2305 CallingConv CC;
2306 if (S.CheckCallingConvAttr(attr, CC))
2307 return;
2308
2309 if (!isa<ObjCMethodDecl>(d)) {
2310 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2311 << attr.getName() << 0 /*function*/;
2312 return;
2313 }
2314
2315 switch (attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002316 case AttributeList::AT_fastcall:
John McCall3882ace2011-01-05 12:14:39 +00002317 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002318 return;
2319 case AttributeList::AT_stdcall:
John McCall3882ace2011-01-05 12:14:39 +00002320 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002321 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002322 case AttributeList::AT_thiscall:
John McCall3882ace2011-01-05 12:14:39 +00002323 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002324 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002325 case AttributeList::AT_cdecl:
John McCall3882ace2011-01-05 12:14:39 +00002326 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002327 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002328 case AttributeList::AT_pascal:
John McCall3882ace2011-01-05 12:14:39 +00002329 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00002330 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002331 default:
2332 llvm_unreachable("unexpected attribute kind");
2333 return;
2334 }
2335}
2336
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002337static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2338 assert(Attr.isInvalid() == false);
2339 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2340}
2341
John McCall3882ace2011-01-05 12:14:39 +00002342bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2343 if (attr.isInvalid())
2344 return true;
2345
2346 if (attr.getNumArgs() != 0) {
2347 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2348 attr.setInvalid();
2349 return true;
2350 }
2351
2352 // TODO: diagnose uses of these conventions on the wrong target.
2353 switch (attr.getKind()) {
2354 case AttributeList::AT_cdecl: CC = CC_C; break;
2355 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2356 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2357 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2358 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
2359 default: llvm_unreachable("unexpected attribute kind"); return true;
2360 }
2361
2362 return false;
2363}
2364
2365static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2366 if (hasDeclarator(d)) return;
2367
2368 unsigned numParams;
2369 if (S.CheckRegparmAttr(attr, numParams))
2370 return;
2371
2372 if (!isa<ObjCMethodDecl>(d)) {
2373 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2374 << attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002375 return;
2376 }
Eli Friedman7044b762009-03-27 21:06:47 +00002377
John McCall3882ace2011-01-05 12:14:39 +00002378 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2379}
2380
2381/// Checks a regparm attribute, returning true if it is ill-formed and
2382/// otherwise setting numParams to the appropriate value.
2383bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2384 if (attr.isInvalid())
2385 return true;
2386
2387 if (attr.getNumArgs() != 1) {
2388 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2389 attr.setInvalid();
2390 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002391 }
Eli Friedman7044b762009-03-27 21:06:47 +00002392
John McCall3882ace2011-01-05 12:14:39 +00002393 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002394 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002395 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00002396 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2397 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00002398 << "regparm" << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002399 attr.setInvalid();
2400 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002401 }
2402
John McCall3882ace2011-01-05 12:14:39 +00002403 if (Context.Target.getRegParmMax() == 0) {
2404 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002405 << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002406 attr.setInvalid();
2407 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002408 }
2409
John McCall3882ace2011-01-05 12:14:39 +00002410 numParams = NumParams.getZExtValue();
2411 if (numParams > Context.Target.getRegParmMax()) {
2412 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2413 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2414 attr.setInvalid();
2415 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002416 }
2417
John McCall3882ace2011-01-05 12:14:39 +00002418 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002419}
2420
Peter Collingbourne827301e2010-12-12 23:03:07 +00002421static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2422 if (S.LangOpts.CUDA) {
2423 // check the attribute arguments.
2424 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
2425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2426 << "1 or 2";
2427 return;
2428 }
2429
2430 if (!isFunctionOrMethod(d)) {
2431 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2432 << Attr.getName() << 0 /*function*/;
2433 return;
2434 }
2435
2436 Expr *MaxThreadsExpr = Attr.getArg(0);
2437 llvm::APSInt MaxThreads(32);
2438 if (MaxThreadsExpr->isTypeDependent() ||
2439 MaxThreadsExpr->isValueDependent() ||
2440 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2441 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2442 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2443 return;
2444 }
2445
2446 llvm::APSInt MinBlocks(32);
2447 if (Attr.getNumArgs() > 1) {
2448 Expr *MinBlocksExpr = Attr.getArg(1);
2449 if (MinBlocksExpr->isTypeDependent() ||
2450 MinBlocksExpr->isValueDependent() ||
2451 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2452 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2453 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2454 return;
2455 }
2456 }
2457
2458 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2459 MaxThreads.getZExtValue(),
2460 MinBlocks.getZExtValue()));
2461 } else {
2462 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2463 }
2464}
2465
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002466//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002467// Checker-specific attribute handlers.
2468//===----------------------------------------------------------------------===//
2469
John McCalled433932011-01-25 03:31:58 +00002470static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2471 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2472}
2473static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2474 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2475}
2476
2477static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2478 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2479 if (!param) {
2480 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2481 << SourceRange(attr.getLoc()) << attr.getName() << 4 /*parameter*/;
2482 return;
2483 }
2484
2485 bool typeOK, cf;
2486 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2487 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2488 cf = false;
2489 } else {
2490 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2491 cf = true;
2492 }
2493
2494 if (!typeOK) {
2495 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2496 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2497 return;
2498 }
2499
2500 if (cf)
2501 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2502 else
2503 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2504}
2505
2506static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2507 Sema &S) {
2508 if (!isa<ObjCMethodDecl>(d)) {
2509 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2510 << SourceRange(attr.getLoc()) << attr.getName() << 13 /*method*/;
2511 return;
2512 }
2513
2514 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2515}
2516
2517static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002518 Sema &S) {
2519
John McCalled433932011-01-25 03:31:58 +00002520 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002521
Ted Kremenek3b204e42009-05-13 21:07:32 +00002522 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002523 returnType = MD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002524 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002525 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002526 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002527 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCalled433932011-01-25 03:31:58 +00002528 << SourceRange(attr.getLoc()) << attr.getName()
2529 << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002530 return;
2531 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002532
John McCalled433932011-01-25 03:31:58 +00002533 bool typeOK;
2534 bool cf;
2535 switch (attr.getKind()) {
2536 default: llvm_unreachable("invalid ownership attribute"); return;
2537 case AttributeList::AT_ns_returns_autoreleased:
2538 case AttributeList::AT_ns_returns_retained:
2539 case AttributeList::AT_ns_returns_not_retained:
2540 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2541 cf = false;
2542 break;
2543
2544 case AttributeList::AT_cf_returns_retained:
2545 case AttributeList::AT_cf_returns_not_retained:
2546 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2547 cf = true;
2548 break;
2549 }
2550
2551 if (!typeOK) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002552 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalled433932011-01-25 03:31:58 +00002553 << SourceRange(attr.getLoc())
2554 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002555 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002556 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002557
John McCalled433932011-01-25 03:31:58 +00002558 switch (attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002559 default:
2560 assert(0 && "invalid ownership attribute");
2561 return;
John McCalled433932011-01-25 03:31:58 +00002562 case AttributeList::AT_ns_returns_autoreleased:
2563 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2564 S.Context));
2565 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002566 case AttributeList::AT_cf_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002567 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002568 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002569 return;
2570 case AttributeList::AT_ns_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002571 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002572 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002573 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002574 case AttributeList::AT_cf_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002575 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002576 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002577 return;
2578 case AttributeList::AT_ns_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002579 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002580 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002581 return;
2582 };
2583}
2584
Charles Davis163855f2010-02-16 18:27:26 +00002585static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2586 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Picheta83957a2010-12-19 06:50:37 +00002587 Attr.getKind() == AttributeList::AT_dllexport ||
2588 Attr.getKind() == AttributeList::AT_uuid;
2589}
2590
2591//===----------------------------------------------------------------------===//
2592// Microsoft specific attribute handlers.
2593//===----------------------------------------------------------------------===//
2594
2595static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2596 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2597 // check the attribute arguments.
2598 if (Attr.getNumArgs() != 1) {
2599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2600 return;
2601 }
2602 Expr *Arg = Attr.getArg(0);
2603 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichet7da11662010-12-20 01:41:49 +00002604 if (Str == 0 || Str->isWide()) {
2605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2606 << "uuid" << 1;
2607 return;
2608 }
2609
2610 llvm::StringRef StrRef = Str->getString();
2611
2612 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2613 StrRef.back() == '}';
2614
2615 // Validate GUID length.
2616 if (IsCurly && StrRef.size() != 38) {
2617 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2618 return;
2619 }
2620 if (!IsCurly && StrRef.size() != 36) {
2621 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2622 return;
2623 }
2624
2625 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2626 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlsson19588aa2011-01-23 21:07:30 +00002627 llvm::StringRef::iterator I = StrRef.begin();
2628 if (IsCurly) // Skip the optional '{'
2629 ++I;
2630
2631 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00002632 if (i == 8 || i == 13 || i == 18 || i == 23) {
2633 if (*I != '-') {
2634 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2635 return;
2636 }
2637 } else if (!isxdigit(*I)) {
2638 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2639 return;
2640 }
2641 I++;
2642 }
Francois Picheta83957a2010-12-19 06:50:37 +00002643
2644 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2645 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00002646 } else
Francois Picheta83957a2010-12-19 06:50:37 +00002647 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00002648}
2649
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002650//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002651// Top Level Sema Entry Points
2652//===----------------------------------------------------------------------===//
2653
Peter Collingbourneb331b262011-01-21 02:08:45 +00002654static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2655 const AttributeList &Attr, Sema &S) {
2656 switch (Attr.getKind()) {
2657 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2658 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2659 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2660 default:
2661 break;
2662 }
2663}
Abramo Bagnara50099372010-04-30 13:10:51 +00002664
Peter Collingbourneb331b262011-01-21 02:08:45 +00002665static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2666 const AttributeList &Attr, Sema &S) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002667 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002668 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002669 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2670 case AttributeList::AT_IBOutletCollection:
2671 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002672 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002673 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002674 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002675 case AttributeList::AT_neon_vector_type:
2676 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002677 // Ignore these, these are type attributes, handled by
2678 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002679 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002680 case AttributeList::AT_device:
2681 case AttributeList::AT_host:
2682 case AttributeList::AT_overloadable:
2683 // Ignore, this is a non-inheritable attribute, handled
2684 // by ProcessNonInheritableDeclAttr.
2685 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002686 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2687 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002688 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002689 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002690 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002691 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002692 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002693 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002694 HandleDependencyAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002695 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002696 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002697 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2698 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2699 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002700 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002701 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002702 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002703 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2704 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002705 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002706 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002707 case AttributeList::AT_launch_bounds:
2708 HandleLaunchBoundsAttr(D, Attr, S);
2709 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002710 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2711 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00002712 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002713 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002714 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002715 case AttributeList::AT_ownership_returns:
2716 case AttributeList::AT_ownership_takes:
2717 case AttributeList::AT_ownership_holds:
2718 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00002719 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002720 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2721 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002722 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002723 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002724
2725 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00002726 case AttributeList::AT_cf_consumed:
2727 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2728 case AttributeList::AT_ns_consumes_self:
2729 HandleNSConsumesSelfAttr(D, Attr, S); break;
2730
2731 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00002732 case AttributeList::AT_ns_returns_not_retained:
2733 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002734 case AttributeList::AT_ns_returns_retained:
2735 case AttributeList::AT_cf_returns_retained:
2736 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2737
Nate Begemanf2758702009-06-26 06:32:41 +00002738 case AttributeList::AT_reqd_wg_size:
2739 HandleReqdWorkGroupSize(D, Attr, S); break;
2740
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002741 case AttributeList::AT_init_priority:
2742 HandleInitPriorityAttr(D, Attr, S); break;
2743
Alexis Hunt54a02542009-11-25 04:20:27 +00002744 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2745 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002746 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2747 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2748 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002749 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002750 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2751 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002752 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002753 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002754 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002755 case AttributeList::AT_transparent_union:
2756 HandleTransparentUnionAttr(D, Attr, S);
2757 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002758 case AttributeList::AT_objc_exception:
2759 HandleObjCExceptionAttr(D, Attr, S);
2760 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002761 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2762 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2763 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2764 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2765 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2766 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2767 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2768 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2769 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002770 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002771 // Just ignore
2772 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002773 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2774 HandleNoInstrumentFunctionAttr(D, Attr, S);
2775 break;
John McCallab26cfa2010-02-05 21:31:56 +00002776 case AttributeList::AT_stdcall:
2777 case AttributeList::AT_cdecl:
2778 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002779 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002780 case AttributeList::AT_pascal:
Abramo Bagnara50099372010-04-30 13:10:51 +00002781 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002782 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002783 case AttributeList::AT_opencl_kernel_function:
2784 HandleOpenCLKernelAttr(D, Attr, S);
2785 break;
Francois Picheta83957a2010-12-19 06:50:37 +00002786 case AttributeList::AT_uuid:
2787 HandleUuidAttr(D, Attr, S);
2788 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002789 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002790 // Ask target about the attribute.
2791 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2792 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002793 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2794 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002795 break;
2796 }
2797}
2798
Peter Collingbourneb331b262011-01-21 02:08:45 +00002799/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2800/// the attribute applies to decls. If the attribute is a type attribute, just
2801/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2802/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2803static void ProcessDeclAttribute(Scope *scope, Decl *D,
2804 const AttributeList &Attr, Sema &S,
2805 bool NonInheritable, bool Inheritable) {
2806 if (Attr.isInvalid())
2807 return;
2808
2809 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2810 // FIXME: Try to deal with other __declspec attributes!
2811 return;
2812
2813 if (NonInheritable)
2814 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2815
2816 if (Inheritable)
2817 ProcessInheritableDeclAttr(scope, D, Attr, S);
2818}
2819
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002820/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2821/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00002822void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00002823 const AttributeList *AttrList,
2824 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002825 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00002826 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00002827 }
2828
2829 // GCC accepts
2830 // static int a9 __attribute__((weakref));
2831 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002832 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002833 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002834 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002835 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002836 }
2837}
2838
Ryan Flynn7d470f32009-07-30 03:15:39 +00002839/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2840/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002841NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002842 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002843 NamedDecl *NewD = 0;
2844 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2845 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2846 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002847 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002848 if (FD->getQualifier()) {
2849 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2850 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2851 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002852 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2853 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2854 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002855 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002856 VD->getStorageClass(),
2857 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002858 if (VD->getQualifier()) {
2859 VarDecl *NewVD = cast<VarDecl>(NewD);
2860 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2861 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002862 }
2863 return NewD;
2864}
2865
2866/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2867/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002868void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002869 if (W.getUsed()) return; // only do this once
2870 W.setUsed(true);
2871 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2872 IdentifierInfo *NDId = ND->getIdentifier();
2873 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002874 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2875 NDId->getName()));
2876 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00002877 WeakTopLevelDecl.push_back(NewD);
2878 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2879 // to insert Decl at TU scope, sorry.
2880 DeclContext *SavedContext = CurContext;
2881 CurContext = Context.getTranslationUnitDecl();
2882 PushOnScopeChains(NewD, S);
2883 CurContext = SavedContext;
2884 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002885 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002886 }
2887}
2888
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002889/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2890/// it, apply them to D. This is a bit tricky because PD can have attributes
2891/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002892void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
2893 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00002894 // It's valid to "forward-declare" #pragma weak, in which case we
2895 // have to do this.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002896 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCall6fe02402010-10-27 00:59:00 +00002897 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2898 if (IdentifierInfo *Id = ND->getIdentifier()) {
2899 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2900 = WeakUndeclaredIdentifiers.find(Id);
2901 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2902 WeakInfo W = I->second;
2903 DeclApplyPragmaWeak(S, ND, W);
2904 WeakUndeclaredIdentifiers[Id] = W;
2905 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002906 }
2907 }
2908 }
2909
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002910 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00002911 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00002912 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002913
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002914 // Walk the declarator structure, applying decl attributes that were in a type
2915 // position to the decl itself. This handles cases like:
2916 // int *__attr__(x)** D;
2917 // when X is a decl attribute.
2918 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2919 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00002920 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002921
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002922 // Finally, apply any attributes on the decl itself.
2923 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00002924 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002925}
John McCall28a6aea2009-11-04 02:18:39 +00002926
John McCallc1465822011-02-14 07:13:47 +00002927// This duplicates a vector push_back but hides the need to know the
2928// size of the type.
2929void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
2930 assert(StackSize <= StackCapacity);
2931
2932 // Grow the stack if necessary.
2933 if (StackSize == StackCapacity) {
2934 unsigned newCapacity = 2 * StackCapacity + 2;
2935 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
2936 const char *oldBuffer = (const char*) Stack;
2937
2938 if (StackCapacity)
2939 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
2940
2941 delete[] oldBuffer;
2942 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
2943 StackCapacity = newCapacity;
2944 }
2945
2946 assert(StackSize < StackCapacity);
2947 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00002948}
2949
John McCallc1465822011-02-14 07:13:47 +00002950void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
2951 Decl *decl) {
2952 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00002953
John McCallc1465822011-02-14 07:13:47 +00002954 // Check the invariants.
2955 assert(DD.StackSize >= state.SavedStackSize);
2956 assert(state.SavedStackSize >= DD.ActiveStackBase);
2957 assert(DD.ParsingDepth > 0);
2958
2959 // Drop the parsing depth.
2960 DD.ParsingDepth--;
2961
2962 // If there are no active diagnostics, we're done.
2963 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00002964 return;
2965
John McCall86121512010-01-27 03:50:35 +00002966 // We only want to actually emit delayed diagnostics when we
2967 // successfully parsed a decl.
John McCallc1465822011-02-14 07:13:47 +00002968 if (decl) {
2969 // We emit all the active diagnostics, not just those starting
2970 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00002971 // decl spec and another for each declarator; in a decl group like:
2972 // deprecated_typedef foo, *bar, baz();
2973 // only the declarator pops will be passed decls. This is correct;
2974 // we really do need to consider delayed diagnostics from the decl spec
2975 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00002976 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
2977 DelayedDiagnostic &diag = DD.Stack[i];
2978 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00002979 continue;
2980
John McCallc1465822011-02-14 07:13:47 +00002981 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00002982 case DelayedDiagnostic::Deprecation:
John McCallc1465822011-02-14 07:13:47 +00002983 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00002984 break;
2985
2986 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00002987 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00002988 break;
2989 }
2990 }
2991 }
2992
John McCall1064d7e2010-03-16 05:22:47 +00002993 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00002994 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
2995 DD.Stack[i].destroy();
John McCall1064d7e2010-03-16 05:22:47 +00002996
John McCallc1465822011-02-14 07:13:47 +00002997 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00002998}
2999
3000static bool isDeclDeprecated(Decl *D) {
3001 do {
3002 if (D->hasAttr<DeprecatedAttr>())
3003 return true;
3004 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3005 return false;
3006}
3007
John McCallb45a1e72010-08-26 02:13:20 +00003008void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00003009 Decl *Ctx) {
3010 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00003011 return;
3012
John McCall86121512010-01-27 03:50:35 +00003013 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003014 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003015 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003016 << DD.getDeprecationDecl()->getDeclName()
3017 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00003018 else
3019 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003020 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00003021}
3022
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003023void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003024 SourceLocation Loc,
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003025 bool UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00003026 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00003027 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3028 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall28a6aea2009-11-04 02:18:39 +00003029 return;
3030 }
3031
3032 // Otherwise, don't warn if our current context is deprecated.
3033 if (isDeclDeprecated(cast<Decl>(CurContext)))
3034 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003035 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003036 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3037 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003038 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003039 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003040 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
3041 else
3042 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
3043 }
John McCall28a6aea2009-11-04 02:18:39 +00003044}