blob: 09feb50bd3018f31828a5f4231ea7960e3eca1ec [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
Daniel Dunbard3f2c102008-10-19 02:04:16 +000081/// hasFunctionProto - Return true if the given decl has a argument
82/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000083/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000084static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000085 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000086 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000087 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000088 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000089 return true;
90 }
91}
92
93/// getFunctionOrMethodNumArgs - Return number of function or method
94/// arguments. It is an error to call this on a K&R function (use
95/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000096static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000097 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000098 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000099 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
100 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +0000101 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000102}
103
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000104static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000105 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000106 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000107 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
108 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000109
Chris Lattner89951a82009-02-20 18:43:26 +0000110 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000111}
112
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000113static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000114 if (const FunctionType *FnTy = getFunctionType(d))
115 return cast<FunctionProtoType>(FnTy)->getResultType();
116 return cast<ObjCMethodDecl>(d)->getResultType();
117}
118
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000119static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000121 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000122 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000123 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000124 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000125 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000126 return cast<ObjCMethodDecl>(d)->isVariadic();
127 }
128}
129
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000131 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000132 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000133 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000134
John McCall506b57e2010-05-17 21:00:27 +0000135 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
136 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000137 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000138
John McCall506b57e2010-05-17 21:00:27 +0000139 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000140
Chris Lattner6b6b5372008-06-26 18:38:35 +0000141 // FIXME: Should we walk the chain of classes?
142 return ClsName == &Ctx.Idents.get("NSString") ||
143 ClsName == &Ctx.Idents.get("NSMutableString");
144}
145
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000146static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000147 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000148 if (!PT)
149 return false;
150
Ted Kremenek6217b802009-07-29 21:53:49 +0000151 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152 if (!RT)
153 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000154
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000155 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000156 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000157 return false;
158
159 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
160}
161
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000162//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000163// Attribute Implementations
164//===----------------------------------------------------------------------===//
165
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000166// FIXME: All this manual attribute parsing code is gross. At the
167// least add some helper functions to check most argument patterns (#
168// and types of args).
169
Mike Stumpbf916502009-07-24 19:02:52 +0000170static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000171 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000172 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
173 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000174 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000175 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176 }
Mike Stumpbf916502009-07-24 19:02:52 +0000177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000179
180 Expr *sizeExpr;
181
182 // Special case where the argument is a template id.
183 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000184 CXXScopeSpec SS;
185 UnqualifiedId id;
186 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
187 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000188 } else {
189 // check the attribute arguments.
190 if (Attr.getNumArgs() != 1) {
191 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
192 return;
193 }
194 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000195 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000196
197 // Instantiate/Install the vector type, and let Sema build the type for us.
198 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000199 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000200 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000201 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000202 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000203
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000204 // Remember this typedef decl, we will need it later for diagnostics.
205 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000206 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207}
208
Chris Lattner803d0802008-06-29 00:43:07 +0000209static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000211 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 return;
214 }
Mike Stumpbf916502009-07-24 19:02:52 +0000215
Chris Lattner6b6b5372008-06-26 18:38:35 +0000216 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Sean Huntcf807c42010-08-18 23:23:40 +0000217 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000218 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
219 // If the alignment is less than or equal to 8 bits, the packed attribute
220 // has no effect.
221 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000222 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000223 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000224 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000225 else
Sean Huntcf807c42010-08-18 23:23:40 +0000226 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000227 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000228 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000229}
230
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000231static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 // check the attribute arguments.
233 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000235 return;
236 }
Mike Stumpbf916502009-07-24 19:02:52 +0000237
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000238 // The IBAction attributes only apply to instance methods.
239 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
240 if (MD->isInstanceMethod()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000241 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000242 return;
243 }
244
245 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
246}
247
248static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
249 // check the attribute arguments.
250 if (Attr.getNumArgs() > 0) {
251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
252 return;
253 }
254
255 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000256 // Objective-C classes.
257 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000258 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000259 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000260 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000261
262 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000263}
264
Ted Kremenek857e9182010-05-19 17:38:06 +0000265static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
266 Sema &S) {
267
268 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000269 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000270 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
271 return;
272 }
273
274 // The IBOutletCollection attributes only apply to instance variables of
275 // Objective-C classes.
276 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
277 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
278 return;
279 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000280 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
281 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
282 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
283 << VD->getType() << 0;
284 return;
285 }
286 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
287 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
288 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
289 << PD->getType() << 1;
290 return;
291 }
292
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000293 IdentifierInfo *II = Attr.getParameterName();
294 if (!II)
295 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000296
John McCallb3d87482010-08-24 05:47:05 +0000297 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000298 S.getScopeForContext(d->getDeclContext()->getParent()));
299 if (!TypeRep) {
300 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
301 return;
302 }
John McCallb3d87482010-08-24 05:47:05 +0000303 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000304 // Diagnose use of non-object type in iboutletcollection attribute.
305 // FIXME. Gnu attribute extension ignores use of builtin types in
306 // attributes. So, __attribute__((iboutletcollection(char))) will be
307 // treated as __attribute__((iboutletcollection())).
308 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
309 !QT->isObjCObjectType()) {
310 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
311 return;
312 }
Sean Huntcf807c42010-08-18 23:23:40 +0000313 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
314 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000315}
316
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000317static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000318 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
319 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000320 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000321 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000322 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000323 return;
324 }
Mike Stumpbf916502009-07-24 19:02:52 +0000325
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000326 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000327
328 // The nonnull attribute only applies to pointers.
329 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000330
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000331 for (AttributeList::arg_iterator I=Attr.arg_begin(),
332 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000333
334
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000335 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000336 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000337 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000338 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
339 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000340 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
341 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000342 return;
343 }
Mike Stumpbf916502009-07-24 19:02:52 +0000344
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000345 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000346
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000347 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000348 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000349 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000350 return;
351 }
Mike Stumpbf916502009-07-24 19:02:52 +0000352
Ted Kremenek465172f2008-07-21 22:09:15 +0000353 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000354
355 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000356 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000357 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000358 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000359 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000360 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000361 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000362 }
Mike Stumpbf916502009-07-24 19:02:52 +0000363
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000364 NonNullArgs.push_back(x);
365 }
Mike Stumpbf916502009-07-24 19:02:52 +0000366
367 // If no arguments were specified to __attribute__((nonnull)) then all pointer
368 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000369 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000370 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
371 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000372 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000373 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000374 }
Mike Stumpbf916502009-07-24 19:02:52 +0000375
Ted Kremenek1db5d142010-09-09 01:17:32 +0000376 // No pointer arguments? The attribute in this case is
377 // trivially satisfied.
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000378 if (NonNullArgs.empty()) {
379 // Warn the trivial case only if attribute is not coming from a
380 // macro instantiation.
381 if (Attr.getLoc().isFileID())
382 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000383 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000384 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000385 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000386
387 unsigned* start = &NonNullArgs[0];
388 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000389 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000390 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
391 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000392}
393
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000394static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
395 // This attribute must be applied to a function declaration.
396 // The first argument to the attribute must be a string,
397 // the name of the resource, for example "malloc".
398 // The following arguments must be argument indexes, the arguments must be
399 // of integer type for Returns, otherwise of pointer type.
400 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000401 // after being held. free() should be __attribute((ownership_takes)), whereas
402 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000403
404 if (!AL.getParameterName()) {
405 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
406 << AL.getName()->getName() << 1;
407 return;
408 }
409 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000410 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000411 switch (AL.getKind()) {
412 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000413 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000414 if (AL.getNumArgs() < 1) {
415 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
416 return;
417 }
Jordy Rose2a479922010-08-12 08:54:03 +0000418 break;
419 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000420 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000421 if (AL.getNumArgs() < 1) {
422 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
423 return;
424 }
Jordy Rose2a479922010-08-12 08:54:03 +0000425 break;
426 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000427 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000428 if (AL.getNumArgs() > 1) {
429 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
430 << AL.getNumArgs() + 1;
431 return;
432 }
Jordy Rose2a479922010-08-12 08:54:03 +0000433 break;
434 default:
435 // This should never happen given how we are called.
436 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000437 }
438
439 if (!isFunction(d) || !hasFunctionProto(d)) {
440 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
441 << 0 /*function*/;
442 return;
443 }
444
445 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
446
447 llvm::StringRef Module = AL.getParameterName()->getName();
448
449 // Normalize the argument, __foo__ becomes foo.
450 if (Module.startswith("__") && Module.endswith("__"))
451 Module = Module.substr(2, Module.size() - 4);
452
453 llvm::SmallVector<unsigned, 10> OwnershipArgs;
454
Jordy Rose2a479922010-08-12 08:54:03 +0000455 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
456 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000457
458 Expr *IdxExpr = static_cast<Expr *>(*I);
459 llvm::APSInt ArgNum(32);
460 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
461 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
462 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
463 << AL.getName()->getName() << IdxExpr->getSourceRange();
464 continue;
465 }
466
467 unsigned x = (unsigned) ArgNum.getZExtValue();
468
469 if (x > NumArgs || x < 1) {
470 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
471 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
472 continue;
473 }
474 --x;
475 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000476 case OwnershipAttr::Takes:
477 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000478 // Is the function argument a pointer type?
479 QualType T = getFunctionOrMethodArgType(d, x);
480 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
481 // FIXME: Should also highlight argument in decl.
482 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000483 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000484 << "pointer"
485 << IdxExpr->getSourceRange();
486 continue;
487 }
488 break;
489 }
Sean Huntcf807c42010-08-18 23:23:40 +0000490 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000491 if (AL.getNumArgs() > 1) {
492 // Is the function argument an integer type?
493 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
494 llvm::APSInt ArgNum(32);
495 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
496 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
497 S.Diag(AL.getLoc(), diag::err_ownership_type)
498 << "ownership_returns" << "integer"
499 << IdxExpr->getSourceRange();
500 return;
501 }
502 }
503 break;
504 }
Jordy Rose2a479922010-08-12 08:54:03 +0000505 default:
506 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000507 } // switch
508
509 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000510 for (specific_attr_iterator<OwnershipAttr>
511 i = d->specific_attr_begin<OwnershipAttr>(),
512 e = d->specific_attr_end<OwnershipAttr>();
513 i != e; ++i) {
514 if ((*i)->getOwnKind() != K) {
515 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
516 I!=E; ++I) {
517 if (x == *I) {
518 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
519 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000520 }
521 }
522 }
523 }
524 OwnershipArgs.push_back(x);
525 }
526
527 unsigned* start = OwnershipArgs.data();
528 unsigned size = OwnershipArgs.size();
529 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000530
531 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
532 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
533 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000534 }
Sean Huntcf807c42010-08-18 23:23:40 +0000535
536 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
537 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000538}
539
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000540static bool isStaticVarOrStaticFunciton(Decl *D) {
541 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000542 return VD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000543 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000544 return FD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000545 return false;
546}
547
548static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
549 // Check the attribute arguments.
550 if (Attr.getNumArgs() > 1) {
551 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
552 return;
553 }
554
555 // gcc rejects
556 // class c {
557 // static int a __attribute__((weakref ("v2")));
558 // static int b() __attribute__((weakref ("f3")));
559 // };
560 // and ignores the attributes of
561 // void f(void) {
562 // static int a __attribute__((weakref ("v2")));
563 // }
564 // we reject them
Sebastian Redl7a126a42010-08-31 00:36:30 +0000565 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
566 if (!Ctx->isFileContext()) {
567 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
568 dyn_cast<NamedDecl>(d)->getNameAsString();
569 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000570 }
571
572 // The GCC manual says
573 //
574 // At present, a declaration to which `weakref' is attached can only
575 // be `static'.
576 //
577 // It also says
578 //
579 // Without a TARGET,
580 // given as an argument to `weakref' or to `alias', `weakref' is
581 // equivalent to `weak'.
582 //
583 // gcc 4.4.1 will accept
584 // int a7 __attribute__((weakref));
585 // as
586 // int a7 __attribute__((weak));
587 // This looks like a bug in gcc. We reject that for now. We should revisit
588 // it if this behaviour is actually used.
589
590 if (!isStaticVarOrStaticFunciton(d)) {
591 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
592 dyn_cast<NamedDecl>(d)->getNameAsString();
593 return;
594 }
595
596 // GCC rejects
597 // static ((alias ("y"), weakref)).
598 // Should we? How to check that weakref is before or after alias?
599
600 if (Attr.getNumArgs() == 1) {
601 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
602 Arg = Arg->IgnoreParenCasts();
603 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
604
605 if (Str == 0 || Str->isWide()) {
606 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
607 << "weakref" << 1;
608 return;
609 }
610 // GCC will accept anything as the argument of weakref. Should we
611 // check for an existing decl?
Sean Huntcf807c42010-08-18 23:23:40 +0000612 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000613 }
614
Sean Huntcf807c42010-08-18 23:23:40 +0000615 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000616}
617
Chris Lattner803d0802008-06-29 00:43:07 +0000618static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000619 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000620 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000622 return;
623 }
Mike Stumpbf916502009-07-24 19:02:52 +0000624
Chris Lattner545dd342008-06-28 23:36:30 +0000625 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000626 Arg = Arg->IgnoreParenCasts();
627 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000628
Chris Lattner6b6b5372008-06-26 18:38:35 +0000629 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000630 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000631 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000632 return;
633 }
Mike Stumpbf916502009-07-24 19:02:52 +0000634
Chris Lattner6b6b5372008-06-26 18:38:35 +0000635 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000636
Sean Huntcf807c42010-08-18 23:23:40 +0000637 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000638}
639
Mike Stumpbf916502009-07-24 19:02:52 +0000640static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000641 Sema &S) {
642 // check the attribute arguments.
643 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000645 return;
646 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000647
Chris Lattnerc5197432009-04-14 17:02:11 +0000648 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000650 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000651 return;
652 }
Mike Stumpbf916502009-07-24 19:02:52 +0000653
Sean Huntcf807c42010-08-18 23:23:40 +0000654 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000655}
656
Ryan Flynn76168e22009-08-09 20:07:29 +0000657static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
658 // check the attribute arguments.
659 if (Attr.getNumArgs() != 0) {
660 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
661 return;
662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000664 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000665 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000666 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000667 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000668 return;
669 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000670 }
671
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000672 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000673}
674
Ted Kremenekb7252322009-04-10 00:01:14 +0000675static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000676 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
677 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000678 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000679}
680
681static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
682 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000683
684 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
685 // because 'analyzer_noreturn' does not impact the type.
686
687 if (Attr.getNumArgs() != 0) {
688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
689 return;
690 }
691
692 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
693 ValueDecl *VD = dyn_cast<ValueDecl>(d);
694 if (VD == 0 || (!VD->getType()->isBlockPointerType()
695 && !VD->getType()->isFunctionPointerType())) {
696 S.Diag(Attr.getLoc(),
697 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
698 : diag::warn_attribute_wrong_decl_type)
699 << Attr.getName() << 0 /*function*/;
700 return;
701 }
702 }
703
704 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000705}
706
John Thompson35cc9622010-08-09 21:53:52 +0000707// PS3 PPU-specific.
708static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
709 Sema &S) {
710/*
711 Returning a Vector Class in Registers
712
713 According to the PPU ABI specifications, a class with a single member of vector type is returned in
714 memory when used as the return value of a function. This results in inefficient code when implementing
715 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
716 definition. This attribute is also applicable to struct types.
717
718 Example:
719
720 struct Vector
721 {
722 __vector float xyzw;
723 } __attribute__((vecreturn));
724
725 Vector Add(Vector lhs, Vector rhs)
726 {
727 Vector result;
728 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
729 return result; // This will be returned in a register
730 }
731*/
John Thompson01add592010-09-18 01:12:07 +0000732 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000733 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
734 << Attr.getName() << 9 /*class*/;
735 return;
736 }
737
738 if (d->getAttr<VecReturnAttr>()) {
739 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
740 return;
741 }
742
John Thompson01add592010-09-18 01:12:07 +0000743 RecordDecl *record = cast<RecordDecl>(d);
744 int count = 0;
745
746 if (!isa<CXXRecordDecl>(record)) {
747 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
748 return;
749 }
750
751 if (!cast<CXXRecordDecl>(record)->isPOD()) {
752 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
753 return;
754 }
755
756 for (RecordDecl::field_iterator iter = record->field_begin(); iter != record->field_end(); iter++) {
757 if ((count == 1) || !iter->getType()->isVectorType()) {
758 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
759 return;
760 }
761 count++;
762 }
763
Sean Huntcf807c42010-08-18 23:23:40 +0000764 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000765}
766
Sean Huntbbd37c62009-11-21 08:43:09 +0000767static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
768 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000770 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000771 return;
772 }
773 // FIXME: Actually store the attribute on the declaration
774}
775
Ted Kremenek73798892008-07-25 04:39:19 +0000776static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
777 // check the attribute arguments.
778 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000779 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000780 return;
781 }
Mike Stumpbf916502009-07-24 19:02:52 +0000782
John McCallaec58602010-03-31 02:47:45 +0000783 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
784 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000785 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000786 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000787 return;
788 }
Mike Stumpbf916502009-07-24 19:02:52 +0000789
Sean Huntcf807c42010-08-18 23:23:40 +0000790 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000791}
792
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000793static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
794 // check the attribute arguments.
795 if (Attr.getNumArgs() != 0) {
796 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
797 return;
798 }
Mike Stumpbf916502009-07-24 19:02:52 +0000799
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000800 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000801 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000802 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
803 return;
804 }
805 } else if (!isFunctionOrMethod(d)) {
806 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000807 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000808 return;
809 }
Mike Stumpbf916502009-07-24 19:02:52 +0000810
Sean Huntcf807c42010-08-18 23:23:40 +0000811 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000812}
813
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000814static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
815 // check the attribute arguments.
816 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
818 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000819 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000820 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000821
822 int priority = 65535; // FIXME: Do not hardcode such constants.
823 if (Attr.getNumArgs() > 0) {
824 Expr *E = static_cast<Expr *>(Attr.getArg(0));
825 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000826 if (E->isTypeDependent() || E->isValueDependent() ||
827 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000828 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000829 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000830 return;
831 }
832 priority = Idx.getZExtValue();
833 }
Mike Stumpbf916502009-07-24 19:02:52 +0000834
Chris Lattnerc5197432009-04-14 17:02:11 +0000835 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000836 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000837 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000838 return;
839 }
840
Sean Huntcf807c42010-08-18 23:23:40 +0000841 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000842}
843
844static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
845 // check the attribute arguments.
846 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
848 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000849 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000850 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000851
852 int priority = 65535; // FIXME: Do not hardcode such constants.
853 if (Attr.getNumArgs() > 0) {
854 Expr *E = static_cast<Expr *>(Attr.getArg(0));
855 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000856 if (E->isTypeDependent() || E->isValueDependent() ||
857 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000858 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000860 return;
861 }
862 priority = Idx.getZExtValue();
863 }
Mike Stumpbf916502009-07-24 19:02:52 +0000864
Anders Carlsson6782fc62008-08-22 22:10:48 +0000865 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000866 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000867 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000868 return;
869 }
870
Sean Huntcf807c42010-08-18 23:23:40 +0000871 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000872}
873
Chris Lattner803d0802008-06-29 00:43:07 +0000874static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000875 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000876 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000877 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000878 return;
879 }
Mike Stumpbf916502009-07-24 19:02:52 +0000880
Sean Huntcf807c42010-08-18 23:23:40 +0000881 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000882}
883
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000884static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
885 // check the attribute arguments.
886 if (Attr.getNumArgs() != 0) {
887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
888 return;
889 }
Mike Stumpbf916502009-07-24 19:02:52 +0000890
Sean Huntcf807c42010-08-18 23:23:40 +0000891 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000892}
893
Chris Lattner803d0802008-06-29 00:43:07 +0000894static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000895 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000896 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000897 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000898 return;
899 }
Mike Stumpbf916502009-07-24 19:02:52 +0000900
Chris Lattner545dd342008-06-28 23:36:30 +0000901 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000902 Arg = Arg->IgnoreParenCasts();
903 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000904
Chris Lattner6b6b5372008-06-26 18:38:35 +0000905 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000906 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000907 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000908 return;
909 }
Mike Stumpbf916502009-07-24 19:02:52 +0000910
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000911 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +0000912 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +0000913
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000914 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +0000915 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000916 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +0000917 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000918 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +0000919 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000920 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +0000921 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000922 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000923 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000924 return;
925 }
Mike Stumpbf916502009-07-24 19:02:52 +0000926
Sean Huntcf807c42010-08-18 23:23:40 +0000927 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000928}
929
Chris Lattner0db29ec2009-02-14 08:09:34 +0000930static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
931 Sema &S) {
932 if (Attr.getNumArgs() != 0) {
933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
934 return;
935 }
Mike Stumpbf916502009-07-24 19:02:52 +0000936
Chris Lattner0db29ec2009-02-14 08:09:34 +0000937 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
938 if (OCI == 0) {
939 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
940 return;
941 }
Mike Stumpbf916502009-07-24 19:02:52 +0000942
Sean Huntcf807c42010-08-18 23:23:40 +0000943 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +0000944}
945
946static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000947 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000948 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000949 return;
950 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000951 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000952 QualType T = TD->getUnderlyingType();
953 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000954 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000955 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
956 return;
957 }
958 }
Sean Huntcf807c42010-08-18 23:23:40 +0000959 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000960}
961
Mike Stumpbf916502009-07-24 19:02:52 +0000962static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000963HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
964 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000966 return;
967 }
968
969 if (!isa<FunctionDecl>(D)) {
970 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
971 return;
972 }
973
Sean Huntcf807c42010-08-18 23:23:40 +0000974 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +0000975}
976
Steve Naroff9eae5762008-09-18 16:44:58 +0000977static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000978 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000979 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000980 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000981 return;
982 }
Mike Stumpbf916502009-07-24 19:02:52 +0000983
Steve Naroff9eae5762008-09-18 16:44:58 +0000984 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000985 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000986 return;
987 }
Mike Stumpbf916502009-07-24 19:02:52 +0000988
Sean Huntcf807c42010-08-18 23:23:40 +0000989 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000990 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000991 type = BlocksAttr::ByRef;
992 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000993 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000994 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000995 return;
996 }
Mike Stumpbf916502009-07-24 19:02:52 +0000997
Sean Huntcf807c42010-08-18 23:23:40 +0000998 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000999}
1000
Anders Carlsson77091822008-10-05 18:05:59 +00001001static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1002 // check the attribute arguments.
1003 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001004 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1005 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001006 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001007 }
1008
Anders Carlsson77091822008-10-05 18:05:59 +00001009 int sentinel = 0;
1010 if (Attr.getNumArgs() > 0) {
1011 Expr *E = static_cast<Expr *>(Attr.getArg(0));
1012 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001013 if (E->isTypeDependent() || E->isValueDependent() ||
1014 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001015 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001016 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001017 return;
1018 }
1019 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001020
Anders Carlsson77091822008-10-05 18:05:59 +00001021 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001022 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1023 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001024 return;
1025 }
1026 }
1027
1028 int nullPos = 0;
1029 if (Attr.getNumArgs() > 1) {
1030 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1031 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001032 if (E->isTypeDependent() || E->isValueDependent() ||
1033 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001035 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001036 return;
1037 }
1038 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001039
Anders Carlsson77091822008-10-05 18:05:59 +00001040 if (nullPos > 1 || nullPos < 0) {
1041 // FIXME: This error message could be improved, it would be nice
1042 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001043 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1044 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001045 return;
1046 }
1047 }
1048
1049 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001050 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001051 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001052
Chris Lattner897cd902009-03-17 23:03:47 +00001053 if (isa<FunctionNoProtoType>(FT)) {
1054 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1055 return;
1056 }
Mike Stumpbf916502009-07-24 19:02:52 +00001057
Chris Lattner897cd902009-03-17 23:03:47 +00001058 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001059 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001060 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001061 }
Anders Carlsson77091822008-10-05 18:05:59 +00001062 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1063 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001064 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001065 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001066 }
1067 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001068 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1069 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001070 ;
1071 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001072 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001073 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001074 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001075 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001076 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001077 int m = Ty->isFunctionPointerType() ? 0 : 1;
1078 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001079 return;
1080 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001081 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001082 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001083 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001084 return;
1085 }
Anders Carlsson77091822008-10-05 18:05:59 +00001086 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001087 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001088 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001089 return;
1090 }
Sean Huntcf807c42010-08-18 23:23:40 +00001091 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001092}
1093
Chris Lattner026dc962009-02-14 07:37:35 +00001094static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1095 // check the attribute arguments.
1096 if (Attr.getNumArgs() != 0) {
1097 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1098 return;
1099 }
1100
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001101 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001102 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001103 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001104 return;
1105 }
Mike Stumpbf916502009-07-24 19:02:52 +00001106
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001107 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1108 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1109 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001110 return;
1111 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001112 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1113 if (MD->getResultType()->isVoidType()) {
1114 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1115 << Attr.getName() << 1;
1116 return;
1117 }
1118
Sean Huntcf807c42010-08-18 23:23:40 +00001119 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001120}
1121
1122static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001123 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001124 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001126 return;
1127 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001128
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001129 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001130 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1132 dyn_cast<NamedDecl>(D)->getNameAsString();
1133 return;
1134 }
1135
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001136 // TODO: could also be applied to methods?
1137 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1138 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001139 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001140 return;
1141 }
Mike Stumpbf916502009-07-24 19:02:52 +00001142
Sean Huntcf807c42010-08-18 23:23:40 +00001143 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001144}
1145
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001146static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1147 // check the attribute arguments.
1148 if (Attr.getNumArgs() != 0) {
1149 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1150 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001151 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001152
1153 // weak_import only applies to variable & function declarations.
1154 bool isDef = false;
1155 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1156 isDef = (!VD->hasExternalStorage() || VD->getInit());
1157 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001158 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001159 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1160 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001161 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001162 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001163 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001164 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001165 !isa<ObjCInterfaceDecl>(D))
1166 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001167 << Attr.getName() << 2 /*variable and function*/;
1168 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001169 }
1170
1171 // Merge should handle any subsequent violations.
1172 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001173 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001174 diag::warn_attribute_weak_import_invalid_on_definition)
1175 << "weak_import" << 2 /*variable and function*/;
1176 return;
1177 }
1178
Sean Huntcf807c42010-08-18 23:23:40 +00001179 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001180}
1181
Nate Begeman6f3d8382009-06-26 06:32:41 +00001182static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1183 Sema &S) {
1184 // Attribute has 3 arguments.
1185 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001187 return;
1188 }
1189
1190 unsigned WGSize[3];
1191 for (unsigned i = 0; i < 3; ++i) {
1192 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1193 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001194 if (E->isTypeDependent() || E->isValueDependent() ||
1195 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001196 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1197 << "reqd_work_group_size" << E->getSourceRange();
1198 return;
1199 }
1200 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1201 }
Sean Huntcf807c42010-08-18 23:23:40 +00001202 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1203 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001204 WGSize[2]));
1205}
1206
Chris Lattner026dc962009-02-14 07:37:35 +00001207static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001208 // Attribute has no arguments.
1209 if (Attr.getNumArgs() != 1) {
1210 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1211 return;
1212 }
1213
1214 // Make sure that there is a string literal as the sections's single
1215 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001216 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1217 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001218 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001219 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001220 return;
1221 }
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Chris Lattner797c3c42009-08-10 19:03:04 +00001223 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001224 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001225 if (!Error.empty()) {
1226 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1227 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001228 return;
1229 }
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001231 // This attribute cannot be applied to local variables.
1232 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1233 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1234 return;
1235 }
1236
Sean Huntcf807c42010-08-18 23:23:40 +00001237 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001238}
1239
Chris Lattner6b6b5372008-06-26 18:38:35 +00001240
Chris Lattner803d0802008-06-29 00:43:07 +00001241static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001242 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001243 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001244 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001245 return;
1246 }
Mike Stumpbf916502009-07-24 19:02:52 +00001247
Sean Huntcf807c42010-08-18 23:23:40 +00001248 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001249}
1250
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001251static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1252 // check the attribute arguments.
1253 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001254 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001255 return;
1256 }
Mike Stumpbf916502009-07-24 19:02:52 +00001257
Sean Huntcf807c42010-08-18 23:23:40 +00001258 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001259}
1260
1261static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1262 // check the attribute arguments.
1263 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001265 return;
1266 }
Mike Stumpbf916502009-07-24 19:02:52 +00001267
Sean Huntcf807c42010-08-18 23:23:40 +00001268 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001269}
1270
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001271static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001272 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1274 return;
1275 }
Mike Stumpbf916502009-07-24 19:02:52 +00001276
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001277 if (Attr.getNumArgs() != 0) {
1278 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1279 return;
1280 }
Mike Stumpbf916502009-07-24 19:02:52 +00001281
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001282 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001283
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001284 if (!VD || !VD->hasLocalStorage()) {
1285 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1286 return;
1287 }
Mike Stumpbf916502009-07-24 19:02:52 +00001288
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001289 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001290 // FIXME: Lookup probably isn't looking in the right place
1291 // FIXME: The lookup source location should be in the attribute, not the
1292 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001293 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001294 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001295 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001296 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001297 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001298 Attr.getParameterName();
1299 return;
1300 }
Mike Stumpbf916502009-07-24 19:02:52 +00001301
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001302 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1303 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001304 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001305 Attr.getParameterName();
1306 return;
1307 }
1308
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001309 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001310 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001311 Attr.getParameterName();
1312 return;
1313 }
Mike Stumpbf916502009-07-24 19:02:52 +00001314
Anders Carlsson89941c12009-02-07 23:16:50 +00001315 // We're currently more strict than GCC about what function types we accept.
1316 // If this ever proves to be a problem it should be easy to fix.
1317 QualType Ty = S.Context.getPointerType(VD->getType());
1318 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001319 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001320 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001321 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1322 Attr.getParameterName() << ParamTy << Ty;
1323 return;
1324 }
Mike Stumpbf916502009-07-24 19:02:52 +00001325
Sean Huntcf807c42010-08-18 23:23:40 +00001326 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001327}
1328
Mike Stumpbf916502009-07-24 19:02:52 +00001329/// Handle __attribute__((format_arg((idx)))) attribute based on
1330/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1331static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001332 if (Attr.getNumArgs() != 1) {
1333 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1334 return;
1335 }
1336 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1337 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1338 << Attr.getName() << 0 /*function*/;
1339 return;
1340 }
Mike Stumpbf916502009-07-24 19:02:52 +00001341 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1342 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001343 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1344 unsigned FirstIdx = 1;
1345 // checks for the 2nd argument
1346 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1347 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001348 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1349 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001350 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1351 << "format" << 2 << IdxExpr->getSourceRange();
1352 return;
1353 }
Mike Stumpbf916502009-07-24 19:02:52 +00001354
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001355 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1356 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1357 << "format" << 2 << IdxExpr->getSourceRange();
1358 return;
1359 }
Mike Stumpbf916502009-07-24 19:02:52 +00001360
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001361 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001362
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001363 // make sure the format string is really a string
1364 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001365
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001366 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1367 if (not_nsstring_type &&
1368 !isCFStringType(Ty, S.Context) &&
1369 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001370 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001371 // FIXME: Should highlight the actual expression that has the wrong type.
1372 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001373 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001374 << IdxExpr->getSourceRange();
1375 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001376 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001377 Ty = getFunctionOrMethodResultType(d);
1378 if (!isNSStringType(Ty, S.Context) &&
1379 !isCFStringType(Ty, S.Context) &&
1380 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001381 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001382 // FIXME: Should highlight the actual expression that has the wrong type.
1383 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001384 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001385 << IdxExpr->getSourceRange();
1386 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001387 }
1388
Sean Huntcf807c42010-08-18 23:23:40 +00001389 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001390}
1391
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001392enum FormatAttrKind {
1393 CFStringFormat,
1394 NSStringFormat,
1395 StrftimeFormat,
1396 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001397 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001398 InvalidFormat
1399};
1400
1401/// getFormatAttrKind - Map from format attribute names to supported format
1402/// types.
1403static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1404 // Check for formats that get handled specially.
1405 if (Format == "NSString")
1406 return NSStringFormat;
1407 if (Format == "CFString")
1408 return CFStringFormat;
1409 if (Format == "strftime")
1410 return StrftimeFormat;
1411
1412 // Otherwise, check for supported formats.
1413 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1414 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1415 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1416 Format == "zcmn_err")
1417 return SupportedFormat;
1418
Duncan Sandsbc525952010-03-23 14:44:19 +00001419 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1420 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001421 return IgnoredFormat;
1422
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001423 return InvalidFormat;
1424}
1425
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001426/// Handle __attribute__((init_priority(priority))) attributes based on
1427/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1428static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1429 Sema &S) {
1430 if (!S.getLangOptions().CPlusPlus) {
1431 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1432 return;
1433 }
1434
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001435 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1436 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1437 Attr.setInvalid();
1438 return;
1439 }
1440 QualType T = dyn_cast<VarDecl>(d)->getType();
1441 if (S.Context.getAsArrayType(T))
1442 T = S.Context.getBaseElementType(T);
1443 if (!T->getAs<RecordType>()) {
1444 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1445 Attr.setInvalid();
1446 return;
1447 }
1448
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001449 if (Attr.getNumArgs() != 1) {
1450 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1451 Attr.setInvalid();
1452 return;
1453 }
1454 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001455
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001456 llvm::APSInt priority(32);
1457 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1458 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1460 << "init_priority" << priorityExpr->getSourceRange();
1461 Attr.setInvalid();
1462 return;
1463 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001464 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001465 if (prioritynum < 101 || prioritynum > 65535) {
1466 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1467 << priorityExpr->getSourceRange();
1468 Attr.setInvalid();
1469 return;
1470 }
Sean Huntcf807c42010-08-18 23:23:40 +00001471 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001472}
1473
Mike Stumpbf916502009-07-24 19:02:52 +00001474/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1475/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001476static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001477
Chris Lattner545dd342008-06-28 23:36:30 +00001478 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001479 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001480 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001481 return;
1482 }
1483
Chris Lattner545dd342008-06-28 23:36:30 +00001484 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001485 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001486 return;
1487 }
1488
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001489 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001490 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001491 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001492 return;
1493 }
1494
Daniel Dunbar35682492008-09-26 04:12:28 +00001495 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001496 unsigned FirstIdx = 1;
1497
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001498 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001499
1500 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001501 if (Format.startswith("__") && Format.endswith("__"))
1502 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001503
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001504 // Check for supported formats.
1505 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001506
1507 if (Kind == IgnoredFormat)
1508 return;
1509
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001510 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001511 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001512 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001513 return;
1514 }
1515
1516 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001517 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001518 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001519 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1520 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001521 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001522 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001523 return;
1524 }
1525
Anders Carlsson4fb77202009-08-25 14:12:34 +00001526 // FIXME: We should handle the implicit 'this' parameter in a more generic
1527 // way that can be used for other arguments.
1528 bool HasImplicitThisParam = false;
1529 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1530 if (MD->isInstance()) {
1531 HasImplicitThisParam = true;
1532 NumArgs++;
1533 }
1534 }
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Chris Lattner6b6b5372008-06-26 18:38:35 +00001536 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001537 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001538 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001539 return;
1540 }
1541
1542 // FIXME: Do we need to bounds check?
1543 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001544
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001545 if (HasImplicitThisParam) {
1546 if (ArgIdx == 0) {
1547 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1548 << "a string type" << IdxExpr->getSourceRange();
1549 return;
1550 }
1551 ArgIdx--;
1552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Chris Lattner6b6b5372008-06-26 18:38:35 +00001554 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001555 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001556
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001557 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001558 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001559 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1560 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001561 return;
1562 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001563 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001564 // FIXME: do we need to check if the type is NSString*? What are the
1565 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001566 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001567 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001568 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1569 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001570 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001571 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001572 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001573 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001574 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001575 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1576 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001577 return;
1578 }
1579
1580 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001581 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001582 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001583 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1584 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001585 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001586 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001587 return;
1588 }
1589
1590 // check if the function is variadic if the 3rd argument non-zero
1591 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001592 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001593 ++NumArgs; // +1 for ...
1594 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001595 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001596 return;
1597 }
1598 }
1599
Chris Lattner3c73c412008-11-19 08:23:25 +00001600 // strftime requires FirstArg to be 0 because it doesn't read from any
1601 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001602 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001603 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001604 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1605 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001606 return;
1607 }
1608 // if 0 it disables parameter checking (to use with e.g. va_list)
1609 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001611 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001612 return;
1613 }
1614
Sean Huntcf807c42010-08-18 23:23:40 +00001615 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1616 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001617 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001618}
1619
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001620static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1621 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001622 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001623 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001624 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001625 return;
1626 }
1627
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001628 // Try to find the underlying union declaration.
1629 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001630 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001631 if (TD && TD->getUnderlyingType()->isUnionType())
1632 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1633 else
1634 RD = dyn_cast<RecordDecl>(d);
1635
1636 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001638 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001639 return;
1640 }
1641
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001642 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001643 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001644 diag::warn_transparent_union_attribute_not_definition);
1645 return;
1646 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001647
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001648 RecordDecl::field_iterator Field = RD->field_begin(),
1649 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001650 if (Field == FieldEnd) {
1651 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1652 return;
1653 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001654
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001655 FieldDecl *FirstField = *Field;
1656 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001657 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001658 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001659 diag::warn_transparent_union_attribute_floating)
1660 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001661 return;
1662 }
1663
1664 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1665 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1666 for (; Field != FieldEnd; ++Field) {
1667 QualType FieldType = Field->getType();
1668 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1669 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1670 // Warn if we drop the attribute.
1671 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001672 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001673 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001674 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001675 diag::warn_transparent_union_attribute_field_size_align)
1676 << isSize << Field->getDeclName() << FieldBits;
1677 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001678 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001679 diag::note_transparent_union_first_field_size_align)
1680 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001681 return;
1682 }
1683 }
1684
Sean Huntcf807c42010-08-18 23:23:40 +00001685 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001686}
1687
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001688static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001689 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001690 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001691 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001692 return;
1693 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001694 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1695 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001696
Chris Lattner6b6b5372008-06-26 18:38:35 +00001697 // Make sure that there is a string literal as the annotation's single
1698 // argument.
1699 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001700 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001701 return;
1702 }
Sean Huntcf807c42010-08-18 23:23:40 +00001703 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001704}
1705
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001706static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001707 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001708 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001709 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001710 return;
1711 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001712
1713 //FIXME: The C++0x version of this attribute has more limited applicabilty
1714 // than GNU's, and should error out when it is used to specify a
1715 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001716
Chris Lattner545dd342008-06-28 23:36:30 +00001717 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001718 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001719 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001720 }
Mike Stumpbf916502009-07-24 19:02:52 +00001721
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001722 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1723}
1724
1725void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1726 if (E->isTypeDependent() || E->isValueDependent()) {
1727 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001728 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001729 return;
1730 }
1731
Sean Huntcf807c42010-08-18 23:23:40 +00001732 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001733 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001734 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1735 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1736 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001737 return;
1738 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001739 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001740 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1741 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001742 return;
1743 }
1744
Sean Huntcf807c42010-08-18 23:23:40 +00001745 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1746}
1747
1748void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1749 // FIXME: Cache the number on the Attr object if non-dependent?
1750 // FIXME: Perform checking of type validity
1751 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1752 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001753}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001754
Mike Stumpbf916502009-07-24 19:02:52 +00001755/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1756/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001757///
Mike Stumpbf916502009-07-24 19:02:52 +00001758/// Despite what would be logical, the mode attribute is a decl attribute, not a
1759/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1760/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001761static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001762 // This attribute isn't documented, but glibc uses it. It changes
1763 // the width of an int or unsigned int to the specified size.
1764
1765 // Check that there aren't any arguments
1766 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001767 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001768 return;
1769 }
1770
1771 IdentifierInfo *Name = Attr.getParameterName();
1772 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001773 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001774 return;
1775 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001776
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001777 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001778
1779 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001780 if (Str.startswith("__") && Str.endswith("__"))
1781 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001782
1783 unsigned DestWidth = 0;
1784 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001785 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001786 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001787 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001788 switch (Str[0]) {
1789 case 'Q': DestWidth = 8; break;
1790 case 'H': DestWidth = 16; break;
1791 case 'S': DestWidth = 32; break;
1792 case 'D': DestWidth = 64; break;
1793 case 'X': DestWidth = 96; break;
1794 case 'T': DestWidth = 128; break;
1795 }
1796 if (Str[1] == 'F') {
1797 IntegerMode = false;
1798 } else if (Str[1] == 'C') {
1799 IntegerMode = false;
1800 ComplexMode = true;
1801 } else if (Str[1] != 'I') {
1802 DestWidth = 0;
1803 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001804 break;
1805 case 4:
1806 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1807 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001808 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001809 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001810 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001811 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001812 break;
1813 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001814 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001815 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001816 break;
1817 }
1818
1819 QualType OldTy;
1820 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1821 OldTy = TD->getUnderlyingType();
1822 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1823 OldTy = VD->getType();
1824 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001825 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1826 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001827 return;
1828 }
Eli Friedman73397492009-03-03 06:41:03 +00001829
John McCall183700f2009-09-21 23:43:11 +00001830 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001831 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1832 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001833 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001834 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1835 } else if (ComplexMode) {
1836 if (!OldTy->isComplexType())
1837 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1838 } else {
1839 if (!OldTy->isFloatingType())
1840 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1841 }
1842
Mike Stump390b4cc2009-05-16 07:39:55 +00001843 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1844 // and friends, at least with glibc.
1845 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1846 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001847 // FIXME: Make sure floating-point mappings are accurate
1848 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001849 QualType NewTy;
1850 switch (DestWidth) {
1851 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001852 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001853 return;
1854 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001855 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001856 return;
1857 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001858 if (!IntegerMode) {
1859 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1860 return;
1861 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001862 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001863 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001864 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001865 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001866 break;
1867 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001868 if (!IntegerMode) {
1869 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1870 return;
1871 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001872 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001873 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001874 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001875 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001876 break;
1877 case 32:
1878 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001879 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001880 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001881 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001882 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001883 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001884 break;
1885 case 64:
1886 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001887 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001888 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001889 if (S.Context.Target.getLongWidth() == 64)
1890 NewTy = S.Context.LongTy;
1891 else
1892 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001893 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001894 if (S.Context.Target.getLongWidth() == 64)
1895 NewTy = S.Context.UnsignedLongTy;
1896 else
1897 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001898 break;
Eli Friedman73397492009-03-03 06:41:03 +00001899 case 96:
1900 NewTy = S.Context.LongDoubleTy;
1901 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001902 case 128:
1903 if (!IntegerMode) {
1904 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1905 return;
1906 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001907 if (OldTy->isSignedIntegerType())
1908 NewTy = S.Context.Int128Ty;
1909 else
1910 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001911 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001912 }
1913
Eli Friedman73397492009-03-03 06:41:03 +00001914 if (ComplexMode) {
1915 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001916 }
1917
1918 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001919 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1920 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001921 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001922 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001923 cast<ValueDecl>(D)->setType(NewTy);
1924}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001925
Mike Stump1feade82009-08-26 22:31:08 +00001926static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001927 // check the attribute arguments.
1928 if (Attr.getNumArgs() > 0) {
1929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1930 return;
1931 }
Anders Carlssone896d982009-02-13 08:11:52 +00001932
Anders Carlsson5bab7882009-02-19 19:16:48 +00001933 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001934 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001935 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001936 return;
1937 }
Mike Stumpbf916502009-07-24 19:02:52 +00001938
Sean Huntcf807c42010-08-18 23:23:40 +00001939 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00001940}
1941
Mike Stump1feade82009-08-26 22:31:08 +00001942static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001943 // check the attribute arguments.
1944 if (Attr.getNumArgs() != 0) {
1945 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1946 return;
1947 }
Mike Stumpbf916502009-07-24 19:02:52 +00001948
Chris Lattnerc5197432009-04-14 17:02:11 +00001949 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001950 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001951 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001952 return;
1953 }
Mike Stumpbf916502009-07-24 19:02:52 +00001954
Sean Huntcf807c42010-08-18 23:23:40 +00001955 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00001956}
1957
Chris Lattner7255a2d2010-06-22 00:03:40 +00001958static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1959 Sema &S) {
1960 // check the attribute arguments.
1961 if (Attr.getNumArgs() != 0) {
1962 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1963 return;
1964 }
1965
1966 if (!isa<FunctionDecl>(d)) {
1967 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1968 << Attr.getName() << 0 /*function*/;
1969 return;
1970 }
1971
Sean Huntcf807c42010-08-18 23:23:40 +00001972 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00001973}
1974
Chris Lattnercf2a7212009-04-20 19:12:28 +00001975static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001976 // check the attribute arguments.
1977 if (Attr.getNumArgs() != 0) {
1978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1979 return;
1980 }
Mike Stumpbf916502009-07-24 19:02:52 +00001981
Chris Lattnerc5197432009-04-14 17:02:11 +00001982 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1983 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001984 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001985 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001986 return;
1987 }
Mike Stumpbf916502009-07-24 19:02:52 +00001988
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001989 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001990 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001991 return;
1992 }
Mike Stumpbf916502009-07-24 19:02:52 +00001993
Sean Huntcf807c42010-08-18 23:23:40 +00001994 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00001995}
1996
Abramo Bagnarae215f722010-04-30 13:10:51 +00001997static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1998 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1999 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2000 assert(Attr.isInvalid() == false);
2001
2002 switch (Attr.getKind()) {
2003 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002004 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002005 return;
2006 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002007 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002008 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002009 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00002010 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002011 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002012 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00002013 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002014 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002015 case AttributeList::AT_pascal:
2016 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2017 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002018 default:
2019 llvm_unreachable("unexpected attribute kind");
2020 return;
2021 }
2022}
2023
Fariborz Jahanianee760332009-03-27 18:38:55 +00002024static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2025 // check the attribute arguments.
2026 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002027 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002028 return;
2029 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002030
Fariborz Jahanianee760332009-03-27 18:38:55 +00002031 if (!isFunctionOrMethod(d)) {
2032 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002033 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002034 return;
2035 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002036
2037 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2038 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002039 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2040 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002041 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2042 << "regparm" << NumParamsExpr->getSourceRange();
2043 return;
2044 }
2045
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002046 if (S.Context.Target.getRegParmMax() == 0) {
2047 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002048 << NumParamsExpr->getSourceRange();
2049 return;
2050 }
2051
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002052 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002053 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2054 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002055 return;
2056 }
2057
Sean Huntcf807c42010-08-18 23:23:40 +00002058 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2059 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002060}
2061
Sean Huntbbd37c62009-11-21 08:43:09 +00002062static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2063 // check the attribute arguments.
2064 if (Attr.getNumArgs() != 0) {
2065 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2066 return;
2067 }
2068
2069 if (!isa<CXXRecordDecl>(d)
2070 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2071 S.Diag(Attr.getLoc(),
2072 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2073 : diag::warn_attribute_wrong_decl_type)
2074 << Attr.getName() << 7 /*virtual method or class*/;
2075 return;
2076 }
Sean Hunt7725e672009-11-25 04:20:27 +00002077
2078 // FIXME: Conform to C++0x redeclaration rules.
2079
2080 if (d->getAttr<FinalAttr>()) {
2081 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2082 return;
2083 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002084
Sean Huntcf807c42010-08-18 23:23:40 +00002085 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002086}
2087
Chris Lattner0744e5f2008-06-29 00:23:49 +00002088//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002089// C++0x member checking attributes
2090//===----------------------------------------------------------------------===//
2091
2092static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2093 if (Attr.getNumArgs() != 0) {
2094 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2095 return;
2096 }
2097
2098 if (!isa<CXXRecordDecl>(d)) {
2099 S.Diag(Attr.getLoc(),
2100 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2101 : diag::warn_attribute_wrong_decl_type)
2102 << Attr.getName() << 9 /*class*/;
2103 return;
2104 }
2105
2106 if (d->getAttr<BaseCheckAttr>()) {
2107 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2108 return;
2109 }
2110
Sean Huntcf807c42010-08-18 23:23:40 +00002111 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002112}
2113
2114static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2115 if (Attr.getNumArgs() != 0) {
2116 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2117 return;
2118 }
2119
2120 if (!isa<RecordDecl>(d->getDeclContext())) {
2121 // FIXME: It's not the type that's the problem
2122 S.Diag(Attr.getLoc(),
2123 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2124 : diag::warn_attribute_wrong_decl_type)
2125 << Attr.getName() << 11 /*member*/;
2126 return;
2127 }
2128
2129 // FIXME: Conform to C++0x redeclaration rules.
2130
2131 if (d->getAttr<HidingAttr>()) {
2132 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2133 return;
2134 }
2135
Sean Huntcf807c42010-08-18 23:23:40 +00002136 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002137}
2138
2139static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2140 if (Attr.getNumArgs() != 0) {
2141 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2142 return;
2143 }
2144
2145 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2146 // FIXME: It's not the type that's the problem
2147 S.Diag(Attr.getLoc(),
2148 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2149 : diag::warn_attribute_wrong_decl_type)
2150 << Attr.getName() << 10 /*virtual method*/;
2151 return;
2152 }
2153
2154 // FIXME: Conform to C++0x redeclaration rules.
2155
2156 if (d->getAttr<OverrideAttr>()) {
2157 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2158 return;
2159 }
2160
Sean Huntcf807c42010-08-18 23:23:40 +00002161 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002162}
2163
2164//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002165// Checker-specific attribute handlers.
2166//===----------------------------------------------------------------------===//
2167
2168static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2169 Sema &S) {
2170
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002171 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002172
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002173 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2174 RetTy = MD->getResultType();
2175 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2176 RetTy = FD->getResultType();
2177 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002178 SourceLocation L = Attr.getLoc();
2179 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2180 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002181 return;
2182 }
Mike Stumpbf916502009-07-24 19:02:52 +00002183
Ted Kremenek6217b802009-07-29 21:53:49 +00002184 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002185 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002186 SourceLocation L = Attr.getLoc();
2187 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2188 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002189 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002190 }
Mike Stumpbf916502009-07-24 19:02:52 +00002191
Ted Kremenekb71368d2009-05-09 02:44:38 +00002192 switch (Attr.getKind()) {
2193 default:
2194 assert(0 && "invalid ownership attribute");
2195 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002196 case AttributeList::AT_cf_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002197 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002198 return;
2199 case AttributeList::AT_ns_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002200 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002201 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002202 case AttributeList::AT_cf_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002203 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002204 return;
2205 case AttributeList::AT_ns_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002206 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002207 return;
2208 };
2209}
2210
Charles Davisf0122fe2010-02-16 18:27:26 +00002211static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2212 return Attr.getKind() == AttributeList::AT_dllimport ||
2213 Attr.getKind() == AttributeList::AT_dllexport;
2214}
2215
Ted Kremenekb71368d2009-05-09 02:44:38 +00002216//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002217// Top Level Sema Entry Points
2218//===----------------------------------------------------------------------===//
2219
Sebastian Redla89d82c2008-12-21 19:24:58 +00002220/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002221/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002222/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2223/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002224static void ProcessDeclAttribute(Scope *scope, Decl *D,
2225 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002226 if (Attr.isInvalid())
2227 return;
2228
Charles Davisf0122fe2010-02-16 18:27:26 +00002229 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2230 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002231 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002232 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002233 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002234 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2235 case AttributeList::AT_IBOutletCollection:
2236 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002237 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002238 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002239 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002240 // Ignore these, these are type attributes, handled by
2241 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002242 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002243 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2244 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002245 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002246 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002247 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002248 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002249 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2250 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002251 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002252 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002253 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2254 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2255 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002256 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002257 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002258 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002259 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2260 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2261 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2262 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2263 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2264 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2265 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2266 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002267 case AttributeList::AT_ownership_returns:
2268 case AttributeList::AT_ownership_takes:
2269 case AttributeList::AT_ownership_holds:
2270 HandleOwnershipAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002271 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2272 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2273 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002274 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002275
2276 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002277 case AttributeList::AT_ns_returns_not_retained:
2278 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002279 case AttributeList::AT_ns_returns_retained:
2280 case AttributeList::AT_cf_returns_retained:
2281 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2282
Nate Begeman6f3d8382009-06-26 06:32:41 +00002283 case AttributeList::AT_reqd_wg_size:
2284 HandleReqdWorkGroupSize(D, Attr, S); break;
2285
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002286 case AttributeList::AT_init_priority:
2287 HandleInitPriorityAttr(D, Attr, S); break;
2288
Sean Hunt7725e672009-11-25 04:20:27 +00002289 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2290 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002291 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2292 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2293 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002294 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002295 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2296 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002297 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002298 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002299 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002300 case AttributeList::AT_transparent_union:
2301 HandleTransparentUnionAttr(D, Attr, S);
2302 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002303 case AttributeList::AT_objc_exception:
2304 HandleObjCExceptionAttr(D, Attr, S);
2305 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002306 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002307 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2308 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2309 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2310 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2311 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2312 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2313 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2314 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2315 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002316 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002317 // Just ignore
2318 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002319 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2320 HandleNoInstrumentFunctionAttr(D, Attr, S);
2321 break;
John McCall04a67a62010-02-05 21:31:56 +00002322 case AttributeList::AT_stdcall:
2323 case AttributeList::AT_cdecl:
2324 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002325 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002326 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002327 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002328 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002329 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002330 // Ask target about the attribute.
2331 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2332 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002333 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2334 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002335 break;
2336 }
2337}
2338
2339/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2340/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002341void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002342 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2343 ProcessDeclAttribute(S, D, *l, *this);
2344 }
2345
2346 // GCC accepts
2347 // static int a9 __attribute__((weakref));
2348 // but that looks really pointless. We reject it.
2349 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2350 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002351 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002352 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002353 }
2354}
2355
Ryan Flynne25ff832009-07-30 03:15:39 +00002356/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2357/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002358NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002359 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002360 NamedDecl *NewD = 0;
2361 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2362 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2363 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002364 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002365 if (FD->getQualifier()) {
2366 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2367 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2368 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002369 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2370 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2371 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002372 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002373 VD->getStorageClass(),
2374 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002375 if (VD->getQualifier()) {
2376 VarDecl *NewVD = cast<VarDecl>(NewD);
2377 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2378 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002379 }
2380 return NewD;
2381}
2382
2383/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2384/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002385void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002386 if (W.getUsed()) return; // only do this once
2387 W.setUsed(true);
2388 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2389 IdentifierInfo *NDId = ND->getIdentifier();
2390 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002391 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2392 NDId->getName()));
2393 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002394 WeakTopLevelDecl.push_back(NewD);
2395 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2396 // to insert Decl at TU scope, sorry.
2397 DeclContext *SavedContext = CurContext;
2398 CurContext = Context.getTranslationUnitDecl();
2399 PushOnScopeChains(NewD, S);
2400 CurContext = SavedContext;
2401 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002402 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002403 }
2404}
2405
Chris Lattner0744e5f2008-06-29 00:23:49 +00002406/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2407/// it, apply them to D. This is a bit tricky because PD can have attributes
2408/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002409void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002410 // Handle #pragma weak
2411 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2412 if (ND->hasLinkage()) {
2413 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2414 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002415 // Identifier referenced by #pragma weak before it was declared
2416 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002417 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2418 }
2419 }
2420 }
2421
Chris Lattner0744e5f2008-06-29 00:23:49 +00002422 // Apply decl attributes from the DeclSpec if present.
2423 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002424 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002425
Chris Lattner0744e5f2008-06-29 00:23:49 +00002426 // Walk the declarator structure, applying decl attributes that were in a type
2427 // position to the decl itself. This handles cases like:
2428 // int *__attr__(x)** D;
2429 // when X is a decl attribute.
2430 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2431 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002432 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002433
Chris Lattner0744e5f2008-06-29 00:23:49 +00002434 // Finally, apply any attributes on the decl itself.
2435 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002436 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002437}
John McCall54abf7d2009-11-04 02:18:39 +00002438
2439/// PushParsingDeclaration - Enter a new "scope" of deprecation
2440/// warnings.
2441///
2442/// The state token we use is the start index of this scope
2443/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002444Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002445 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002446 return (ParsingDeclStackState) DelayedDiagnostics.size();
2447}
2448
John McCalld226f652010-08-21 09:40:31 +00002449void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002450 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2451 ParsingDeclDepth--;
2452
2453 if (DelayedDiagnostics.empty())
2454 return;
2455
2456 unsigned SavedIndex = (unsigned) S;
2457 assert(SavedIndex <= DelayedDiagnostics.size() &&
2458 "saved index is out of bounds");
2459
John McCall58e6f342010-03-16 05:22:47 +00002460 unsigned E = DelayedDiagnostics.size();
2461
John McCall2f514482010-01-27 03:50:35 +00002462 // We only want to actually emit delayed diagnostics when we
2463 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002464 if (D) {
2465 // We really do want to start with 0 here. We get one push for a
2466 // decl spec and another for each declarator; in a decl group like:
2467 // deprecated_typedef foo, *bar, baz();
2468 // only the declarator pops will be passed decls. This is correct;
2469 // we really do need to consider delayed diagnostics from the decl spec
2470 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002471 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002472 if (DelayedDiagnostics[I].Triggered)
2473 continue;
2474
2475 switch (DelayedDiagnostics[I].Kind) {
2476 case DelayedDiagnostic::Deprecation:
2477 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2478 break;
2479
2480 case DelayedDiagnostic::Access:
2481 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2482 break;
2483 }
2484 }
2485 }
2486
John McCall58e6f342010-03-16 05:22:47 +00002487 // Destroy all the delayed diagnostics we're about to pop off.
2488 for (unsigned I = SavedIndex; I != E; ++I)
2489 DelayedDiagnostics[I].destroy();
2490
John McCall2f514482010-01-27 03:50:35 +00002491 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002492}
2493
2494static bool isDeclDeprecated(Decl *D) {
2495 do {
2496 if (D->hasAttr<DeprecatedAttr>())
2497 return true;
2498 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2499 return false;
2500}
2501
John McCall9c3087b2010-08-26 02:13:20 +00002502void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002503 Decl *Ctx) {
2504 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002505 return;
2506
John McCall2f514482010-01-27 03:50:35 +00002507 DD.Triggered = true;
2508 Diag(DD.Loc, diag::warn_deprecated)
2509 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002510}
2511
2512void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2513 // Delay if we're currently parsing a declaration.
2514 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002515 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002516 return;
2517 }
2518
2519 // Otherwise, don't warn if our current context is deprecated.
2520 if (isDeclDeprecated(cast<Decl>(CurContext)))
2521 return;
2522
2523 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2524}