blob: 9c9846ebf56aa2dd32dae23f676604454c96f36a [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
258 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
259}
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
275 S.Diag(Attr.getLoc(), diag::err_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))) {
290 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
291 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
Peter Collingbournea96022c2011-01-02 19:53:19 +0000589static bool isStaticVarOrStaticFunction(Decl *D) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000590 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000591 return VD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000592 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000593 return FD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000594 return false;
595}
596
597static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
598 // Check the attribute arguments.
599 if (Attr.getNumArgs() > 1) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
601 return;
602 }
603
604 // gcc rejects
605 // class c {
606 // static int a __attribute__((weakref ("v2")));
607 // static int b() __attribute__((weakref ("f3")));
608 // };
609 // and ignores the attributes of
610 // void f(void) {
611 // static int a __attribute__((weakref ("v2")));
612 // }
613 // we reject them
Sebastian Redl7a126a42010-08-31 00:36:30 +0000614 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
615 if (!Ctx->isFileContext()) {
616 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
617 dyn_cast<NamedDecl>(d)->getNameAsString();
618 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000619 }
620
621 // The GCC manual says
622 //
623 // At present, a declaration to which `weakref' is attached can only
624 // be `static'.
625 //
626 // It also says
627 //
628 // Without a TARGET,
629 // given as an argument to `weakref' or to `alias', `weakref' is
630 // equivalent to `weak'.
631 //
632 // gcc 4.4.1 will accept
633 // int a7 __attribute__((weakref));
634 // as
635 // int a7 __attribute__((weak));
636 // This looks like a bug in gcc. We reject that for now. We should revisit
637 // it if this behaviour is actually used.
638
Peter Collingbournea96022c2011-01-02 19:53:19 +0000639 if (!isStaticVarOrStaticFunction(d)) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000640 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
641 dyn_cast<NamedDecl>(d)->getNameAsString();
642 return;
643 }
644
645 // GCC rejects
646 // static ((alias ("y"), weakref)).
647 // Should we? How to check that weakref is before or after alias?
648
649 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000650 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000651 Arg = Arg->IgnoreParenCasts();
652 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
653
654 if (Str == 0 || Str->isWide()) {
655 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
656 << "weakref" << 1;
657 return;
658 }
659 // GCC will accept anything as the argument of weakref. Should we
660 // check for an existing decl?
Eric Christopherf48f3672010-12-01 22:13:54 +0000661 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
662 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000663 }
664
Sean Huntcf807c42010-08-18 23:23:40 +0000665 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000666}
667
Chris Lattner803d0802008-06-29 00:43:07 +0000668static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000669 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000670 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000672 return;
673 }
Mike Stumpbf916502009-07-24 19:02:52 +0000674
Peter Collingbourne7a730022010-11-23 20:45:58 +0000675 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000676 Arg = Arg->IgnoreParenCasts();
677 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000678
Chris Lattner6b6b5372008-06-26 18:38:35 +0000679 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000680 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000681 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000682 return;
683 }
Mike Stumpbf916502009-07-24 19:02:52 +0000684
Rafael Espindolaf5fe2922010-12-07 15:23:23 +0000685 if (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin) {
686 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
687 return;
688 }
689
Chris Lattner6b6b5372008-06-26 18:38:35 +0000690 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000691
Eric Christopherf48f3672010-12-01 22:13:54 +0000692 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
693 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000694}
695
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000696static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000697 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000698 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000699 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000700 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000701 return;
702 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000703
Chris Lattnerc5197432009-04-14 17:02:11 +0000704 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000705 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000706 << Attr.getName() << 0 /*function*/;
707 return;
708 }
709
710 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
711}
712
713static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
714 Sema &S) {
715 // Check the attribute arguments.
716 if (Attr.getNumArgs() != 0) {
717 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
718 return;
719 }
720
721 if (!isa<FunctionDecl>(d)) {
722 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
723 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000724 return;
725 }
Mike Stumpbf916502009-07-24 19:02:52 +0000726
Sean Huntcf807c42010-08-18 23:23:40 +0000727 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000728}
729
Ryan Flynn76168e22009-08-09 20:07:29 +0000730static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000731 // Check the attribute arguments.
Ryan Flynn76168e22009-08-09 20:07:29 +0000732 if (Attr.getNumArgs() != 0) {
733 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
734 return;
735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000737 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000738 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000739 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000740 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000741 return;
742 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000743 }
744
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000745 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000746}
747
Dan Gohman34c26302010-11-17 00:03:07 +0000748static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
749 // check the attribute arguments.
750 if (Attr.getNumArgs() != 0) {
751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
752 return;
753 }
754
Dan Gohman34c26302010-11-17 00:03:07 +0000755 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
756}
757
Eric Christophera6cf1e72010-12-02 02:45:55 +0000758static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
759 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-12-03 06:58:14 +0000760 if (isa<VarDecl>(d))
761 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
762 else
763 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
764 << Attr.getName() << 12 /* variable */;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000765}
766
767static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
768 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-12-03 06:58:14 +0000769 if (isa<VarDecl>(d))
770 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
771 else
772 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
773 << Attr.getName() << 12 /* variable */;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000774}
775
John McCall711c52b2011-01-05 12:14:39 +0000776static void HandleNoReturnAttr(Decl *d, const AttributeList &attr, Sema &S) {
777 if (hasDeclarator(d)) return;
778
779 if (S.CheckNoReturnAttr(attr)) return;
780
781 if (!isa<ObjCMethodDecl>(d)) {
782 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
783 << attr.getName() << 0 /*function*/;
784 return;
785 }
786
787 d->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
788}
789
790bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
791 if (attr.getNumArgs() != 0) {
792 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
793 attr.setInvalid();
794 return true;
795 }
796
797 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +0000798}
799
800static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
801 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000802
803 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
804 // because 'analyzer_noreturn' does not impact the type.
805
806 if (Attr.getNumArgs() != 0) {
807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
808 return;
809 }
810
811 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
812 ValueDecl *VD = dyn_cast<ValueDecl>(d);
813 if (VD == 0 || (!VD->getType()->isBlockPointerType()
814 && !VD->getType()->isFunctionPointerType())) {
815 S.Diag(Attr.getLoc(),
816 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
817 : diag::warn_attribute_wrong_decl_type)
818 << Attr.getName() << 0 /*function*/;
819 return;
820 }
821 }
822
823 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000824}
825
John Thompson35cc9622010-08-09 21:53:52 +0000826// PS3 PPU-specific.
827static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
828 Sema &S) {
829/*
830 Returning a Vector Class in Registers
831
Eric Christopherf48f3672010-12-01 22:13:54 +0000832 According to the PPU ABI specifications, a class with a single member of
833 vector type is returned in memory when used as the return value of a function.
834 This results in inefficient code when implementing vector classes. To return
835 the value in a single vector register, add the vecreturn attribute to the
836 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +0000837
838 Example:
839
840 struct Vector
841 {
842 __vector float xyzw;
843 } __attribute__((vecreturn));
844
845 Vector Add(Vector lhs, Vector rhs)
846 {
847 Vector result;
848 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
849 return result; // This will be returned in a register
850 }
851*/
John Thompson01add592010-09-18 01:12:07 +0000852 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
854 << Attr.getName() << 9 /*class*/;
855 return;
856 }
857
858 if (d->getAttr<VecReturnAttr>()) {
859 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
860 return;
861 }
862
John Thompson01add592010-09-18 01:12:07 +0000863 RecordDecl *record = cast<RecordDecl>(d);
864 int count = 0;
865
866 if (!isa<CXXRecordDecl>(record)) {
867 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
868 return;
869 }
870
871 if (!cast<CXXRecordDecl>(record)->isPOD()) {
872 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
873 return;
874 }
875
Eric Christopherf48f3672010-12-01 22:13:54 +0000876 for (RecordDecl::field_iterator iter = record->field_begin();
877 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +0000878 if ((count == 1) || !iter->getType()->isVectorType()) {
879 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
880 return;
881 }
882 count++;
883 }
884
Sean Huntcf807c42010-08-18 23:23:40 +0000885 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000886}
887
Sean Huntbbd37c62009-11-21 08:43:09 +0000888static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
889 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
890 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000891 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000892 return;
893 }
894 // FIXME: Actually store the attribute on the declaration
895}
896
Ted Kremenek73798892008-07-25 04:39:19 +0000897static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
898 // check the attribute arguments.
899 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000901 return;
902 }
Mike Stumpbf916502009-07-24 19:02:52 +0000903
John McCallaec58602010-03-31 02:47:45 +0000904 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
905 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000906 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000907 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000908 return;
909 }
Mike Stumpbf916502009-07-24 19:02:52 +0000910
Sean Huntcf807c42010-08-18 23:23:40 +0000911 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000912}
913
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000914static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
915 // check the attribute arguments.
916 if (Attr.getNumArgs() != 0) {
917 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
918 return;
919 }
Mike Stumpbf916502009-07-24 19:02:52 +0000920
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000921 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000922 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000923 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
924 return;
925 }
926 } else if (!isFunctionOrMethod(d)) {
927 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000928 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000929 return;
930 }
Mike Stumpbf916502009-07-24 19:02:52 +0000931
Sean Huntcf807c42010-08-18 23:23:40 +0000932 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000933}
934
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000935static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
936 // check the attribute arguments.
937 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000938 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
939 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000940 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000941 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000942
943 int priority = 65535; // FIXME: Do not hardcode such constants.
944 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000945 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000946 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000947 if (E->isTypeDependent() || E->isValueDependent() ||
948 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000949 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000950 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000951 return;
952 }
953 priority = Idx.getZExtValue();
954 }
Mike Stumpbf916502009-07-24 19:02:52 +0000955
Chris Lattnerc5197432009-04-14 17:02:11 +0000956 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000957 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000958 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000959 return;
960 }
961
Eric Christopherf48f3672010-12-01 22:13:54 +0000962 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
963 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000964}
965
966static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
967 // check the attribute arguments.
968 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000969 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
970 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000971 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000972 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000973
974 int priority = 65535; // FIXME: Do not hardcode such constants.
975 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000976 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000977 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000978 if (E->isTypeDependent() || E->isValueDependent() ||
979 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000980 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000981 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000982 return;
983 }
984 priority = Idx.getZExtValue();
985 }
Mike Stumpbf916502009-07-24 19:02:52 +0000986
Anders Carlsson6782fc62008-08-22 22:10:48 +0000987 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000988 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000989 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000990 return;
991 }
992
Eric Christopherf48f3672010-12-01 22:13:54 +0000993 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
994 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000995}
996
Chris Lattner803d0802008-06-29 00:43:07 +0000997static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000998 // check the attribute arguments.
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000999 int noArgs = Attr.getNumArgs();
1000 if (noArgs > 1) {
1001 S.Diag(Attr.getLoc(),
1002 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001003 return;
1004 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001005 // Handle the case where deprecated attribute has a text message.
1006 StringLiteral *SE;
1007 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001008 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001009 SE = dyn_cast<StringLiteral>(ArgExpr);
1010 if (!SE) {
1011 S.Diag(ArgExpr->getLocStart(),
1012 diag::err_attribute_not_string) << "deprecated";
1013 return;
1014 }
1015 }
1016 else
1017 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpbf916502009-07-24 19:02:52 +00001018
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001019 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
1020 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001021}
1022
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001023static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1024 // check the attribute arguments.
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001025 int noArgs = Attr.getNumArgs();
1026 if (noArgs > 1) {
Eric Christopherf48f3672010-12-01 22:13:54 +00001027 S.Diag(Attr.getLoc(),
1028 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001029 return;
1030 }
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001031 // Handle the case where unavailable attribute has a text message.
1032 StringLiteral *SE;
1033 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001034 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001035 SE = dyn_cast<StringLiteral>(ArgExpr);
1036 if (!SE) {
1037 S.Diag(ArgExpr->getLocStart(),
1038 diag::err_attribute_not_string) << "unavailable";
1039 return;
1040 }
1041 }
1042 else
1043 SE = StringLiteral::CreateEmpty(S.Context, 1);
1044 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
1045 SE->getString()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001046}
1047
Chris Lattner803d0802008-06-29 00:43:07 +00001048static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001049 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001050 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001051 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001052 return;
1053 }
Mike Stumpbf916502009-07-24 19:02:52 +00001054
Peter Collingbourne7a730022010-11-23 20:45:58 +00001055 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001056 Arg = Arg->IgnoreParenCasts();
1057 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001058
Chris Lattner6b6b5372008-06-26 18:38:35 +00001059 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001060 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001061 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001062 return;
1063 }
Mike Stumpbf916502009-07-24 19:02:52 +00001064
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001065 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001066 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001067
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001068 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001069 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001070 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001071 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001072 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001073 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001074 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001075 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001076 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001077 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001078 return;
1079 }
Mike Stumpbf916502009-07-24 19:02:52 +00001080
Sean Huntcf807c42010-08-18 23:23:40 +00001081 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001082}
1083
Chris Lattner0db29ec2009-02-14 08:09:34 +00001084static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1085 Sema &S) {
1086 if (Attr.getNumArgs() != 0) {
1087 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1088 return;
1089 }
Mike Stumpbf916502009-07-24 19:02:52 +00001090
Chris Lattner0db29ec2009-02-14 08:09:34 +00001091 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1092 if (OCI == 0) {
1093 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1094 return;
1095 }
Mike Stumpbf916502009-07-24 19:02:52 +00001096
Sean Huntcf807c42010-08-18 23:23:40 +00001097 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001098}
1099
1100static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001101 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001102 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001103 return;
1104 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001105 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001106 QualType T = TD->getUnderlyingType();
1107 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001108 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001109 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1110 return;
1111 }
1112 }
Sean Huntcf807c42010-08-18 23:23:40 +00001113 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001114}
1115
Mike Stumpbf916502009-07-24 19:02:52 +00001116static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001117HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1118 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001119 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001120 return;
1121 }
1122
1123 if (!isa<FunctionDecl>(D)) {
1124 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1125 return;
1126 }
1127
Sean Huntcf807c42010-08-18 23:23:40 +00001128 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001129}
1130
Steve Naroff9eae5762008-09-18 16:44:58 +00001131static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001132 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001133 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001134 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001135 return;
1136 }
Mike Stumpbf916502009-07-24 19:02:52 +00001137
Steve Naroff9eae5762008-09-18 16:44:58 +00001138 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001139 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001140 return;
1141 }
Mike Stumpbf916502009-07-24 19:02:52 +00001142
Sean Huntcf807c42010-08-18 23:23:40 +00001143 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001144 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001145 type = BlocksAttr::ByRef;
1146 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001147 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001148 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001149 return;
1150 }
Mike Stumpbf916502009-07-24 19:02:52 +00001151
Sean Huntcf807c42010-08-18 23:23:40 +00001152 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001153}
1154
Anders Carlsson77091822008-10-05 18:05:59 +00001155static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1156 // check the attribute arguments.
1157 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001158 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1159 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001160 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001161 }
1162
Anders Carlsson77091822008-10-05 18:05:59 +00001163 int sentinel = 0;
1164 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001165 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001166 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001167 if (E->isTypeDependent() || E->isValueDependent() ||
1168 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001170 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001171 return;
1172 }
1173 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001174
Anders Carlsson77091822008-10-05 18:05:59 +00001175 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001176 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1177 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001178 return;
1179 }
1180 }
1181
1182 int nullPos = 0;
1183 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001184 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001185 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001186 if (E->isTypeDependent() || E->isValueDependent() ||
1187 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001189 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001190 return;
1191 }
1192 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001193
Anders Carlsson77091822008-10-05 18:05:59 +00001194 if (nullPos > 1 || nullPos < 0) {
1195 // FIXME: This error message could be improved, it would be nice
1196 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001197 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1198 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001199 return;
1200 }
1201 }
1202
1203 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001204 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001205 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001206
Chris Lattner897cd902009-03-17 23:03:47 +00001207 if (isa<FunctionNoProtoType>(FT)) {
1208 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1209 return;
1210 }
Mike Stumpbf916502009-07-24 19:02:52 +00001211
Chris Lattner897cd902009-03-17 23:03:47 +00001212 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001213 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001214 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001215 }
Anders Carlsson77091822008-10-05 18:05:59 +00001216 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1217 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001218 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001219 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001220 }
1221 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001222 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1223 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001224 ;
1225 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001226 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001227 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001228 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherf48f3672010-12-01 22:13:54 +00001229 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001230 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001231 int m = Ty->isFunctionPointerType() ? 0 : 1;
1232 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001233 return;
1234 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001235 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001236 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001237 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001238 return;
1239 }
Anders Carlsson77091822008-10-05 18:05:59 +00001240 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001241 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001242 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001243 return;
1244 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001245 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1246 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001247}
1248
Chris Lattner026dc962009-02-14 07:37:35 +00001249static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1250 // check the attribute arguments.
1251 if (Attr.getNumArgs() != 0) {
1252 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1253 return;
1254 }
1255
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001256 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001257 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001258 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001259 return;
1260 }
Mike Stumpbf916502009-07-24 19:02:52 +00001261
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001262 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1263 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1264 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001265 return;
1266 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001267 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1268 if (MD->getResultType()->isVoidType()) {
1269 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1270 << Attr.getName() << 1;
1271 return;
1272 }
1273
Sean Huntcf807c42010-08-18 23:23:40 +00001274 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001275}
1276
1277static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001278 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001279 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001280 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001281 return;
1282 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001283
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001284 /* weak only applies to non-static declarations */
Peter Collingbournea96022c2011-01-02 19:53:19 +00001285 if (isStaticVarOrStaticFunction(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1287 dyn_cast<NamedDecl>(D)->getNameAsString();
1288 return;
1289 }
1290
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001291 // TODO: could also be applied to methods?
1292 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1293 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001294 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001295 return;
1296 }
Mike Stumpbf916502009-07-24 19:02:52 +00001297
Sean Huntcf807c42010-08-18 23:23:40 +00001298 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001299}
1300
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001301static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1302 // check the attribute arguments.
1303 if (Attr.getNumArgs() != 0) {
1304 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1305 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001306 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001307
1308 // weak_import only applies to variable & function declarations.
1309 bool isDef = false;
1310 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1311 isDef = (!VD->hasExternalStorage() || VD->getInit());
1312 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001313 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001314 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1315 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001316 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001317 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001318 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001319 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001320 !isa<ObjCInterfaceDecl>(D))
1321 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001322 << Attr.getName() << 2 /*variable and function*/;
1323 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001324 }
1325
1326 // Merge should handle any subsequent violations.
1327 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001328 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001329 diag::warn_attribute_weak_import_invalid_on_definition)
1330 << "weak_import" << 2 /*variable and function*/;
1331 return;
1332 }
1333
Sean Huntcf807c42010-08-18 23:23:40 +00001334 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001335}
1336
Nate Begeman6f3d8382009-06-26 06:32:41 +00001337static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1338 Sema &S) {
1339 // Attribute has 3 arguments.
1340 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001341 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001342 return;
1343 }
1344
1345 unsigned WGSize[3];
1346 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001347 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001348 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001349 if (E->isTypeDependent() || E->isValueDependent() ||
1350 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1352 << "reqd_work_group_size" << E->getSourceRange();
1353 return;
1354 }
1355 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1356 }
Sean Huntcf807c42010-08-18 23:23:40 +00001357 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1358 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001359 WGSize[2]));
1360}
1361
Chris Lattner026dc962009-02-14 07:37:35 +00001362static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001363 // Attribute has no arguments.
1364 if (Attr.getNumArgs() != 1) {
1365 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1366 return;
1367 }
1368
1369 // Make sure that there is a string literal as the sections's single
1370 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001371 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001372 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001373 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001374 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001375 return;
1376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Chris Lattner797c3c42009-08-10 19:03:04 +00001378 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001379 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001380 if (!Error.empty()) {
1381 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1382 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001383 return;
1384 }
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001386 // This attribute cannot be applied to local variables.
1387 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1388 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1389 return;
1390 }
1391
Eric Christopherf48f3672010-12-01 22:13:54 +00001392 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1393 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001394}
1395
Chris Lattner6b6b5372008-06-26 18:38:35 +00001396
Chris Lattner803d0802008-06-29 00:43:07 +00001397static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001398 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001399 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001401 return;
1402 }
Mike Stumpbf916502009-07-24 19:02:52 +00001403
Sean Huntcf807c42010-08-18 23:23:40 +00001404 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001405}
1406
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001407static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1408 // check the attribute arguments.
1409 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001410 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001411 return;
1412 }
Mike Stumpbf916502009-07-24 19:02:52 +00001413
Sean Huntcf807c42010-08-18 23:23:40 +00001414 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001415}
1416
1417static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1418 // check the attribute arguments.
1419 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001421 return;
1422 }
Mike Stumpbf916502009-07-24 19:02:52 +00001423
Sean Huntcf807c42010-08-18 23:23:40 +00001424 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001425}
1426
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001427static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001428 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1430 return;
1431 }
Mike Stumpbf916502009-07-24 19:02:52 +00001432
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001433 if (Attr.getNumArgs() != 0) {
1434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1435 return;
1436 }
Mike Stumpbf916502009-07-24 19:02:52 +00001437
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001438 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001439
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001440 if (!VD || !VD->hasLocalStorage()) {
1441 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1442 return;
1443 }
Mike Stumpbf916502009-07-24 19:02:52 +00001444
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001445 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001446 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001447 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001448 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1449 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001450 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001451 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001452 Attr.getParameterName();
1453 return;
1454 }
Mike Stumpbf916502009-07-24 19:02:52 +00001455
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001456 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1457 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001458 S.Diag(Attr.getParameterLoc(),
1459 diag::err_attribute_cleanup_arg_not_function)
1460 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001461 return;
1462 }
1463
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001464 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001465 S.Diag(Attr.getParameterLoc(),
1466 diag::err_attribute_cleanup_func_must_take_one_arg)
1467 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001468 return;
1469 }
Mike Stumpbf916502009-07-24 19:02:52 +00001470
Anders Carlsson89941c12009-02-07 23:16:50 +00001471 // We're currently more strict than GCC about what function types we accept.
1472 // If this ever proves to be a problem it should be easy to fix.
1473 QualType Ty = S.Context.getPointerType(VD->getType());
1474 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall1c23e912010-11-16 02:32:08 +00001475 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001476 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001477 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1478 Attr.getParameterName() << ParamTy << Ty;
1479 return;
1480 }
Mike Stumpbf916502009-07-24 19:02:52 +00001481
Sean Huntcf807c42010-08-18 23:23:40 +00001482 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001483 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001484}
1485
Mike Stumpbf916502009-07-24 19:02:52 +00001486/// Handle __attribute__((format_arg((idx)))) attribute based on
1487/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1488static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001489 if (Attr.getNumArgs() != 1) {
1490 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1491 return;
1492 }
1493 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1494 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1495 << Attr.getName() << 0 /*function*/;
1496 return;
1497 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001498
1499 // In C++ the implicit 'this' function parameter also counts, and they are
1500 // counted from one.
1501 bool HasImplicitThisParam = isInstanceMethod(d);
1502 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001503 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001504
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001505 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001506 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001507 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001508 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1509 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001510 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1511 << "format" << 2 << IdxExpr->getSourceRange();
1512 return;
1513 }
Mike Stumpbf916502009-07-24 19:02:52 +00001514
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001515 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1516 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1517 << "format" << 2 << IdxExpr->getSourceRange();
1518 return;
1519 }
Mike Stumpbf916502009-07-24 19:02:52 +00001520
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001521 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001522
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001523 if (HasImplicitThisParam) {
1524 if (ArgIdx == 0) {
1525 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1526 << "format_arg" << IdxExpr->getSourceRange();
1527 return;
1528 }
1529 ArgIdx--;
1530 }
1531
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001532 // make sure the format string is really a string
1533 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001534
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001535 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1536 if (not_nsstring_type &&
1537 !isCFStringType(Ty, S.Context) &&
1538 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001539 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001540 // FIXME: Should highlight the actual expression that has the wrong type.
1541 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001542 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001543 << IdxExpr->getSourceRange();
1544 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001545 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001546 Ty = getFunctionOrMethodResultType(d);
1547 if (!isNSStringType(Ty, S.Context) &&
1548 !isCFStringType(Ty, S.Context) &&
1549 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001550 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001551 // FIXME: Should highlight the actual expression that has the wrong type.
1552 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001553 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001554 << IdxExpr->getSourceRange();
1555 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001556 }
1557
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001558 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1559 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001560}
1561
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001562enum FormatAttrKind {
1563 CFStringFormat,
1564 NSStringFormat,
1565 StrftimeFormat,
1566 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001567 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001568 InvalidFormat
1569};
1570
1571/// getFormatAttrKind - Map from format attribute names to supported format
1572/// types.
1573static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1574 // Check for formats that get handled specially.
1575 if (Format == "NSString")
1576 return NSStringFormat;
1577 if (Format == "CFString")
1578 return CFStringFormat;
1579 if (Format == "strftime")
1580 return StrftimeFormat;
1581
1582 // Otherwise, check for supported formats.
1583 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1584 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1585 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1586 Format == "zcmn_err")
1587 return SupportedFormat;
1588
Duncan Sandsbc525952010-03-23 14:44:19 +00001589 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1590 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001591 return IgnoredFormat;
1592
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001593 return InvalidFormat;
1594}
1595
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001596/// Handle __attribute__((init_priority(priority))) attributes based on
1597/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1598static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1599 Sema &S) {
1600 if (!S.getLangOptions().CPlusPlus) {
1601 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1602 return;
1603 }
1604
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001605 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1606 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1607 Attr.setInvalid();
1608 return;
1609 }
1610 QualType T = dyn_cast<VarDecl>(d)->getType();
1611 if (S.Context.getAsArrayType(T))
1612 T = S.Context.getBaseElementType(T);
1613 if (!T->getAs<RecordType>()) {
1614 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1615 Attr.setInvalid();
1616 return;
1617 }
1618
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001619 if (Attr.getNumArgs() != 1) {
1620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1621 Attr.setInvalid();
1622 return;
1623 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001624 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001625
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001626 llvm::APSInt priority(32);
1627 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1628 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1629 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1630 << "init_priority" << priorityExpr->getSourceRange();
1631 Attr.setInvalid();
1632 return;
1633 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001634 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001635 if (prioritynum < 101 || prioritynum > 65535) {
1636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1637 << priorityExpr->getSourceRange();
1638 Attr.setInvalid();
1639 return;
1640 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001641 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1642 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001643}
1644
Mike Stumpbf916502009-07-24 19:02:52 +00001645/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1646/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001647static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001648
Chris Lattner545dd342008-06-28 23:36:30 +00001649 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001650 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001651 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001652 return;
1653 }
1654
Chris Lattner545dd342008-06-28 23:36:30 +00001655 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001657 return;
1658 }
1659
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001660 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001661 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001662 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001663 return;
1664 }
1665
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001666 // In C++ the implicit 'this' function parameter also counts, and they are
1667 // counted from one.
1668 bool HasImplicitThisParam = isInstanceMethod(d);
1669 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001670 unsigned FirstIdx = 1;
1671
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001672 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001673
1674 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001675 if (Format.startswith("__") && Format.endswith("__"))
1676 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001677
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001678 // Check for supported formats.
1679 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001680
1681 if (Kind == IgnoredFormat)
1682 return;
1683
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001684 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001685 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001686 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001687 return;
1688 }
1689
1690 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001691 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001692 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001693 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1694 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001696 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001697 return;
1698 }
1699
1700 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001701 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001702 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001703 return;
1704 }
1705
1706 // FIXME: Do we need to bounds check?
1707 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001708
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001709 if (HasImplicitThisParam) {
1710 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001711 S.Diag(Attr.getLoc(),
1712 diag::err_format_attribute_implicit_this_format_string)
1713 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001714 return;
1715 }
1716 ArgIdx--;
1717 }
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Chris Lattner6b6b5372008-06-26 18:38:35 +00001719 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001720 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001721
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001722 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001723 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001724 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1725 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001726 return;
1727 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001728 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001729 // FIXME: do we need to check if the type is NSString*? What are the
1730 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001731 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001732 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001733 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1734 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001735 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001736 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001737 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001738 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001739 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001740 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1741 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001742 return;
1743 }
1744
1745 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001746 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001747 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001748 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1749 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001751 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001752 return;
1753 }
1754
1755 // check if the function is variadic if the 3rd argument non-zero
1756 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001757 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001758 ++NumArgs; // +1 for ...
1759 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001760 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001761 return;
1762 }
1763 }
1764
Chris Lattner3c73c412008-11-19 08:23:25 +00001765 // strftime requires FirstArg to be 0 because it doesn't read from any
1766 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001767 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001768 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001769 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1770 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001771 return;
1772 }
1773 // if 0 it disables parameter checking (to use with e.g. va_list)
1774 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001775 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001776 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001777 return;
1778 }
1779
Sean Huntcf807c42010-08-18 23:23:40 +00001780 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1781 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001782 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001783}
1784
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001785static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1786 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001787 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001788 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001789 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001790 return;
1791 }
1792
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001793 // Try to find the underlying union declaration.
1794 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001795 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001796 if (TD && TD->getUnderlyingType()->isUnionType())
1797 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1798 else
1799 RD = dyn_cast<RecordDecl>(d);
1800
1801 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001802 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001803 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001804 return;
1805 }
1806
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001807 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001808 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001809 diag::warn_transparent_union_attribute_not_definition);
1810 return;
1811 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001812
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001813 RecordDecl::field_iterator Field = RD->field_begin(),
1814 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001815 if (Field == FieldEnd) {
1816 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1817 return;
1818 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001819
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001820 FieldDecl *FirstField = *Field;
1821 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001822 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001823 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001824 diag::warn_transparent_union_attribute_floating)
1825 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001826 return;
1827 }
1828
1829 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1830 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1831 for (; Field != FieldEnd; ++Field) {
1832 QualType FieldType = Field->getType();
1833 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1834 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1835 // Warn if we drop the attribute.
1836 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001837 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001838 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001839 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001840 diag::warn_transparent_union_attribute_field_size_align)
1841 << isSize << Field->getDeclName() << FieldBits;
1842 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001843 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001844 diag::note_transparent_union_first_field_size_align)
1845 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001846 return;
1847 }
1848 }
1849
Sean Huntcf807c42010-08-18 23:23:40 +00001850 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001851}
1852
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001853static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001854 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001855 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001856 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001857 return;
1858 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001859 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001860 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001861
Chris Lattner6b6b5372008-06-26 18:38:35 +00001862 // Make sure that there is a string literal as the annotation's single
1863 // argument.
1864 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001865 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001866 return;
1867 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001868 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1869 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001870}
1871
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001872static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001873 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001874 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001875 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001876 return;
1877 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001878
1879 //FIXME: The C++0x version of this attribute has more limited applicabilty
1880 // than GNU's, and should error out when it is used to specify a
1881 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001882
Chris Lattner545dd342008-06-28 23:36:30 +00001883 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001884 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001885 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001886 }
Mike Stumpbf916502009-07-24 19:02:52 +00001887
Peter Collingbourne7a730022010-11-23 20:45:58 +00001888 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001889}
1890
1891void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1892 if (E->isTypeDependent() || E->isValueDependent()) {
1893 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001894 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001895 return;
1896 }
1897
Sean Huntcf807c42010-08-18 23:23:40 +00001898 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001899 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001900 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1901 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1902 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001903 return;
1904 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001905 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001906 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1907 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001908 return;
1909 }
1910
Sean Huntcf807c42010-08-18 23:23:40 +00001911 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1912}
1913
1914void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1915 // FIXME: Cache the number on the Attr object if non-dependent?
1916 // FIXME: Perform checking of type validity
1917 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1918 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001919}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001920
Mike Stumpbf916502009-07-24 19:02:52 +00001921/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1922/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001923///
Mike Stumpbf916502009-07-24 19:02:52 +00001924/// Despite what would be logical, the mode attribute is a decl attribute, not a
1925/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1926/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001927static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001928 // This attribute isn't documented, but glibc uses it. It changes
1929 // the width of an int or unsigned int to the specified size.
1930
1931 // Check that there aren't any arguments
1932 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001934 return;
1935 }
1936
1937 IdentifierInfo *Name = Attr.getParameterName();
1938 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001939 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001940 return;
1941 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001942
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001943 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001944
1945 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001946 if (Str.startswith("__") && Str.endswith("__"))
1947 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001948
1949 unsigned DestWidth = 0;
1950 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001951 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001952 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001953 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001954 switch (Str[0]) {
1955 case 'Q': DestWidth = 8; break;
1956 case 'H': DestWidth = 16; break;
1957 case 'S': DestWidth = 32; break;
1958 case 'D': DestWidth = 64; break;
1959 case 'X': DestWidth = 96; break;
1960 case 'T': DestWidth = 128; break;
1961 }
1962 if (Str[1] == 'F') {
1963 IntegerMode = false;
1964 } else if (Str[1] == 'C') {
1965 IntegerMode = false;
1966 ComplexMode = true;
1967 } else if (Str[1] != 'I') {
1968 DestWidth = 0;
1969 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001970 break;
1971 case 4:
1972 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1973 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001974 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001975 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001976 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001977 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001978 break;
1979 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001980 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001981 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001982 break;
1983 }
1984
1985 QualType OldTy;
1986 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1987 OldTy = TD->getUnderlyingType();
1988 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1989 OldTy = VD->getType();
1990 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001991 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1992 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001993 return;
1994 }
Eli Friedman73397492009-03-03 06:41:03 +00001995
John McCall183700f2009-09-21 23:43:11 +00001996 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001997 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1998 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001999 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002000 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2001 } else if (ComplexMode) {
2002 if (!OldTy->isComplexType())
2003 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2004 } else {
2005 if (!OldTy->isFloatingType())
2006 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2007 }
2008
Mike Stump390b4cc2009-05-16 07:39:55 +00002009 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2010 // and friends, at least with glibc.
2011 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2012 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002013 // FIXME: Make sure floating-point mappings are accurate
2014 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002015 QualType NewTy;
2016 switch (DestWidth) {
2017 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002018 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002019 return;
2020 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002021 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002022 return;
2023 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002024 if (!IntegerMode) {
2025 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2026 return;
2027 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002028 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002029 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002030 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002031 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002032 break;
2033 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002034 if (!IntegerMode) {
2035 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2036 return;
2037 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002038 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002039 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002040 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002041 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002042 break;
2043 case 32:
2044 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002045 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002046 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002047 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002048 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002049 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002050 break;
2051 case 64:
2052 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002053 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002054 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002055 if (S.Context.Target.getLongWidth() == 64)
2056 NewTy = S.Context.LongTy;
2057 else
2058 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002059 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002060 if (S.Context.Target.getLongWidth() == 64)
2061 NewTy = S.Context.UnsignedLongTy;
2062 else
2063 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002064 break;
Eli Friedman73397492009-03-03 06:41:03 +00002065 case 96:
2066 NewTy = S.Context.LongDoubleTy;
2067 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002068 case 128:
2069 if (!IntegerMode) {
2070 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2071 return;
2072 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002073 if (OldTy->isSignedIntegerType())
2074 NewTy = S.Context.Int128Ty;
2075 else
2076 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002077 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002078 }
2079
Eli Friedman73397492009-03-03 06:41:03 +00002080 if (ComplexMode) {
2081 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002082 }
2083
2084 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002085 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2086 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002087 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002088 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002089 cast<ValueDecl>(D)->setType(NewTy);
2090}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002091
Mike Stump1feade82009-08-26 22:31:08 +00002092static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002093 // check the attribute arguments.
2094 if (Attr.getNumArgs() > 0) {
2095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2096 return;
2097 }
Anders Carlssone896d982009-02-13 08:11:52 +00002098
Anders Carlsson5bab7882009-02-19 19:16:48 +00002099 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002100 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002101 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00002102 return;
2103 }
Mike Stumpbf916502009-07-24 19:02:52 +00002104
Sean Huntcf807c42010-08-18 23:23:40 +00002105 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002106}
2107
Mike Stump1feade82009-08-26 22:31:08 +00002108static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002109 // check the attribute arguments.
2110 if (Attr.getNumArgs() != 0) {
2111 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2112 return;
2113 }
Mike Stumpbf916502009-07-24 19:02:52 +00002114
Chris Lattnerc5197432009-04-14 17:02:11 +00002115 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002116 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002117 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002118 return;
2119 }
Mike Stumpbf916502009-07-24 19:02:52 +00002120
Sean Huntcf807c42010-08-18 23:23:40 +00002121 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002122}
2123
Chris Lattner7255a2d2010-06-22 00:03:40 +00002124static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2125 Sema &S) {
2126 // check the attribute arguments.
2127 if (Attr.getNumArgs() != 0) {
2128 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2129 return;
2130 }
2131
2132 if (!isa<FunctionDecl>(d)) {
2133 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2134 << Attr.getName() << 0 /*function*/;
2135 return;
2136 }
2137
Eric Christopherf48f3672010-12-01 22:13:54 +00002138 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2139 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002140}
2141
Peter Collingbourneced76712010-12-01 03:15:31 +00002142static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2143 if (S.LangOpts.CUDA) {
2144 // check the attribute arguments.
2145 if (Attr.getNumArgs() != 0) {
2146 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2147 return;
2148 }
2149
2150 if (!isa<VarDecl>(d)) {
2151 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2152 << Attr.getName() << 12 /*variable*/;
2153 return;
2154 }
2155
2156 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2157 } else {
2158 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2159 }
2160}
2161
2162static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2163 if (S.LangOpts.CUDA) {
2164 // check the attribute arguments.
2165 if (Attr.getNumArgs() != 0) {
2166 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2167 return;
2168 }
2169
2170 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2171 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2172 << Attr.getName() << 2 /*variable and function*/;
2173 return;
2174 }
2175
2176 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2177 } else {
2178 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2179 }
2180}
2181
2182static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2183 if (S.LangOpts.CUDA) {
2184 // check the attribute arguments.
2185 if (Attr.getNumArgs() != 0) {
2186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2187 return;
2188 }
2189
2190 if (!isa<FunctionDecl>(d)) {
2191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2192 << Attr.getName() << 0 /*function*/;
2193 return;
2194 }
2195
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002196 FunctionDecl *FD = cast<FunctionDecl>(d);
2197 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002198 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002199 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2200 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2201 << FD->getType()
2202 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2203 "void");
2204 } else {
2205 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2206 << FD->getType();
2207 }
2208 return;
2209 }
2210
Peter Collingbourneced76712010-12-01 03:15:31 +00002211 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2212 } else {
2213 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2214 }
2215}
2216
2217static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2218 if (S.LangOpts.CUDA) {
2219 // check the attribute arguments.
2220 if (Attr.getNumArgs() != 0) {
2221 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2222 return;
2223 }
2224
2225 if (!isa<FunctionDecl>(d)) {
2226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2227 << Attr.getName() << 0 /*function*/;
2228 return;
2229 }
2230
2231 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2232 } else {
2233 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2234 }
2235}
2236
2237static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2238 if (S.LangOpts.CUDA) {
2239 // check the attribute arguments.
2240 if (Attr.getNumArgs() != 0) {
2241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2242 return;
2243 }
2244
2245 if (!isa<VarDecl>(d)) {
2246 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2247 << Attr.getName() << 12 /*variable*/;
2248 return;
2249 }
2250
2251 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2252 } else {
2253 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2254 }
2255}
2256
Chris Lattnercf2a7212009-04-20 19:12:28 +00002257static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002258 // check the attribute arguments.
2259 if (Attr.getNumArgs() != 0) {
2260 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2261 return;
2262 }
Mike Stumpbf916502009-07-24 19:02:52 +00002263
Chris Lattnerc5197432009-04-14 17:02:11 +00002264 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2265 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002267 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002268 return;
2269 }
Mike Stumpbf916502009-07-24 19:02:52 +00002270
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002271 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002272 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002273 return;
2274 }
Mike Stumpbf916502009-07-24 19:02:52 +00002275
Sean Huntcf807c42010-08-18 23:23:40 +00002276 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002277}
2278
John McCall711c52b2011-01-05 12:14:39 +00002279static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2280 if (hasDeclarator(d)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002281
John McCall711c52b2011-01-05 12:14:39 +00002282 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2283 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2284 CallingConv CC;
2285 if (S.CheckCallingConvAttr(attr, CC))
2286 return;
2287
2288 if (!isa<ObjCMethodDecl>(d)) {
2289 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2290 << attr.getName() << 0 /*function*/;
2291 return;
2292 }
2293
2294 switch (attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002295 case AttributeList::AT_fastcall:
John McCall711c52b2011-01-05 12:14:39 +00002296 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002297 return;
2298 case AttributeList::AT_stdcall:
John McCall711c52b2011-01-05 12:14:39 +00002299 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002300 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002301 case AttributeList::AT_thiscall:
John McCall711c52b2011-01-05 12:14:39 +00002302 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002303 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002304 case AttributeList::AT_cdecl:
John McCall711c52b2011-01-05 12:14:39 +00002305 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002306 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002307 case AttributeList::AT_pascal:
John McCall711c52b2011-01-05 12:14:39 +00002308 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002309 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002310 default:
2311 llvm_unreachable("unexpected attribute kind");
2312 return;
2313 }
2314}
2315
John McCall711c52b2011-01-05 12:14:39 +00002316bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2317 if (attr.isInvalid())
2318 return true;
2319
2320 if (attr.getNumArgs() != 0) {
2321 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2322 attr.setInvalid();
2323 return true;
2324 }
2325
2326 // TODO: diagnose uses of these conventions on the wrong target.
2327 switch (attr.getKind()) {
2328 case AttributeList::AT_cdecl: CC = CC_C; break;
2329 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2330 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2331 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2332 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
2333 default: llvm_unreachable("unexpected attribute kind"); return true;
2334 }
2335
2336 return false;
2337}
2338
2339static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2340 if (hasDeclarator(d)) return;
2341
2342 unsigned numParams;
2343 if (S.CheckRegparmAttr(attr, numParams))
2344 return;
2345
2346 if (!isa<ObjCMethodDecl>(d)) {
2347 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2348 << attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002349 return;
2350 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002351
John McCall711c52b2011-01-05 12:14:39 +00002352 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2353}
2354
2355/// Checks a regparm attribute, returning true if it is ill-formed and
2356/// otherwise setting numParams to the appropriate value.
2357bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2358 if (attr.isInvalid())
2359 return true;
2360
2361 if (attr.getNumArgs() != 1) {
2362 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2363 attr.setInvalid();
2364 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002365 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002366
John McCall711c52b2011-01-05 12:14:39 +00002367 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002368 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002369 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002370 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2371 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002372 << "regparm" << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002373 attr.setInvalid();
2374 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002375 }
2376
John McCall711c52b2011-01-05 12:14:39 +00002377 if (Context.Target.getRegParmMax() == 0) {
2378 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002379 << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002380 attr.setInvalid();
2381 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002382 }
2383
John McCall711c52b2011-01-05 12:14:39 +00002384 numParams = NumParams.getZExtValue();
2385 if (numParams > Context.Target.getRegParmMax()) {
2386 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2387 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2388 attr.setInvalid();
2389 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002390 }
2391
John McCall711c52b2011-01-05 12:14:39 +00002392 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002393}
2394
Peter Collingbourne7b381982010-12-12 23:03:07 +00002395static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2396 if (S.LangOpts.CUDA) {
2397 // check the attribute arguments.
2398 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
2399 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2400 << "1 or 2";
2401 return;
2402 }
2403
2404 if (!isFunctionOrMethod(d)) {
2405 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2406 << Attr.getName() << 0 /*function*/;
2407 return;
2408 }
2409
2410 Expr *MaxThreadsExpr = Attr.getArg(0);
2411 llvm::APSInt MaxThreads(32);
2412 if (MaxThreadsExpr->isTypeDependent() ||
2413 MaxThreadsExpr->isValueDependent() ||
2414 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2415 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2416 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2417 return;
2418 }
2419
2420 llvm::APSInt MinBlocks(32);
2421 if (Attr.getNumArgs() > 1) {
2422 Expr *MinBlocksExpr = Attr.getArg(1);
2423 if (MinBlocksExpr->isTypeDependent() ||
2424 MinBlocksExpr->isValueDependent() ||
2425 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2426 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2427 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2428 return;
2429 }
2430 }
2431
2432 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2433 MaxThreads.getZExtValue(),
2434 MinBlocks.getZExtValue()));
2435 } else {
2436 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2437 }
2438}
2439
Sean Huntbbd37c62009-11-21 08:43:09 +00002440static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2441 // check the attribute arguments.
2442 if (Attr.getNumArgs() != 0) {
2443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2444 return;
2445 }
2446
2447 if (!isa<CXXRecordDecl>(d)
2448 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2449 S.Diag(Attr.getLoc(),
2450 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2451 : diag::warn_attribute_wrong_decl_type)
2452 << Attr.getName() << 7 /*virtual method or class*/;
2453 return;
2454 }
Sean Hunt7725e672009-11-25 04:20:27 +00002455
2456 // FIXME: Conform to C++0x redeclaration rules.
2457
2458 if (d->getAttr<FinalAttr>()) {
2459 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2460 return;
2461 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002462
Sean Huntcf807c42010-08-18 23:23:40 +00002463 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002464}
2465
Chris Lattner0744e5f2008-06-29 00:23:49 +00002466//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002467// C++0x member checking attributes
2468//===----------------------------------------------------------------------===//
2469
2470static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2471 if (Attr.getNumArgs() != 0) {
2472 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2473 return;
2474 }
2475
2476 if (!isa<CXXRecordDecl>(d)) {
2477 S.Diag(Attr.getLoc(),
2478 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2479 : diag::warn_attribute_wrong_decl_type)
2480 << Attr.getName() << 9 /*class*/;
2481 return;
2482 }
2483
2484 if (d->getAttr<BaseCheckAttr>()) {
2485 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2486 return;
2487 }
2488
Sean Huntcf807c42010-08-18 23:23:40 +00002489 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002490}
2491
2492static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2493 if (Attr.getNumArgs() != 0) {
2494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2495 return;
2496 }
2497
2498 if (!isa<RecordDecl>(d->getDeclContext())) {
2499 // FIXME: It's not the type that's the problem
2500 S.Diag(Attr.getLoc(),
2501 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2502 : diag::warn_attribute_wrong_decl_type)
2503 << Attr.getName() << 11 /*member*/;
2504 return;
2505 }
2506
2507 // FIXME: Conform to C++0x redeclaration rules.
2508
2509 if (d->getAttr<HidingAttr>()) {
2510 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2511 return;
2512 }
2513
Sean Huntcf807c42010-08-18 23:23:40 +00002514 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002515}
2516
2517static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2518 if (Attr.getNumArgs() != 0) {
2519 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2520 return;
2521 }
2522
2523 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2524 // FIXME: It's not the type that's the problem
2525 S.Diag(Attr.getLoc(),
2526 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2527 : diag::warn_attribute_wrong_decl_type)
2528 << Attr.getName() << 10 /*virtual method*/;
2529 return;
2530 }
2531
2532 // FIXME: Conform to C++0x redeclaration rules.
2533
2534 if (d->getAttr<OverrideAttr>()) {
2535 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2536 return;
2537 }
2538
Sean Huntcf807c42010-08-18 23:23:40 +00002539 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002540}
2541
2542//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002543// Checker-specific attribute handlers.
2544//===----------------------------------------------------------------------===//
2545
2546static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2547 Sema &S) {
2548
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002549 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002550
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002551 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2552 RetTy = MD->getResultType();
2553 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2554 RetTy = FD->getResultType();
2555 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002556 SourceLocation L = Attr.getLoc();
2557 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2558 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002559 return;
2560 }
Mike Stumpbf916502009-07-24 19:02:52 +00002561
Ted Kremenek6217b802009-07-29 21:53:49 +00002562 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002563 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002564 SourceLocation L = Attr.getLoc();
2565 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2566 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002567 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002568 }
Mike Stumpbf916502009-07-24 19:02:52 +00002569
Ted Kremenekb71368d2009-05-09 02:44:38 +00002570 switch (Attr.getKind()) {
2571 default:
2572 assert(0 && "invalid ownership attribute");
2573 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002574 case AttributeList::AT_cf_returns_not_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002575 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
2576 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002577 return;
2578 case AttributeList::AT_ns_returns_not_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002579 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
2580 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002581 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002582 case AttributeList::AT_cf_returns_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002583 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
2584 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002585 return;
2586 case AttributeList::AT_ns_returns_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002587 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
2588 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002589 return;
2590 };
2591}
2592
Charles Davisf0122fe2010-02-16 18:27:26 +00002593static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2594 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002595 Attr.getKind() == AttributeList::AT_dllexport ||
2596 Attr.getKind() == AttributeList::AT_uuid;
2597}
2598
2599//===----------------------------------------------------------------------===//
2600// Microsoft specific attribute handlers.
2601//===----------------------------------------------------------------------===//
2602
2603static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2604 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2605 // check the attribute arguments.
2606 if (Attr.getNumArgs() != 1) {
2607 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2608 return;
2609 }
2610 Expr *Arg = Attr.getArg(0);
2611 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichetd3d3be92010-12-20 01:41:49 +00002612 if (Str == 0 || Str->isWide()) {
2613 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2614 << "uuid" << 1;
2615 return;
2616 }
2617
2618 llvm::StringRef StrRef = Str->getString();
2619
2620 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2621 StrRef.back() == '}';
2622
2623 // Validate GUID length.
2624 if (IsCurly && StrRef.size() != 38) {
2625 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2626 return;
2627 }
2628 if (!IsCurly && StrRef.size() != 36) {
2629 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2630 return;
2631 }
2632
2633 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2634 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
2635 llvm::StringRef::iterator I = StrRef.begin();
2636 if (IsCurly) // Skip the optional '{'
2637 ++I;
2638
2639 for (int i = 0; i < 36; ++i) {
2640 if (i == 8 || i == 13 || i == 18 || i == 23) {
2641 if (*I != '-') {
2642 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2643 return;
2644 }
2645 } else if (!isxdigit(*I)) {
2646 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2647 return;
2648 }
2649 I++;
2650 }
Francois Pichet11542142010-12-19 06:50:37 +00002651
2652 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2653 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00002654 } else
Francois Pichet11542142010-12-19 06:50:37 +00002655 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00002656}
2657
Ted Kremenekb71368d2009-05-09 02:44:38 +00002658//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002659// Top Level Sema Entry Points
2660//===----------------------------------------------------------------------===//
2661
Peter Collingbourne60700392011-01-21 02:08:45 +00002662static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2663 const AttributeList &Attr, Sema &S) {
2664 switch (Attr.getKind()) {
2665 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2666 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2667 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2668 default:
2669 break;
2670 }
2671}
Abramo Bagnarae215f722010-04-30 13:10:51 +00002672
Peter Collingbourne60700392011-01-21 02:08:45 +00002673static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2674 const AttributeList &Attr, Sema &S) {
Chris Lattner803d0802008-06-29 00:43:07 +00002675 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002676 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002677 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2678 case AttributeList::AT_IBOutletCollection:
2679 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002680 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002681 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002682 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002683 case AttributeList::AT_neon_vector_type:
2684 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002685 // Ignore these, these are type attributes, handled by
2686 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002687 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002688 case AttributeList::AT_device:
2689 case AttributeList::AT_host:
2690 case AttributeList::AT_overloadable:
2691 // Ignore, this is a non-inheritable attribute, handled
2692 // by ProcessNonInheritableDeclAttr.
2693 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002694 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2695 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002696 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002697 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002698 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002699 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002700 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2701 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002702 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002703 HandleDependencyAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002704 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002705 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002706 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2707 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2708 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002709 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002710 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002711 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002712 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2713 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2714 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002715 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002716 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2717 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002718 case AttributeList::AT_launch_bounds:
2719 HandleLaunchBoundsAttr(D, Attr, S);
2720 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002721 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2722 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002723 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002724 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002725 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002726 case AttributeList::AT_ownership_returns:
2727 case AttributeList::AT_ownership_takes:
2728 case AttributeList::AT_ownership_holds:
2729 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002730 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002731 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2732 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2733 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002734 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002735 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002736
2737 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002738 case AttributeList::AT_ns_returns_not_retained:
2739 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002740 case AttributeList::AT_ns_returns_retained:
2741 case AttributeList::AT_cf_returns_retained:
2742 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2743
Nate Begeman6f3d8382009-06-26 06:32:41 +00002744 case AttributeList::AT_reqd_wg_size:
2745 HandleReqdWorkGroupSize(D, Attr, S); break;
2746
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002747 case AttributeList::AT_init_priority:
2748 HandleInitPriorityAttr(D, Attr, S); break;
2749
Sean Hunt7725e672009-11-25 04:20:27 +00002750 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2751 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002752 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2753 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2754 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002755 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002756 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2757 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002758 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002759 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002760 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002761 case AttributeList::AT_transparent_union:
2762 HandleTransparentUnionAttr(D, Attr, S);
2763 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002764 case AttributeList::AT_objc_exception:
2765 HandleObjCExceptionAttr(D, Attr, S);
2766 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002767 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2768 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2769 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2770 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2771 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2772 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2773 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2774 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2775 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002776 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002777 // Just ignore
2778 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002779 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2780 HandleNoInstrumentFunctionAttr(D, Attr, S);
2781 break;
John McCall04a67a62010-02-05 21:31:56 +00002782 case AttributeList::AT_stdcall:
2783 case AttributeList::AT_cdecl:
2784 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002785 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002786 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002787 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002788 break;
Francois Pichet11542142010-12-19 06:50:37 +00002789 case AttributeList::AT_uuid:
2790 HandleUuidAttr(D, Attr, S);
2791 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002792 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002793 // Ask target about the attribute.
2794 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2795 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002796 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2797 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002798 break;
2799 }
2800}
2801
Peter Collingbourne60700392011-01-21 02:08:45 +00002802/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2803/// the attribute applies to decls. If the attribute is a type attribute, just
2804/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2805/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2806static void ProcessDeclAttribute(Scope *scope, Decl *D,
2807 const AttributeList &Attr, Sema &S,
2808 bool NonInheritable, bool Inheritable) {
2809 if (Attr.isInvalid())
2810 return;
2811
2812 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2813 // FIXME: Try to deal with other __declspec attributes!
2814 return;
2815
2816 if (NonInheritable)
2817 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2818
2819 if (Inheritable)
2820 ProcessInheritableDeclAttr(scope, D, Attr, S);
2821}
2822
Chris Lattner803d0802008-06-29 00:43:07 +00002823/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2824/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00002825void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00002826 const AttributeList *AttrList,
2827 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002828 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourne60700392011-01-21 02:08:45 +00002829 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002830 }
2831
2832 // GCC accepts
2833 // static int a9 __attribute__((weakref));
2834 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00002835 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002836 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002837 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002838 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002839 }
2840}
2841
Ryan Flynne25ff832009-07-30 03:15:39 +00002842/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2843/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002844NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002845 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002846 NamedDecl *NewD = 0;
2847 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2848 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2849 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002850 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002851 if (FD->getQualifier()) {
2852 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2853 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2854 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002855 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2856 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2857 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002858 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002859 VD->getStorageClass(),
2860 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002861 if (VD->getQualifier()) {
2862 VarDecl *NewVD = cast<VarDecl>(NewD);
2863 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2864 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002865 }
2866 return NewD;
2867}
2868
2869/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2870/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002871void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002872 if (W.getUsed()) return; // only do this once
2873 W.setUsed(true);
2874 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2875 IdentifierInfo *NDId = ND->getIdentifier();
2876 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002877 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2878 NDId->getName()));
2879 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002880 WeakTopLevelDecl.push_back(NewD);
2881 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2882 // to insert Decl at TU scope, sorry.
2883 DeclContext *SavedContext = CurContext;
2884 CurContext = Context.getTranslationUnitDecl();
2885 PushOnScopeChains(NewD, S);
2886 CurContext = SavedContext;
2887 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002888 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002889 }
2890}
2891
Chris Lattner0744e5f2008-06-29 00:23:49 +00002892/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2893/// it, apply them to D. This is a bit tricky because PD can have attributes
2894/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00002895void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
2896 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00002897 // It's valid to "forward-declare" #pragma weak, in which case we
2898 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00002899 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00002900 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2901 if (IdentifierInfo *Id = ND->getIdentifier()) {
2902 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2903 = WeakUndeclaredIdentifiers.find(Id);
2904 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2905 WeakInfo W = I->second;
2906 DeclApplyPragmaWeak(S, ND, W);
2907 WeakUndeclaredIdentifiers[Id] = W;
2908 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002909 }
2910 }
2911 }
2912
Chris Lattner0744e5f2008-06-29 00:23:49 +00002913 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00002914 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00002915 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00002916
Chris Lattner0744e5f2008-06-29 00:23:49 +00002917 // Walk the declarator structure, applying decl attributes that were in a type
2918 // position to the decl itself. This handles cases like:
2919 // int *__attr__(x)** D;
2920 // when X is a decl attribute.
2921 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2922 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00002923 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00002924
Chris Lattner0744e5f2008-06-29 00:23:49 +00002925 // Finally, apply any attributes on the decl itself.
2926 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00002927 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002928}
John McCall54abf7d2009-11-04 02:18:39 +00002929
2930/// PushParsingDeclaration - Enter a new "scope" of deprecation
2931/// warnings.
2932///
2933/// The state token we use is the start index of this scope
2934/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002935Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002936 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002937 return (ParsingDeclStackState) DelayedDiagnostics.size();
2938}
2939
John McCalld226f652010-08-21 09:40:31 +00002940void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002941 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2942 ParsingDeclDepth--;
2943
2944 if (DelayedDiagnostics.empty())
2945 return;
2946
2947 unsigned SavedIndex = (unsigned) S;
2948 assert(SavedIndex <= DelayedDiagnostics.size() &&
2949 "saved index is out of bounds");
2950
John McCall58e6f342010-03-16 05:22:47 +00002951 unsigned E = DelayedDiagnostics.size();
2952
John McCall2f514482010-01-27 03:50:35 +00002953 // We only want to actually emit delayed diagnostics when we
2954 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002955 if (D) {
2956 // We really do want to start with 0 here. We get one push for a
2957 // decl spec and another for each declarator; in a decl group like:
2958 // deprecated_typedef foo, *bar, baz();
2959 // only the declarator pops will be passed decls. This is correct;
2960 // we really do need to consider delayed diagnostics from the decl spec
2961 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002962 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002963 if (DelayedDiagnostics[I].Triggered)
2964 continue;
2965
2966 switch (DelayedDiagnostics[I].Kind) {
2967 case DelayedDiagnostic::Deprecation:
2968 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2969 break;
2970
2971 case DelayedDiagnostic::Access:
2972 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2973 break;
2974 }
2975 }
2976 }
2977
John McCall58e6f342010-03-16 05:22:47 +00002978 // Destroy all the delayed diagnostics we're about to pop off.
2979 for (unsigned I = SavedIndex; I != E; ++I)
2980 DelayedDiagnostics[I].destroy();
2981
John McCall2f514482010-01-27 03:50:35 +00002982 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002983}
2984
2985static bool isDeclDeprecated(Decl *D) {
2986 do {
2987 if (D->hasAttr<DeprecatedAttr>())
2988 return true;
2989 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2990 return false;
2991}
2992
John McCall9c3087b2010-08-26 02:13:20 +00002993void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002994 Decl *Ctx) {
2995 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002996 return;
2997
John McCall2f514482010-01-27 03:50:35 +00002998 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002999 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003000 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003001 << DD.getDeprecationDecl()->getDeclName()
3002 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003003 else
3004 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003005 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003006}
3007
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003008void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003009 SourceLocation Loc,
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003010 bool UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003011 // Delay if we're currently parsing a declaration.
3012 if (ParsingDeclDepth) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003013 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
3014 Message));
John McCall54abf7d2009-11-04 02:18:39 +00003015 return;
3016 }
3017
3018 // Otherwise, don't warn if our current context is deprecated.
3019 if (isDeclDeprecated(cast<Decl>(CurContext)))
3020 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003021 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003022 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3023 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003024 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003025 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003026 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
3027 else
3028 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
3029 }
John McCall54abf7d2009-11-04 02:18:39 +00003030}