blob: db4bc3e4af9ca5ec543c41ba84810617d92202e8 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000024using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000026
Chris Lattnere5c5ee12008-06-29 00:16:31 +000027//===----------------------------------------------------------------------===//
28// Helper functions
29//===----------------------------------------------------------------------===//
30
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031static const FunctionType *getFunctionType(const Decl *d,
32 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000033 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000034 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000035 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000036 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000037 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000038 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000039 Ty = decl->getUnderlyingType();
40 else
41 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000042
Chris Lattner6b6b5372008-06-26 18:38:35 +000043 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000044 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000045 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000046 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000047
John McCall183700f2009-09-21 23:43:11 +000048 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000049}
50
Daniel Dunbar35682492008-09-26 04:12:28 +000051// FIXME: We should provide an abstraction around a method or function
52// to provide the following bits of information.
53
Nuno Lopesd20254f2009-12-20 23:11:08 +000054/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055/// type (function or function-typed variable).
56static bool isFunction(const Decl *d) {
57 return getFunctionType(d, false) != NULL;
58}
59
60/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000061/// type (function or function-typed variable) or an Objective-C
62/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000063static bool isFunctionOrMethod(const Decl *d) {
64 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000065}
66
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000067/// isFunctionOrMethodOrBlock - Return true if the given decl has function
68/// type (function or function-typed variable) or an Objective-C
69/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000070static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000071 if (isFunctionOrMethod(d))
72 return true;
73 // check for block is more involved.
74 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
75 QualType Ty = V->getType();
76 return Ty->isBlockPointerType();
77 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000078 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000079}
80
John McCall711c52b2011-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 Dunbard3f2c102008-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 Jahanian620d89c2009-05-15 23:15:03 +000090/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000091static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000092 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000093 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000094 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000095 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-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 Kremeneka18d7d82009-08-14 20:49:40 +0000103static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +0000104 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000105 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000106 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
107 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +0000108 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000109}
110
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000111static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000112 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000113 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000114 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
115 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000116
Chris Lattner89951a82009-02-20 18:43:26 +0000117 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000118}
119
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000120static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-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 Kremeneka18d7d82009-08-14 20:49:40 +0000126static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000127 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000128 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000129 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000130 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000131 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000132 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000133 return cast<ObjCMethodDecl>(d)->isVariadic();
134 }
135}
136
Chandler Carruth07d7e7a2010-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 Lattner6b6b5372008-06-26 18:38:35 +0000143static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000144 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000145 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000146 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000147
John McCall506b57e2010-05-17 21:00:27 +0000148 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
149 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
John McCall506b57e2010-05-17 21:00:27 +0000152 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000153
Chris Lattner6b6b5372008-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 Dunbar085e8f72008-09-26 03:32:58 +0000159static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000160 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000161 if (!PT)
162 return false;
163
Ted Kremenek6217b802009-07-29 21:53:49 +0000164 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000165 if (!RT)
166 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000167
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000168 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000169 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000170 return false;
171
172 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
173}
174
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000175//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000176// Attribute Implementations
177//===----------------------------------------------------------------------===//
178
Daniel Dunbar3068ae02008-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 Stumpbf916502009-07-24 19:02:52 +0000183static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000184 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000185 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
186 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000187 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000188 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000189 }
Mike Stumpbf916502009-07-24 19:02:52 +0000190
Chris Lattner6b6b5372008-06-26 18:38:35 +0000191 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-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 McCallf7a1a742009-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 Gregor9cdda0c2009-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 Collingbourne7a730022010-11-23 20:45:58 +0000207 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000208 }
Douglas Gregor9cdda0c2009-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 McCall9ae2f072010-08-23 23:25:46 +0000212 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000213 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000214 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000215 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000216
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000217 // Remember this typedef decl, we will need it later for diagnostics.
218 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000219 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000220}
221
Chris Lattner803d0802008-06-29 00:43:07 +0000222static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000223 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000224 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226 return;
227 }
Mike Stumpbf916502009-07-24 19:02:52 +0000228
Chris Lattner6b6b5372008-06-26 18:38:35 +0000229 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Sean Huntcf807c42010-08-18 23:23:40 +0000230 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-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 Lattner803d0802008-06-29 00:43:07 +0000235 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000236 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000237 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000238 else
Sean Huntcf807c42010-08-18 23:23:40 +0000239 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000240 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000241 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000242}
243
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000244static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000245 // check the attribute arguments.
246 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000248 return;
249 }
Mike Stumpbf916502009-07-24 19:02:52 +0000250
Ted Kremenek63e5d7c2010-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()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000254 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000255 return;
256 }
257
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000258 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-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 Kremenekefbddd22010-02-17 02:37:45 +0000269 // Objective-C classes.
270 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000271 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000272 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000273 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000274
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000275 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000276}
277
Ted Kremenek857e9182010-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 Jahaniana8fb24f2010-08-17 20:23:12 +0000282 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-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 Kremenek4ee2bb12011-02-04 06:54:16 +0000290 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000291 return;
292 }
Fariborz Jahanian3a3400b2010-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 Jahaniana8fb24f2010-08-17 20:23:12 +0000306 IdentifierInfo *II = Attr.getParameterName();
307 if (!II)
308 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000309
John McCallb3d87482010-08-24 05:47:05 +0000310 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahaniana8fb24f2010-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 McCallb3d87482010-08-24 05:47:05 +0000316 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-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 }
Sean Huntcf807c42010-08-18 23:23:40 +0000326 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
327 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000328}
329
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000330static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-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 Dunbard3f2c102008-10-19 02:04:16 +0000333 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000334 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000335 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336 return;
337 }
Mike Stumpbf916502009-07-24 19:02:52 +0000338
Chandler Carruth07d7e7a2010-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 Kremenekeb2b2a32008-07-21 21:53:04 +0000343
344 // The nonnull attribute only applies to pointers.
345 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000346
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000347 for (AttributeList::arg_iterator I=Attr.arg_begin(),
348 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000349
350
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000352 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000353 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000354 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
355 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000356 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
357 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000358 return;
359 }
Mike Stumpbf916502009-07-24 19:02:52 +0000360
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000362
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000363 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000364 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000365 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000366 return;
367 }
Mike Stumpbf916502009-07-24 19:02:52 +0000368
Ted Kremenek465172f2008-07-21 22:09:15 +0000369 --x;
Chandler Carruth07d7e7a2010-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 Kremenekeb2b2a32008-07-21 21:53:04 +0000379
380 // Is the function argument a pointer type?
Douglas Gregorde436322010-12-15 15:41:46 +0000381 QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType();
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000382 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000383 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000384 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000385 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000386 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000387 }
Mike Stumpbf916502009-07-24 19:02:52 +0000388
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000389 NonNullArgs.push_back(x);
390 }
Mike Stumpbf916502009-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 Kremenek7fb43c12008-09-01 19:57:52 +0000394 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000395 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
Douglas Gregorde436322010-12-15 15:41:46 +0000396 QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType();
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000397 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000398 NonNullArgs.push_back(I);
Fariborz Jahanianff3a0782010-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 Kremenek46bbaca2008-11-18 06:52:58 +0000412 }
Mike Stumpbf916502009-07-24 19:02:52 +0000413
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000414 // No pointer arguments?
Fariborz Jahanian60acea42010-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 Kremenek7fb43c12008-09-01 19:57:52 +0000420 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000421 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000422 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000423
424 unsigned* start = &NonNullArgs[0];
425 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000426 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000427 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
428 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000429}
430
Ted Kremenekdd0e4902010-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 Rose2a479922010-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 Kremenekdd0e4902010-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.
Sean Huntcf807c42010-08-18 23:23:40 +0000447 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000448 switch (AL.getKind()) {
449 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000450 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000451 if (AL.getNumArgs() < 1) {
452 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
453 return;
454 }
Jordy Rose2a479922010-08-12 08:54:03 +0000455 break;
456 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000457 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000462 break;
463 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000464 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-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 Rose2a479922010-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 Kremenekdd0e4902010-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 Carruth07d7e7a2010-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 Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000495 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
496 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000497
Peter Collingbourne7a730022010-11-23 20:45:58 +0000498 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-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 Carruth07d7e7a2010-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 Kremenekdd0e4902010-07-31 01:52:11 +0000524 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000525 case OwnershipAttr::Takes:
526 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-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)
Sean Huntcf807c42010-08-18 23:23:40 +0000532 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000533 << "pointer"
534 << IdxExpr->getSourceRange();
535 continue;
536 }
537 break;
538 }
Sean Huntcf807c42010-08-18 23:23:40 +0000539 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000540 if (AL.getNumArgs() > 1) {
541 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000542 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000554 default:
555 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000556 } // switch
557
558 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-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 Kremenekdd0e4902010-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);
Sean Huntcf807c42010-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 Kremenekdd0e4902010-07-31 01:52:11 +0000583 }
Sean Huntcf807c42010-08-18 23:23:40 +0000584
585 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
586 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000587}
588
John McCall332bb2a2011-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 Espindola11e8ce72010-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 McCall332bb2a2011-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 Espindola11e8ce72010-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 Redl7a126a42010-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 McCall332bb2a2011-02-08 22:35:49 +0000637 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000638 return;
Rafael Espindola11e8ce72010-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 McCall332bb2a2011-02-08 22:35:49 +0000659 if (!hasEffectivelyInternalLinkage(nd)) {
660 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-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 Collingbourne7a730022010-11-23 20:45:58 +0000669 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-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 Christopherf48f3672010-12-01 22:13:54 +0000680 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
681 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000682 }
683
Sean Huntcf807c42010-08-18 23:23:40 +0000684 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000685}
686
Chris Lattner803d0802008-06-29 00:43:07 +0000687static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000688 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000689 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000691 return;
692 }
Mike Stumpbf916502009-07-24 19:02:52 +0000693
Peter Collingbourne7a730022010-11-23 20:45:58 +0000694 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000695 Arg = Arg->IgnoreParenCasts();
696 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000697
Chris Lattner6b6b5372008-06-26 18:38:35 +0000698 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000699 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000700 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000701 return;
702 }
Mike Stumpbf916502009-07-24 19:02:52 +0000703
Rafael Espindolaf5fe2922010-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 Lattner6b6b5372008-06-26 18:38:35 +0000709 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000710
Eric Christopherf48f3672010-12-01 22:13:54 +0000711 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
712 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000713}
714
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000715static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000716 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000717 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000718 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000720 return;
721 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000722
Chris Lattnerc5197432009-04-14 17:02:11 +0000723 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbardd0cb222010-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 Carlsson5bab7882009-02-19 19:16:48 +0000743 return;
744 }
Mike Stumpbf916502009-07-24 19:02:52 +0000745
Sean Huntcf807c42010-08-18 23:23:40 +0000746 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000747}
748
Ryan Flynn76168e22009-08-09 20:07:29 +0000749static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000750 // Check the attribute arguments.
Ryan Flynn76168e22009-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 Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000756 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000757 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000758 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000759 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000760 return;
761 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000762 }
763
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000764 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000765}
766
Dan Gohman34c26302010-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 Gohman34c26302010-11-17 00:03:07 +0000774 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
775}
776
Eric Christophera6cf1e72010-12-02 02:45:55 +0000777static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
778 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-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 Christophera6cf1e72010-12-02 02:45:55 +0000784}
785
786static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
787 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-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 Christophera6cf1e72010-12-02 02:45:55 +0000793}
794
John McCall711c52b2011-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 Kremenekb7252322009-04-10 00:01:14 +0000817}
818
819static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
820 Sema &S) {
Ted Kremenekb56c1cc2010-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 Lattner6b6b5372008-06-26 18:38:35 +0000843}
844
John Thompson35cc9622010-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 Christopherf48f3672010-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 Thompson35cc9622010-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 Thompson01add592010-09-18 01:12:07 +0000871 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-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 Thompson01add592010-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 Christopherf48f3672010-12-01 22:13:54 +0000895 for (RecordDecl::field_iterator iter = record->field_begin();
896 iter != record->field_end(); iter++) {
John Thompson01add592010-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
Sean Huntcf807c42010-08-18 23:23:40 +0000904 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000905}
906
Sean Huntbbd37c62009-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 McCall04a67a62010-02-05 21:31:56 +0000910 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000911 return;
912 }
913 // FIXME: Actually store the attribute on the declaration
914}
915
Ted Kremenek73798892008-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 Lattner3c73c412008-11-19 08:23:25 +0000919 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000920 return;
921 }
Mike Stumpbf916502009-07-24 19:02:52 +0000922
John McCallaec58602010-03-31 02:47:45 +0000923 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
Chris Lattner57ad3782011-02-17 20:34:02 +0000924 !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000925 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner57ad3782011-02-17 20:34:02 +0000926 << Attr.getName() << 14 /*variable, function, labels*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000927 return;
928 }
Mike Stumpbf916502009-07-24 19:02:52 +0000929
Sean Huntcf807c42010-08-18 23:23:40 +0000930 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000931}
932
Daniel Dunbarb805dad2009-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 Stumpbf916502009-07-24 19:02:52 +0000939
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000940 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000941 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-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 Kremenek5dc53c92009-05-13 21:07:32 +0000947 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000948 return;
949 }
Mike Stumpbf916502009-07-24 19:02:52 +0000950
Sean Huntcf807c42010-08-18 23:23:40 +0000951 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000952}
953
Daniel Dunbar3068ae02008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000957 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
958 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000959 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000960 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000961
962 int priority = 65535; // FIXME: Do not hardcode such constants.
963 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000964 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000965 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000966 if (E->isTypeDependent() || E->isValueDependent() ||
967 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000968 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000969 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000970 return;
971 }
972 priority = Idx.getZExtValue();
973 }
Mike Stumpbf916502009-07-24 19:02:52 +0000974
Chris Lattnerc5197432009-04-14 17:02:11 +0000975 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000976 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000977 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000978 return;
979 }
980
Eric Christopherf48f3672010-12-01 22:13:54 +0000981 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
982 priority));
Daniel Dunbar3068ae02008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000988 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
989 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000990 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000991 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000992
993 int priority = 65535; // FIXME: Do not hardcode such constants.
994 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000995 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000996 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000997 if (E->isTypeDependent() || E->isValueDependent() ||
998 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000999 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001000 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001001 return;
1002 }
1003 priority = Idx.getZExtValue();
1004 }
Mike Stumpbf916502009-07-24 19:02:52 +00001005
Anders Carlsson6782fc62008-08-22 22:10:48 +00001006 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001008 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001009 return;
1010 }
1011
Eric Christopherf48f3672010-12-01 22:13:54 +00001012 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
1013 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001014}
1015
Chris Lattner803d0802008-06-29 00:43:07 +00001016static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001017 unsigned NumArgs = Attr.getNumArgs();
1018 if (NumArgs > 1) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001019 S.Diag(Attr.getLoc(),
1020 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001021 return;
1022 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001023
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001024 // Handle the case where deprecated attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001025 llvm::StringRef Str;
1026 if (NumArgs == 1) {
1027 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001028 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001029 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1030 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001031 return;
1032 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001033 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001034 }
Mike Stumpbf916502009-07-24 19:02:52 +00001035
Chris Lattner951bbb22011-02-24 05:42:24 +00001036 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001037}
1038
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001039static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001040 unsigned NumArgs = Attr.getNumArgs();
1041 if (NumArgs > 1) {
Eric Christopherf48f3672010-12-01 22:13:54 +00001042 S.Diag(Attr.getLoc(),
1043 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001044 return;
1045 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001046
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001047 // Handle the case where unavailable attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001048 llvm::StringRef Str;
1049 if (NumArgs == 1) {
1050 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001051 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001052 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001053 diag::err_attribute_not_string) << "unavailable";
1054 return;
1055 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001056 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001057 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001058 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001059}
1060
Chris Lattner803d0802008-06-29 00:43:07 +00001061static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001062 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001063 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001065 return;
1066 }
Mike Stumpbf916502009-07-24 19:02:52 +00001067
Peter Collingbourne7a730022010-11-23 20:45:58 +00001068 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001069 Arg = Arg->IgnoreParenCasts();
1070 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001071
Chris Lattner6b6b5372008-06-26 18:38:35 +00001072 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001073 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001074 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001075 return;
1076 }
Mike Stumpbf916502009-07-24 19:02:52 +00001077
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001078 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001079 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001080
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001081 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001082 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001083 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001084 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001085 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001086 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001087 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001088 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001089 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001090 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001091 return;
1092 }
Mike Stumpbf916502009-07-24 19:02:52 +00001093
Sean Huntcf807c42010-08-18 23:23:40 +00001094 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001095}
1096
John McCalld5313b02011-03-02 11:33:24 +00001097static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr,
1098 Sema &S) {
1099 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1100 if (!method) {
1101 S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type)
1102 << 13; // methods
1103 return;
1104 }
1105
1106 if (attr.getNumArgs() != 0 || !attr.getParameterName()) {
1107 if (!attr.getParameterName() && attr.getNumArgs() == 1) {
1108 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
1109 << "objc_method_family" << 1;
1110 } else {
1111 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1112 }
1113 attr.setInvalid();
1114 return;
1115 }
1116
1117 llvm::StringRef param = attr.getParameterName()->getName();
1118 ObjCMethodFamilyAttr::FamilyKind family;
1119 if (param == "none")
1120 family = ObjCMethodFamilyAttr::OMF_None;
1121 else if (param == "alloc")
1122 family = ObjCMethodFamilyAttr::OMF_alloc;
1123 else if (param == "copy")
1124 family = ObjCMethodFamilyAttr::OMF_copy;
1125 else if (param == "init")
1126 family = ObjCMethodFamilyAttr::OMF_init;
1127 else if (param == "mutableCopy")
1128 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1129 else if (param == "new")
1130 family = ObjCMethodFamilyAttr::OMF_new;
1131 else {
1132 // Just warn and ignore it. This is future-proof against new
1133 // families being used in system headers.
1134 S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family);
1135 return;
1136 }
1137
1138 decl->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(),
1139 S.Context, family));
1140}
1141
Chris Lattner0db29ec2009-02-14 08:09:34 +00001142static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1143 Sema &S) {
1144 if (Attr.getNumArgs() != 0) {
1145 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1146 return;
1147 }
Mike Stumpbf916502009-07-24 19:02:52 +00001148
Chris Lattner0db29ec2009-02-14 08:09:34 +00001149 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1150 if (OCI == 0) {
1151 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1152 return;
1153 }
Mike Stumpbf916502009-07-24 19:02:52 +00001154
Sean Huntcf807c42010-08-18 23:23:40 +00001155 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001156}
1157
1158static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001159 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001160 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001161 return;
1162 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001163 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001164 QualType T = TD->getUnderlyingType();
1165 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001166 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001167 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1168 return;
1169 }
1170 }
Sean Huntcf807c42010-08-18 23:23:40 +00001171 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001172}
1173
Mike Stumpbf916502009-07-24 19:02:52 +00001174static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001175HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1176 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001178 return;
1179 }
1180
1181 if (!isa<FunctionDecl>(D)) {
1182 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1183 return;
1184 }
1185
Sean Huntcf807c42010-08-18 23:23:40 +00001186 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001187}
1188
Steve Naroff9eae5762008-09-18 16:44:58 +00001189static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001190 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001192 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001193 return;
1194 }
Mike Stumpbf916502009-07-24 19:02:52 +00001195
Steve Naroff9eae5762008-09-18 16:44:58 +00001196 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001197 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001198 return;
1199 }
Mike Stumpbf916502009-07-24 19:02:52 +00001200
Sean Huntcf807c42010-08-18 23:23:40 +00001201 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001202 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001203 type = BlocksAttr::ByRef;
1204 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001205 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001206 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001207 return;
1208 }
Mike Stumpbf916502009-07-24 19:02:52 +00001209
Sean Huntcf807c42010-08-18 23:23:40 +00001210 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001211}
1212
Anders Carlsson77091822008-10-05 18:05:59 +00001213static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1214 // check the attribute arguments.
1215 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001216 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1217 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001218 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001219 }
1220
Anders Carlsson77091822008-10-05 18:05:59 +00001221 int sentinel = 0;
1222 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001223 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001224 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001225 if (E->isTypeDependent() || E->isValueDependent() ||
1226 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001228 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001229 return;
1230 }
1231 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001232
Anders Carlsson77091822008-10-05 18:05:59 +00001233 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001234 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1235 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001236 return;
1237 }
1238 }
1239
1240 int nullPos = 0;
1241 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001242 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001243 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001244 if (E->isTypeDependent() || E->isValueDependent() ||
1245 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001246 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001247 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001248 return;
1249 }
1250 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001251
Anders Carlsson77091822008-10-05 18:05:59 +00001252 if (nullPos > 1 || nullPos < 0) {
1253 // FIXME: This error message could be improved, it would be nice
1254 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001255 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1256 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001257 return;
1258 }
1259 }
1260
1261 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001262 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001263 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001264
Chris Lattner897cd902009-03-17 23:03:47 +00001265 if (isa<FunctionNoProtoType>(FT)) {
1266 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1267 return;
1268 }
Mike Stumpbf916502009-07-24 19:02:52 +00001269
Chris Lattner897cd902009-03-17 23:03:47 +00001270 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001271 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001272 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001273 }
Anders Carlsson77091822008-10-05 18:05:59 +00001274 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1275 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001276 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001277 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001278 }
1279 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001280 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1281 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001282 ;
1283 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001284 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001285 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001286 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherf48f3672010-12-01 22:13:54 +00001287 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001288 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001289 int m = Ty->isFunctionPointerType() ? 0 : 1;
1290 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001291 return;
1292 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001293 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001294 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001295 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001296 return;
1297 }
Anders Carlsson77091822008-10-05 18:05:59 +00001298 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001299 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001300 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001301 return;
1302 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001303 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1304 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001305}
1306
Chris Lattner026dc962009-02-14 07:37:35 +00001307static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1308 // check the attribute arguments.
1309 if (Attr.getNumArgs() != 0) {
1310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1311 return;
1312 }
1313
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001314 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001315 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001316 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001317 return;
1318 }
Mike Stumpbf916502009-07-24 19:02:52 +00001319
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001320 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1321 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1322 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001323 return;
1324 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001325 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1326 if (MD->getResultType()->isVoidType()) {
1327 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1328 << Attr.getName() << 1;
1329 return;
1330 }
1331
Sean Huntcf807c42010-08-18 23:23:40 +00001332 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001333}
1334
John McCall332bb2a2011-02-08 22:35:49 +00001335static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001336 // check the attribute arguments.
John McCall332bb2a2011-02-08 22:35:49 +00001337 if (attr.getNumArgs() != 0) {
1338 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001339 return;
1340 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001341
John McCall332bb2a2011-02-08 22:35:49 +00001342 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
1343 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1344 << attr.getName() << 2 /*variables and functions*/;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001345 return;
1346 }
1347
John McCall332bb2a2011-02-08 22:35:49 +00001348 NamedDecl *nd = cast<NamedDecl>(d);
1349
1350 // 'weak' only applies to declarations with external linkage.
1351 if (hasEffectivelyInternalLinkage(nd)) {
1352 S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001353 return;
1354 }
Mike Stumpbf916502009-07-24 19:02:52 +00001355
John McCall332bb2a2011-02-08 22:35:49 +00001356 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001357}
1358
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001359static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1360 // check the attribute arguments.
1361 if (Attr.getNumArgs() != 0) {
1362 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1363 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001364 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001365
1366 // weak_import only applies to variable & function declarations.
1367 bool isDef = false;
1368 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1369 isDef = (!VD->hasExternalStorage() || VD->getInit());
1370 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001371 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001372 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1373 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001374 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001375 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001376 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001377 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001378 !isa<ObjCInterfaceDecl>(D))
1379 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001380 << Attr.getName() << 2 /*variable and function*/;
1381 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001382 }
1383
1384 // Merge should handle any subsequent violations.
1385 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001386 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001387 diag::warn_attribute_weak_import_invalid_on_definition)
1388 << "weak_import" << 2 /*variable and function*/;
1389 return;
1390 }
1391
Sean Huntcf807c42010-08-18 23:23:40 +00001392 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001393}
1394
Nate Begeman6f3d8382009-06-26 06:32:41 +00001395static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1396 Sema &S) {
1397 // Attribute has 3 arguments.
1398 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001399 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001400 return;
1401 }
1402
1403 unsigned WGSize[3];
1404 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001405 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001406 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001407 if (E->isTypeDependent() || E->isValueDependent() ||
1408 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001409 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1410 << "reqd_work_group_size" << E->getSourceRange();
1411 return;
1412 }
1413 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1414 }
Sean Huntcf807c42010-08-18 23:23:40 +00001415 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1416 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001417 WGSize[2]));
1418}
1419
Chris Lattner026dc962009-02-14 07:37:35 +00001420static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001421 // Attribute has no arguments.
1422 if (Attr.getNumArgs() != 1) {
1423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1424 return;
1425 }
1426
1427 // Make sure that there is a string literal as the sections's single
1428 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001429 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001430 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001431 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001432 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001433 return;
1434 }
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Chris Lattner797c3c42009-08-10 19:03:04 +00001436 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001437 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001438 if (!Error.empty()) {
1439 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1440 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001441 return;
1442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001444 // This attribute cannot be applied to local variables.
1445 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1446 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1447 return;
1448 }
1449
Eric Christopherf48f3672010-12-01 22:13:54 +00001450 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1451 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001452}
1453
Chris Lattner6b6b5372008-06-26 18:38:35 +00001454
Chris Lattner803d0802008-06-29 00:43:07 +00001455static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001456 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001457 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001459 return;
1460 }
Mike Stumpbf916502009-07-24 19:02:52 +00001461
Sean Huntcf807c42010-08-18 23:23:40 +00001462 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001463}
1464
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001465static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1466 // check the attribute arguments.
1467 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001468 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001469 return;
1470 }
Mike Stumpbf916502009-07-24 19:02:52 +00001471
Sean Huntcf807c42010-08-18 23:23:40 +00001472 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001473}
1474
1475static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1476 // check the attribute arguments.
1477 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001479 return;
1480 }
Mike Stumpbf916502009-07-24 19:02:52 +00001481
Sean Huntcf807c42010-08-18 23:23:40 +00001482 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001483}
1484
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001485static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001486 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001487 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1488 return;
1489 }
Mike Stumpbf916502009-07-24 19:02:52 +00001490
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001491 if (Attr.getNumArgs() != 0) {
1492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1493 return;
1494 }
Mike Stumpbf916502009-07-24 19:02:52 +00001495
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001496 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001497
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001498 if (!VD || !VD->hasLocalStorage()) {
1499 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1500 return;
1501 }
Mike Stumpbf916502009-07-24 19:02:52 +00001502
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001503 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001504 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001505 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001506 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1507 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001508 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001509 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001510 Attr.getParameterName();
1511 return;
1512 }
Mike Stumpbf916502009-07-24 19:02:52 +00001513
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001514 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1515 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001516 S.Diag(Attr.getParameterLoc(),
1517 diag::err_attribute_cleanup_arg_not_function)
1518 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001519 return;
1520 }
1521
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001522 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001523 S.Diag(Attr.getParameterLoc(),
1524 diag::err_attribute_cleanup_func_must_take_one_arg)
1525 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001526 return;
1527 }
Mike Stumpbf916502009-07-24 19:02:52 +00001528
Anders Carlsson89941c12009-02-07 23:16:50 +00001529 // We're currently more strict than GCC about what function types we accept.
1530 // If this ever proves to be a problem it should be easy to fix.
1531 QualType Ty = S.Context.getPointerType(VD->getType());
1532 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001533 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1534 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001535 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001536 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1537 Attr.getParameterName() << ParamTy << Ty;
1538 return;
1539 }
Mike Stumpbf916502009-07-24 19:02:52 +00001540
Sean Huntcf807c42010-08-18 23:23:40 +00001541 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001542 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001543}
1544
Mike Stumpbf916502009-07-24 19:02:52 +00001545/// Handle __attribute__((format_arg((idx)))) attribute based on
1546/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1547static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001548 if (Attr.getNumArgs() != 1) {
1549 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1550 return;
1551 }
1552 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1553 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1554 << Attr.getName() << 0 /*function*/;
1555 return;
1556 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001557
1558 // In C++ the implicit 'this' function parameter also counts, and they are
1559 // counted from one.
1560 bool HasImplicitThisParam = isInstanceMethod(d);
1561 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001562 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001563
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001564 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001565 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001566 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001567 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1568 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1570 << "format" << 2 << IdxExpr->getSourceRange();
1571 return;
1572 }
Mike Stumpbf916502009-07-24 19:02:52 +00001573
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001574 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1575 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1576 << "format" << 2 << IdxExpr->getSourceRange();
1577 return;
1578 }
Mike Stumpbf916502009-07-24 19:02:52 +00001579
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001580 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001581
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001582 if (HasImplicitThisParam) {
1583 if (ArgIdx == 0) {
1584 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1585 << "format_arg" << IdxExpr->getSourceRange();
1586 return;
1587 }
1588 ArgIdx--;
1589 }
1590
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001591 // make sure the format string is really a string
1592 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001593
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001594 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1595 if (not_nsstring_type &&
1596 !isCFStringType(Ty, S.Context) &&
1597 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001598 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001599 // FIXME: Should highlight the actual expression that has the wrong type.
1600 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001601 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001602 << IdxExpr->getSourceRange();
1603 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001604 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001605 Ty = getFunctionOrMethodResultType(d);
1606 if (!isNSStringType(Ty, S.Context) &&
1607 !isCFStringType(Ty, S.Context) &&
1608 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001609 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001610 // FIXME: Should highlight the actual expression that has the wrong type.
1611 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001612 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001613 << IdxExpr->getSourceRange();
1614 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001615 }
1616
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001617 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1618 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001619}
1620
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001621enum FormatAttrKind {
1622 CFStringFormat,
1623 NSStringFormat,
1624 StrftimeFormat,
1625 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001626 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001627 InvalidFormat
1628};
1629
1630/// getFormatAttrKind - Map from format attribute names to supported format
1631/// types.
1632static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1633 // Check for formats that get handled specially.
1634 if (Format == "NSString")
1635 return NSStringFormat;
1636 if (Format == "CFString")
1637 return CFStringFormat;
1638 if (Format == "strftime")
1639 return StrftimeFormat;
1640
1641 // Otherwise, check for supported formats.
1642 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1643 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1644 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00001645 Format == "zcmn_err" ||
1646 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001647 return SupportedFormat;
1648
Duncan Sandsbc525952010-03-23 14:44:19 +00001649 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1650 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001651 return IgnoredFormat;
1652
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001653 return InvalidFormat;
1654}
1655
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001656/// Handle __attribute__((init_priority(priority))) attributes based on
1657/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1658static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1659 Sema &S) {
1660 if (!S.getLangOptions().CPlusPlus) {
1661 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1662 return;
1663 }
1664
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001665 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1666 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1667 Attr.setInvalid();
1668 return;
1669 }
1670 QualType T = dyn_cast<VarDecl>(d)->getType();
1671 if (S.Context.getAsArrayType(T))
1672 T = S.Context.getBaseElementType(T);
1673 if (!T->getAs<RecordType>()) {
1674 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1675 Attr.setInvalid();
1676 return;
1677 }
1678
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001679 if (Attr.getNumArgs() != 1) {
1680 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1681 Attr.setInvalid();
1682 return;
1683 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001684 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001685
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001686 llvm::APSInt priority(32);
1687 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1688 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1689 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1690 << "init_priority" << priorityExpr->getSourceRange();
1691 Attr.setInvalid();
1692 return;
1693 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001694 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001695 if (prioritynum < 101 || prioritynum > 65535) {
1696 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1697 << priorityExpr->getSourceRange();
1698 Attr.setInvalid();
1699 return;
1700 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001701 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1702 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001703}
1704
Mike Stumpbf916502009-07-24 19:02:52 +00001705/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1706/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001707static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001708
Chris Lattner545dd342008-06-28 23:36:30 +00001709 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001710 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001711 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001712 return;
1713 }
1714
Chris Lattner545dd342008-06-28 23:36:30 +00001715 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001716 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001717 return;
1718 }
1719
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001720 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001721 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001722 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001723 return;
1724 }
1725
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001726 // In C++ the implicit 'this' function parameter also counts, and they are
1727 // counted from one.
1728 bool HasImplicitThisParam = isInstanceMethod(d);
1729 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001730 unsigned FirstIdx = 1;
1731
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001732 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001733
1734 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001735 if (Format.startswith("__") && Format.endswith("__"))
1736 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001737
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001738 // Check for supported formats.
1739 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001740
1741 if (Kind == IgnoredFormat)
1742 return;
1743
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001744 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001745 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001746 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001747 return;
1748 }
1749
1750 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001751 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001752 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001753 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1754 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001755 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001756 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001757 return;
1758 }
1759
1760 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001761 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001762 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001763 return;
1764 }
1765
1766 // FIXME: Do we need to bounds check?
1767 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001768
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001769 if (HasImplicitThisParam) {
1770 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001771 S.Diag(Attr.getLoc(),
1772 diag::err_format_attribute_implicit_this_format_string)
1773 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001774 return;
1775 }
1776 ArgIdx--;
1777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Chris Lattner6b6b5372008-06-26 18:38:35 +00001779 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001780 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001781
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001782 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001783 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001784 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1785 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001786 return;
1787 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001788 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001789 // FIXME: do we need to check if the type is NSString*? What are the
1790 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001791 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001792 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001793 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1794 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001795 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001796 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001797 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001798 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001799 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001800 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1801 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001802 return;
1803 }
1804
1805 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001806 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001807 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001808 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1809 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001810 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001811 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001812 return;
1813 }
1814
1815 // check if the function is variadic if the 3rd argument non-zero
1816 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001817 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001818 ++NumArgs; // +1 for ...
1819 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001820 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001821 return;
1822 }
1823 }
1824
Chris Lattner3c73c412008-11-19 08:23:25 +00001825 // strftime requires FirstArg to be 0 because it doesn't read from any
1826 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001827 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001828 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001829 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1830 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001831 return;
1832 }
1833 // if 0 it disables parameter checking (to use with e.g. va_list)
1834 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001835 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001836 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001837 return;
1838 }
1839
Sean Huntcf807c42010-08-18 23:23:40 +00001840 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1841 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001842 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001843}
1844
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001845static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1846 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001847 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001848 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001850 return;
1851 }
1852
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001853 // Try to find the underlying union declaration.
1854 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001855 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001856 if (TD && TD->getUnderlyingType()->isUnionType())
1857 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1858 else
1859 RD = dyn_cast<RecordDecl>(d);
1860
1861 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001863 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001864 return;
1865 }
1866
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001867 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001868 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001869 diag::warn_transparent_union_attribute_not_definition);
1870 return;
1871 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001872
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001873 RecordDecl::field_iterator Field = RD->field_begin(),
1874 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001875 if (Field == FieldEnd) {
1876 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1877 return;
1878 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001879
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001880 FieldDecl *FirstField = *Field;
1881 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001882 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001883 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001884 diag::warn_transparent_union_attribute_floating)
1885 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001886 return;
1887 }
1888
1889 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1890 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1891 for (; Field != FieldEnd; ++Field) {
1892 QualType FieldType = Field->getType();
1893 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1894 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1895 // Warn if we drop the attribute.
1896 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001897 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001898 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001899 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001900 diag::warn_transparent_union_attribute_field_size_align)
1901 << isSize << Field->getDeclName() << FieldBits;
1902 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001903 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001904 diag::note_transparent_union_first_field_size_align)
1905 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001906 return;
1907 }
1908 }
1909
Sean Huntcf807c42010-08-18 23:23:40 +00001910 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001911}
1912
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001913static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001914 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001915 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001917 return;
1918 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001919 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001920 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001921
Chris Lattner6b6b5372008-06-26 18:38:35 +00001922 // Make sure that there is a string literal as the annotation's single
1923 // argument.
1924 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001925 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001926 return;
1927 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001928 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1929 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001930}
1931
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001932static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001933 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001934 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001936 return;
1937 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001938
1939 //FIXME: The C++0x version of this attribute has more limited applicabilty
1940 // than GNU's, and should error out when it is used to specify a
1941 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001942
Chris Lattner545dd342008-06-28 23:36:30 +00001943 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001944 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001945 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001946 }
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Peter Collingbourne7a730022010-11-23 20:45:58 +00001948 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001949}
1950
1951void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1952 if (E->isTypeDependent() || E->isValueDependent()) {
1953 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001954 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001955 return;
1956 }
1957
Sean Huntcf807c42010-08-18 23:23:40 +00001958 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001959 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001960 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1961 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1962 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001963 return;
1964 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001965 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001966 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1967 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001968 return;
1969 }
1970
Sean Huntcf807c42010-08-18 23:23:40 +00001971 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1972}
1973
1974void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1975 // FIXME: Cache the number on the Attr object if non-dependent?
1976 // FIXME: Perform checking of type validity
1977 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1978 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001979}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001980
Mike Stumpbf916502009-07-24 19:02:52 +00001981/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1982/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001983///
Mike Stumpbf916502009-07-24 19:02:52 +00001984/// Despite what would be logical, the mode attribute is a decl attribute, not a
1985/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1986/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001987static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001988 // This attribute isn't documented, but glibc uses it. It changes
1989 // the width of an int or unsigned int to the specified size.
1990
1991 // Check that there aren't any arguments
1992 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001993 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001994 return;
1995 }
1996
1997 IdentifierInfo *Name = Attr.getParameterName();
1998 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001999 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002000 return;
2001 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002002
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002003 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002004
2005 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002006 if (Str.startswith("__") && Str.endswith("__"))
2007 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002008
2009 unsigned DestWidth = 0;
2010 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002011 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002012 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002013 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002014 switch (Str[0]) {
2015 case 'Q': DestWidth = 8; break;
2016 case 'H': DestWidth = 16; break;
2017 case 'S': DestWidth = 32; break;
2018 case 'D': DestWidth = 64; break;
2019 case 'X': DestWidth = 96; break;
2020 case 'T': DestWidth = 128; break;
2021 }
2022 if (Str[1] == 'F') {
2023 IntegerMode = false;
2024 } else if (Str[1] == 'C') {
2025 IntegerMode = false;
2026 ComplexMode = true;
2027 } else if (Str[1] != 'I') {
2028 DestWidth = 0;
2029 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002030 break;
2031 case 4:
2032 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2033 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002034 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002035 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002036 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002037 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002038 break;
2039 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002040 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002041 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002042 break;
2043 }
2044
2045 QualType OldTy;
2046 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
2047 OldTy = TD->getUnderlyingType();
2048 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2049 OldTy = VD->getType();
2050 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002051 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2052 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002053 return;
2054 }
Eli Friedman73397492009-03-03 06:41:03 +00002055
John McCall183700f2009-09-21 23:43:11 +00002056 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002057 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2058 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002059 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002060 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2061 } else if (ComplexMode) {
2062 if (!OldTy->isComplexType())
2063 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2064 } else {
2065 if (!OldTy->isFloatingType())
2066 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2067 }
2068
Mike Stump390b4cc2009-05-16 07:39:55 +00002069 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2070 // and friends, at least with glibc.
2071 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2072 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002073 // FIXME: Make sure floating-point mappings are accurate
2074 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002075 QualType NewTy;
2076 switch (DestWidth) {
2077 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002078 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002079 return;
2080 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002081 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002082 return;
2083 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002084 if (!IntegerMode) {
2085 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2086 return;
2087 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002088 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002089 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002090 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002091 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002092 break;
2093 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002094 if (!IntegerMode) {
2095 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2096 return;
2097 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002098 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002099 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002100 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002101 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002102 break;
2103 case 32:
2104 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002105 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002106 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002107 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002108 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002109 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002110 break;
2111 case 64:
2112 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002113 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002114 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002115 if (S.Context.Target.getLongWidth() == 64)
2116 NewTy = S.Context.LongTy;
2117 else
2118 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002119 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002120 if (S.Context.Target.getLongWidth() == 64)
2121 NewTy = S.Context.UnsignedLongTy;
2122 else
2123 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002124 break;
Eli Friedman73397492009-03-03 06:41:03 +00002125 case 96:
2126 NewTy = S.Context.LongDoubleTy;
2127 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002128 case 128:
2129 if (!IntegerMode) {
2130 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2131 return;
2132 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002133 if (OldTy->isSignedIntegerType())
2134 NewTy = S.Context.Int128Ty;
2135 else
2136 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002137 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002138 }
2139
Eli Friedman73397492009-03-03 06:41:03 +00002140 if (ComplexMode) {
2141 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002142 }
2143
2144 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002145 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2146 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002147 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002148 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002149 cast<ValueDecl>(D)->setType(NewTy);
2150}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002151
Mike Stump1feade82009-08-26 22:31:08 +00002152static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002153 // check the attribute arguments.
2154 if (Attr.getNumArgs() > 0) {
2155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2156 return;
2157 }
Anders Carlssone896d982009-02-13 08:11:52 +00002158
Anders Carlsson5bab7882009-02-19 19:16:48 +00002159 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002160 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002161 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00002162 return;
2163 }
Mike Stumpbf916502009-07-24 19:02:52 +00002164
Sean Huntcf807c42010-08-18 23:23:40 +00002165 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002166}
2167
Mike Stump1feade82009-08-26 22:31:08 +00002168static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002169 // check the attribute arguments.
2170 if (Attr.getNumArgs() != 0) {
2171 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2172 return;
2173 }
Mike Stumpbf916502009-07-24 19:02:52 +00002174
Chris Lattnerc5197432009-04-14 17:02:11 +00002175 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002176 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002177 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002178 return;
2179 }
Mike Stumpbf916502009-07-24 19:02:52 +00002180
Sean Huntcf807c42010-08-18 23:23:40 +00002181 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002182}
2183
Chris Lattner7255a2d2010-06-22 00:03:40 +00002184static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2185 Sema &S) {
2186 // check the attribute arguments.
2187 if (Attr.getNumArgs() != 0) {
2188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2189 return;
2190 }
2191
2192 if (!isa<FunctionDecl>(d)) {
2193 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2194 << Attr.getName() << 0 /*function*/;
2195 return;
2196 }
2197
Eric Christopherf48f3672010-12-01 22:13:54 +00002198 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2199 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002200}
2201
Peter Collingbourneced76712010-12-01 03:15:31 +00002202static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2203 if (S.LangOpts.CUDA) {
2204 // check the attribute arguments.
2205 if (Attr.getNumArgs() != 0) {
2206 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2207 return;
2208 }
2209
2210 if (!isa<VarDecl>(d)) {
2211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2212 << Attr.getName() << 12 /*variable*/;
2213 return;
2214 }
2215
2216 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2217 } else {
2218 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2219 }
2220}
2221
2222static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2223 if (S.LangOpts.CUDA) {
2224 // check the attribute arguments.
2225 if (Attr.getNumArgs() != 0) {
2226 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2227 return;
2228 }
2229
2230 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2231 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2232 << Attr.getName() << 2 /*variable and function*/;
2233 return;
2234 }
2235
2236 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2237 } else {
2238 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2239 }
2240}
2241
2242static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2243 if (S.LangOpts.CUDA) {
2244 // check the attribute arguments.
2245 if (Attr.getNumArgs() != 0) {
2246 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2247 return;
2248 }
2249
2250 if (!isa<FunctionDecl>(d)) {
2251 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2252 << Attr.getName() << 0 /*function*/;
2253 return;
2254 }
2255
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002256 FunctionDecl *FD = cast<FunctionDecl>(d);
2257 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002258 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002259 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2260 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2261 << FD->getType()
2262 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2263 "void");
2264 } else {
2265 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2266 << FD->getType();
2267 }
2268 return;
2269 }
2270
Peter Collingbourneced76712010-12-01 03:15:31 +00002271 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2272 } else {
2273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2274 }
2275}
2276
2277static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2278 if (S.LangOpts.CUDA) {
2279 // check the attribute arguments.
2280 if (Attr.getNumArgs() != 0) {
2281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2282 return;
2283 }
2284
2285 if (!isa<FunctionDecl>(d)) {
2286 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2287 << Attr.getName() << 0 /*function*/;
2288 return;
2289 }
2290
2291 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2292 } else {
2293 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2294 }
2295}
2296
2297static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2298 if (S.LangOpts.CUDA) {
2299 // check the attribute arguments.
2300 if (Attr.getNumArgs() != 0) {
2301 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2302 return;
2303 }
2304
2305 if (!isa<VarDecl>(d)) {
2306 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2307 << Attr.getName() << 12 /*variable*/;
2308 return;
2309 }
2310
2311 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2312 } else {
2313 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2314 }
2315}
2316
Chris Lattnercf2a7212009-04-20 19:12:28 +00002317static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002318 // check the attribute arguments.
2319 if (Attr.getNumArgs() != 0) {
2320 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2321 return;
2322 }
Mike Stumpbf916502009-07-24 19:02:52 +00002323
Chris Lattnerc5197432009-04-14 17:02:11 +00002324 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2325 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002326 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002327 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002328 return;
2329 }
Mike Stumpbf916502009-07-24 19:02:52 +00002330
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002331 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002332 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002333 return;
2334 }
Mike Stumpbf916502009-07-24 19:02:52 +00002335
Sean Huntcf807c42010-08-18 23:23:40 +00002336 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002337}
2338
John McCall711c52b2011-01-05 12:14:39 +00002339static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2340 if (hasDeclarator(d)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002341
John McCall711c52b2011-01-05 12:14:39 +00002342 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2343 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2344 CallingConv CC;
2345 if (S.CheckCallingConvAttr(attr, CC))
2346 return;
2347
2348 if (!isa<ObjCMethodDecl>(d)) {
2349 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2350 << attr.getName() << 0 /*function*/;
2351 return;
2352 }
2353
2354 switch (attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002355 case AttributeList::AT_fastcall:
John McCall711c52b2011-01-05 12:14:39 +00002356 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002357 return;
2358 case AttributeList::AT_stdcall:
John McCall711c52b2011-01-05 12:14:39 +00002359 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002360 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002361 case AttributeList::AT_thiscall:
John McCall711c52b2011-01-05 12:14:39 +00002362 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002363 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002364 case AttributeList::AT_cdecl:
John McCall711c52b2011-01-05 12:14:39 +00002365 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002366 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002367 case AttributeList::AT_pascal:
John McCall711c52b2011-01-05 12:14:39 +00002368 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002369 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002370 default:
2371 llvm_unreachable("unexpected attribute kind");
2372 return;
2373 }
2374}
2375
Peter Collingbournef315fa82011-02-14 01:42:53 +00002376static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2377 assert(Attr.isInvalid() == false);
2378 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2379}
2380
John McCall711c52b2011-01-05 12:14:39 +00002381bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2382 if (attr.isInvalid())
2383 return true;
2384
2385 if (attr.getNumArgs() != 0) {
2386 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2387 attr.setInvalid();
2388 return true;
2389 }
2390
2391 // TODO: diagnose uses of these conventions on the wrong target.
2392 switch (attr.getKind()) {
2393 case AttributeList::AT_cdecl: CC = CC_C; break;
2394 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2395 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2396 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2397 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
2398 default: llvm_unreachable("unexpected attribute kind"); return true;
2399 }
2400
2401 return false;
2402}
2403
2404static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2405 if (hasDeclarator(d)) return;
2406
2407 unsigned numParams;
2408 if (S.CheckRegparmAttr(attr, numParams))
2409 return;
2410
2411 if (!isa<ObjCMethodDecl>(d)) {
2412 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2413 << attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002414 return;
2415 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002416
John McCall711c52b2011-01-05 12:14:39 +00002417 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2418}
2419
2420/// Checks a regparm attribute, returning true if it is ill-formed and
2421/// otherwise setting numParams to the appropriate value.
2422bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2423 if (attr.isInvalid())
2424 return true;
2425
2426 if (attr.getNumArgs() != 1) {
2427 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2428 attr.setInvalid();
2429 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002430 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002431
John McCall711c52b2011-01-05 12:14:39 +00002432 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002433 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002434 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002435 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2436 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002437 << "regparm" << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002438 attr.setInvalid();
2439 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002440 }
2441
John McCall711c52b2011-01-05 12:14:39 +00002442 if (Context.Target.getRegParmMax() == 0) {
2443 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002444 << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002445 attr.setInvalid();
2446 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002447 }
2448
John McCall711c52b2011-01-05 12:14:39 +00002449 numParams = NumParams.getZExtValue();
2450 if (numParams > Context.Target.getRegParmMax()) {
2451 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2452 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2453 attr.setInvalid();
2454 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002455 }
2456
John McCall711c52b2011-01-05 12:14:39 +00002457 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002458}
2459
Peter Collingbourne7b381982010-12-12 23:03:07 +00002460static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2461 if (S.LangOpts.CUDA) {
2462 // check the attribute arguments.
2463 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
2464 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2465 << "1 or 2";
2466 return;
2467 }
2468
2469 if (!isFunctionOrMethod(d)) {
2470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2471 << Attr.getName() << 0 /*function*/;
2472 return;
2473 }
2474
2475 Expr *MaxThreadsExpr = Attr.getArg(0);
2476 llvm::APSInt MaxThreads(32);
2477 if (MaxThreadsExpr->isTypeDependent() ||
2478 MaxThreadsExpr->isValueDependent() ||
2479 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2481 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2482 return;
2483 }
2484
2485 llvm::APSInt MinBlocks(32);
2486 if (Attr.getNumArgs() > 1) {
2487 Expr *MinBlocksExpr = Attr.getArg(1);
2488 if (MinBlocksExpr->isTypeDependent() ||
2489 MinBlocksExpr->isValueDependent() ||
2490 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2491 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2492 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2493 return;
2494 }
2495 }
2496
2497 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2498 MaxThreads.getZExtValue(),
2499 MinBlocks.getZExtValue()));
2500 } else {
2501 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2502 }
2503}
2504
Chris Lattner0744e5f2008-06-29 00:23:49 +00002505//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002506// Checker-specific attribute handlers.
2507//===----------------------------------------------------------------------===//
2508
John McCallc7ad3812011-01-25 03:31:58 +00002509static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2510 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2511}
2512static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2513 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2514}
2515
2516static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2517 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2518 if (!param) {
2519 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2520 << SourceRange(attr.getLoc()) << attr.getName() << 4 /*parameter*/;
2521 return;
2522 }
2523
2524 bool typeOK, cf;
2525 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2526 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2527 cf = false;
2528 } else {
2529 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2530 cf = true;
2531 }
2532
2533 if (!typeOK) {
2534 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2535 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2536 return;
2537 }
2538
2539 if (cf)
2540 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2541 else
2542 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2543}
2544
2545static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2546 Sema &S) {
2547 if (!isa<ObjCMethodDecl>(d)) {
2548 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2549 << SourceRange(attr.getLoc()) << attr.getName() << 13 /*method*/;
2550 return;
2551 }
2552
2553 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2554}
2555
2556static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenekb71368d2009-05-09 02:44:38 +00002557 Sema &S) {
2558
John McCallc7ad3812011-01-25 03:31:58 +00002559 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002560
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002561 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002562 returnType = MD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002563 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002564 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002565 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002566 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCallc7ad3812011-01-25 03:31:58 +00002567 << SourceRange(attr.getLoc()) << attr.getName()
2568 << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002569 return;
2570 }
Mike Stumpbf916502009-07-24 19:02:52 +00002571
John McCallc7ad3812011-01-25 03:31:58 +00002572 bool typeOK;
2573 bool cf;
2574 switch (attr.getKind()) {
2575 default: llvm_unreachable("invalid ownership attribute"); return;
2576 case AttributeList::AT_ns_returns_autoreleased:
2577 case AttributeList::AT_ns_returns_retained:
2578 case AttributeList::AT_ns_returns_not_retained:
2579 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2580 cf = false;
2581 break;
2582
2583 case AttributeList::AT_cf_returns_retained:
2584 case AttributeList::AT_cf_returns_not_retained:
2585 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2586 cf = true;
2587 break;
2588 }
2589
2590 if (!typeOK) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002591 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallc7ad3812011-01-25 03:31:58 +00002592 << SourceRange(attr.getLoc())
2593 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00002594 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002595 }
Mike Stumpbf916502009-07-24 19:02:52 +00002596
John McCallc7ad3812011-01-25 03:31:58 +00002597 switch (attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002598 default:
2599 assert(0 && "invalid ownership attribute");
2600 return;
John McCallc7ad3812011-01-25 03:31:58 +00002601 case AttributeList::AT_ns_returns_autoreleased:
2602 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2603 S.Context));
2604 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002605 case AttributeList::AT_cf_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002606 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002607 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002608 return;
2609 case AttributeList::AT_ns_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002610 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002611 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002612 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002613 case AttributeList::AT_cf_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002614 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002615 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002616 return;
2617 case AttributeList::AT_ns_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002618 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002619 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002620 return;
2621 };
2622}
2623
Charles Davisf0122fe2010-02-16 18:27:26 +00002624static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2625 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002626 Attr.getKind() == AttributeList::AT_dllexport ||
2627 Attr.getKind() == AttributeList::AT_uuid;
2628}
2629
2630//===----------------------------------------------------------------------===//
2631// Microsoft specific attribute handlers.
2632//===----------------------------------------------------------------------===//
2633
2634static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2635 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2636 // check the attribute arguments.
2637 if (Attr.getNumArgs() != 1) {
2638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2639 return;
2640 }
2641 Expr *Arg = Attr.getArg(0);
2642 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichetd3d3be92010-12-20 01:41:49 +00002643 if (Str == 0 || Str->isWide()) {
2644 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2645 << "uuid" << 1;
2646 return;
2647 }
2648
2649 llvm::StringRef StrRef = Str->getString();
2650
2651 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2652 StrRef.back() == '}';
2653
2654 // Validate GUID length.
2655 if (IsCurly && StrRef.size() != 38) {
2656 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2657 return;
2658 }
2659 if (!IsCurly && StrRef.size() != 36) {
2660 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2661 return;
2662 }
2663
2664 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2665 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlssonf89e0422011-01-23 21:07:30 +00002666 llvm::StringRef::iterator I = StrRef.begin();
2667 if (IsCurly) // Skip the optional '{'
2668 ++I;
2669
2670 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002671 if (i == 8 || i == 13 || i == 18 || i == 23) {
2672 if (*I != '-') {
2673 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2674 return;
2675 }
2676 } else if (!isxdigit(*I)) {
2677 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2678 return;
2679 }
2680 I++;
2681 }
Francois Pichet11542142010-12-19 06:50:37 +00002682
2683 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2684 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00002685 } else
Francois Pichet11542142010-12-19 06:50:37 +00002686 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00002687}
2688
Ted Kremenekb71368d2009-05-09 02:44:38 +00002689//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002690// Top Level Sema Entry Points
2691//===----------------------------------------------------------------------===//
2692
Peter Collingbourne60700392011-01-21 02:08:45 +00002693static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2694 const AttributeList &Attr, Sema &S) {
2695 switch (Attr.getKind()) {
2696 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2697 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2698 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2699 default:
2700 break;
2701 }
2702}
Abramo Bagnarae215f722010-04-30 13:10:51 +00002703
Peter Collingbourne60700392011-01-21 02:08:45 +00002704static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2705 const AttributeList &Attr, Sema &S) {
Chris Lattner803d0802008-06-29 00:43:07 +00002706 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002707 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002708 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2709 case AttributeList::AT_IBOutletCollection:
2710 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002711 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002712 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002713 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002714 case AttributeList::AT_neon_vector_type:
2715 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002716 // Ignore these, these are type attributes, handled by
2717 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002718 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002719 case AttributeList::AT_device:
2720 case AttributeList::AT_host:
2721 case AttributeList::AT_overloadable:
2722 // Ignore, this is a non-inheritable attribute, handled
2723 // by ProcessNonInheritableDeclAttr.
2724 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002725 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2726 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002727 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002728 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002729 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002730 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002731 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002732 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002733 HandleDependencyAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002734 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002735 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002736 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2737 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2738 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002739 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002740 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002741 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002742 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2743 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002744 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002745 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002746 case AttributeList::AT_launch_bounds:
2747 HandleLaunchBoundsAttr(D, Attr, S);
2748 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002749 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2750 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002751 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002752 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002753 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002754 case AttributeList::AT_ownership_returns:
2755 case AttributeList::AT_ownership_takes:
2756 case AttributeList::AT_ownership_holds:
2757 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002758 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002759 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2760 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002761 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002762 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002763
2764 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00002765 case AttributeList::AT_cf_consumed:
2766 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2767 case AttributeList::AT_ns_consumes_self:
2768 HandleNSConsumesSelfAttr(D, Attr, S); break;
2769
2770 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00002771 case AttributeList::AT_ns_returns_not_retained:
2772 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002773 case AttributeList::AT_ns_returns_retained:
2774 case AttributeList::AT_cf_returns_retained:
2775 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2776
Nate Begeman6f3d8382009-06-26 06:32:41 +00002777 case AttributeList::AT_reqd_wg_size:
2778 HandleReqdWorkGroupSize(D, Attr, S); break;
2779
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002780 case AttributeList::AT_init_priority:
2781 HandleInitPriorityAttr(D, Attr, S); break;
2782
Sean Hunt7725e672009-11-25 04:20:27 +00002783 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2784 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002785 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2786 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2787 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002788 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002789 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2790 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002791 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002792 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002793 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002794 case AttributeList::AT_transparent_union:
2795 HandleTransparentUnionAttr(D, Attr, S);
2796 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002797 case AttributeList::AT_objc_exception:
2798 HandleObjCExceptionAttr(D, Attr, S);
2799 break;
John McCalld5313b02011-03-02 11:33:24 +00002800 case AttributeList::AT_objc_method_family:
2801 HandleObjCMethodFamilyAttr(D, Attr, S);
2802 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002803 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2804 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2805 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2806 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2807 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2808 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2809 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2810 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2811 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002812 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002813 // Just ignore
2814 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002815 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2816 HandleNoInstrumentFunctionAttr(D, Attr, S);
2817 break;
John McCall04a67a62010-02-05 21:31:56 +00002818 case AttributeList::AT_stdcall:
2819 case AttributeList::AT_cdecl:
2820 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002821 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002822 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002823 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002824 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00002825 case AttributeList::AT_opencl_kernel_function:
2826 HandleOpenCLKernelAttr(D, Attr, S);
2827 break;
Francois Pichet11542142010-12-19 06:50:37 +00002828 case AttributeList::AT_uuid:
2829 HandleUuidAttr(D, Attr, S);
2830 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002831 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002832 // Ask target about the attribute.
2833 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2834 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002835 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2836 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002837 break;
2838 }
2839}
2840
Peter Collingbourne60700392011-01-21 02:08:45 +00002841/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2842/// the attribute applies to decls. If the attribute is a type attribute, just
2843/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2844/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2845static void ProcessDeclAttribute(Scope *scope, Decl *D,
2846 const AttributeList &Attr, Sema &S,
2847 bool NonInheritable, bool Inheritable) {
2848 if (Attr.isInvalid())
2849 return;
2850
2851 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2852 // FIXME: Try to deal with other __declspec attributes!
2853 return;
2854
2855 if (NonInheritable)
2856 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2857
2858 if (Inheritable)
2859 ProcessInheritableDeclAttr(scope, D, Attr, S);
2860}
2861
Chris Lattner803d0802008-06-29 00:43:07 +00002862/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2863/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00002864void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00002865 const AttributeList *AttrList,
2866 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002867 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourne60700392011-01-21 02:08:45 +00002868 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002869 }
2870
2871 // GCC accepts
2872 // static int a9 __attribute__((weakref));
2873 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00002874 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002875 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002876 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002877 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002878 }
2879}
2880
Ryan Flynne25ff832009-07-30 03:15:39 +00002881/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2882/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002883NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002884 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002885 NamedDecl *NewD = 0;
2886 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2887 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2888 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002889 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002890 if (FD->getQualifier()) {
2891 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002892 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00002893 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002894 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2895 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2896 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002897 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002898 VD->getStorageClass(),
2899 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002900 if (VD->getQualifier()) {
2901 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002902 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00002903 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002904 }
2905 return NewD;
2906}
2907
2908/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2909/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002910void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002911 if (W.getUsed()) return; // only do this once
2912 W.setUsed(true);
2913 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2914 IdentifierInfo *NDId = ND->getIdentifier();
2915 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002916 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2917 NDId->getName()));
2918 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002919 WeakTopLevelDecl.push_back(NewD);
2920 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2921 // to insert Decl at TU scope, sorry.
2922 DeclContext *SavedContext = CurContext;
2923 CurContext = Context.getTranslationUnitDecl();
2924 PushOnScopeChains(NewD, S);
2925 CurContext = SavedContext;
2926 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002927 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002928 }
2929}
2930
Chris Lattner0744e5f2008-06-29 00:23:49 +00002931/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2932/// it, apply them to D. This is a bit tricky because PD can have attributes
2933/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00002934void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
2935 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00002936 // It's valid to "forward-declare" #pragma weak, in which case we
2937 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00002938 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00002939 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2940 if (IdentifierInfo *Id = ND->getIdentifier()) {
2941 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2942 = WeakUndeclaredIdentifiers.find(Id);
2943 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2944 WeakInfo W = I->second;
2945 DeclApplyPragmaWeak(S, ND, W);
2946 WeakUndeclaredIdentifiers[Id] = W;
2947 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002948 }
2949 }
2950 }
2951
Chris Lattner0744e5f2008-06-29 00:23:49 +00002952 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00002953 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00002954 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00002955
Chris Lattner0744e5f2008-06-29 00:23:49 +00002956 // Walk the declarator structure, applying decl attributes that were in a type
2957 // position to the decl itself. This handles cases like:
2958 // int *__attr__(x)** D;
2959 // when X is a decl attribute.
2960 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2961 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00002962 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00002963
Chris Lattner0744e5f2008-06-29 00:23:49 +00002964 // Finally, apply any attributes on the decl itself.
2965 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00002966 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002967}
John McCall54abf7d2009-11-04 02:18:39 +00002968
John McCalleee1d542011-02-14 07:13:47 +00002969// This duplicates a vector push_back but hides the need to know the
2970// size of the type.
2971void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
2972 assert(StackSize <= StackCapacity);
2973
2974 // Grow the stack if necessary.
2975 if (StackSize == StackCapacity) {
2976 unsigned newCapacity = 2 * StackCapacity + 2;
2977 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
2978 const char *oldBuffer = (const char*) Stack;
2979
2980 if (StackCapacity)
2981 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
2982
2983 delete[] oldBuffer;
2984 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
2985 StackCapacity = newCapacity;
2986 }
2987
2988 assert(StackSize < StackCapacity);
2989 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00002990}
2991
John McCalleee1d542011-02-14 07:13:47 +00002992void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
2993 Decl *decl) {
2994 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00002995
John McCalleee1d542011-02-14 07:13:47 +00002996 // Check the invariants.
2997 assert(DD.StackSize >= state.SavedStackSize);
2998 assert(state.SavedStackSize >= DD.ActiveStackBase);
2999 assert(DD.ParsingDepth > 0);
3000
3001 // Drop the parsing depth.
3002 DD.ParsingDepth--;
3003
3004 // If there are no active diagnostics, we're done.
3005 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003006 return;
3007
John McCall2f514482010-01-27 03:50:35 +00003008 // We only want to actually emit delayed diagnostics when we
3009 // successfully parsed a decl.
John McCalleee1d542011-02-14 07:13:47 +00003010 if (decl) {
3011 // We emit all the active diagnostics, not just those starting
3012 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003013 // decl spec and another for each declarator; in a decl group like:
3014 // deprecated_typedef foo, *bar, baz();
3015 // only the declarator pops will be passed decls. This is correct;
3016 // we really do need to consider delayed diagnostics from the decl spec
3017 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003018 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3019 DelayedDiagnostic &diag = DD.Stack[i];
3020 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003021 continue;
3022
John McCalleee1d542011-02-14 07:13:47 +00003023 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003024 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003025 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003026 break;
3027
3028 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003029 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003030 break;
3031 }
3032 }
3033 }
3034
John McCall58e6f342010-03-16 05:22:47 +00003035 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003036 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
3037 DD.Stack[i].destroy();
John McCall58e6f342010-03-16 05:22:47 +00003038
John McCalleee1d542011-02-14 07:13:47 +00003039 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003040}
3041
3042static bool isDeclDeprecated(Decl *D) {
3043 do {
3044 if (D->hasAttr<DeprecatedAttr>())
3045 return true;
3046 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3047 return false;
3048}
3049
John McCall9c3087b2010-08-26 02:13:20 +00003050void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003051 Decl *Ctx) {
3052 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003053 return;
3054
John McCall2f514482010-01-27 03:50:35 +00003055 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003056 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003057 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003058 << DD.getDeprecationDecl()->getDeclName()
3059 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003060 else
3061 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003062 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003063}
3064
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003065void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003066 SourceLocation Loc,
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003067 bool UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003068 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003069 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3070 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003071 return;
3072 }
3073
3074 // Otherwise, don't warn if our current context is deprecated.
3075 if (isDeclDeprecated(cast<Decl>(CurContext)))
3076 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003077 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003078 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3079 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003080 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003081 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003082 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
3083 else
3084 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
3085 }
John McCall54abf7d2009-11-04 02:18:39 +00003086}