blob: 9800db09e30251a2a1b21e339ed00f7d4db2bbbe [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);
Fariborz Jahanianff3a0782010-09-27 22:42:37 +0000374 else if (const RecordType *UT = T->getAsUnionType()) {
375 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
376 RecordDecl *UD = UT->getDecl();
377 for (RecordDecl::field_iterator it = UD->field_begin(),
378 itend = UD->field_end(); it != itend; ++it) {
379 T = it->getType();
380 if (T->isAnyPointerType() || T->isBlockPointerType()) {
381 NonNullArgs.push_back(I);
382 break;
383 }
384 }
385 }
386 }
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000387 }
Mike Stumpbf916502009-07-24 19:02:52 +0000388
Ted Kremenek1db5d142010-09-09 01:17:32 +0000389 // No pointer arguments? The attribute in this case is
390 // trivially satisfied.
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000391 if (NonNullArgs.empty()) {
392 // Warn the trivial case only if attribute is not coming from a
393 // macro instantiation.
394 if (Attr.getLoc().isFileID())
395 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000396 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000397 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000398 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000399
400 unsigned* start = &NonNullArgs[0];
401 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000402 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000403 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
404 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000405}
406
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000407static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
408 // This attribute must be applied to a function declaration.
409 // The first argument to the attribute must be a string,
410 // the name of the resource, for example "malloc".
411 // The following arguments must be argument indexes, the arguments must be
412 // of integer type for Returns, otherwise of pointer type.
413 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000414 // after being held. free() should be __attribute((ownership_takes)), whereas
415 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000416
417 if (!AL.getParameterName()) {
418 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
419 << AL.getName()->getName() << 1;
420 return;
421 }
422 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000423 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000424 switch (AL.getKind()) {
425 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000426 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000427 if (AL.getNumArgs() < 1) {
428 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
429 return;
430 }
Jordy Rose2a479922010-08-12 08:54:03 +0000431 break;
432 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000433 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000434 if (AL.getNumArgs() < 1) {
435 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
436 return;
437 }
Jordy Rose2a479922010-08-12 08:54:03 +0000438 break;
439 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000440 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000441 if (AL.getNumArgs() > 1) {
442 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
443 << AL.getNumArgs() + 1;
444 return;
445 }
Jordy Rose2a479922010-08-12 08:54:03 +0000446 break;
447 default:
448 // This should never happen given how we are called.
449 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000450 }
451
452 if (!isFunction(d) || !hasFunctionProto(d)) {
453 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
454 << 0 /*function*/;
455 return;
456 }
457
458 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
459
460 llvm::StringRef Module = AL.getParameterName()->getName();
461
462 // Normalize the argument, __foo__ becomes foo.
463 if (Module.startswith("__") && Module.endswith("__"))
464 Module = Module.substr(2, Module.size() - 4);
465
466 llvm::SmallVector<unsigned, 10> OwnershipArgs;
467
Jordy Rose2a479922010-08-12 08:54:03 +0000468 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
469 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000470
471 Expr *IdxExpr = static_cast<Expr *>(*I);
472 llvm::APSInt ArgNum(32);
473 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
474 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
475 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
476 << AL.getName()->getName() << IdxExpr->getSourceRange();
477 continue;
478 }
479
480 unsigned x = (unsigned) ArgNum.getZExtValue();
481
482 if (x > NumArgs || x < 1) {
483 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
484 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
485 continue;
486 }
487 --x;
488 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000489 case OwnershipAttr::Takes:
490 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000491 // Is the function argument a pointer type?
492 QualType T = getFunctionOrMethodArgType(d, x);
493 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
494 // FIXME: Should also highlight argument in decl.
495 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000496 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000497 << "pointer"
498 << IdxExpr->getSourceRange();
499 continue;
500 }
501 break;
502 }
Sean Huntcf807c42010-08-18 23:23:40 +0000503 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000504 if (AL.getNumArgs() > 1) {
505 // Is the function argument an integer type?
506 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
507 llvm::APSInt ArgNum(32);
508 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
509 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
510 S.Diag(AL.getLoc(), diag::err_ownership_type)
511 << "ownership_returns" << "integer"
512 << IdxExpr->getSourceRange();
513 return;
514 }
515 }
516 break;
517 }
Jordy Rose2a479922010-08-12 08:54:03 +0000518 default:
519 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000520 } // switch
521
522 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000523 for (specific_attr_iterator<OwnershipAttr>
524 i = d->specific_attr_begin<OwnershipAttr>(),
525 e = d->specific_attr_end<OwnershipAttr>();
526 i != e; ++i) {
527 if ((*i)->getOwnKind() != K) {
528 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
529 I!=E; ++I) {
530 if (x == *I) {
531 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
532 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000533 }
534 }
535 }
536 }
537 OwnershipArgs.push_back(x);
538 }
539
540 unsigned* start = OwnershipArgs.data();
541 unsigned size = OwnershipArgs.size();
542 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000543
544 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
545 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
546 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000547 }
Sean Huntcf807c42010-08-18 23:23:40 +0000548
549 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
550 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000551}
552
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000553static bool isStaticVarOrStaticFunciton(Decl *D) {
554 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000555 return VD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000556 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000557 return FD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000558 return false;
559}
560
561static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
562 // Check the attribute arguments.
563 if (Attr.getNumArgs() > 1) {
564 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
565 return;
566 }
567
568 // gcc rejects
569 // class c {
570 // static int a __attribute__((weakref ("v2")));
571 // static int b() __attribute__((weakref ("f3")));
572 // };
573 // and ignores the attributes of
574 // void f(void) {
575 // static int a __attribute__((weakref ("v2")));
576 // }
577 // we reject them
Sebastian Redl7a126a42010-08-31 00:36:30 +0000578 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
579 if (!Ctx->isFileContext()) {
580 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
581 dyn_cast<NamedDecl>(d)->getNameAsString();
582 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000583 }
584
585 // The GCC manual says
586 //
587 // At present, a declaration to which `weakref' is attached can only
588 // be `static'.
589 //
590 // It also says
591 //
592 // Without a TARGET,
593 // given as an argument to `weakref' or to `alias', `weakref' is
594 // equivalent to `weak'.
595 //
596 // gcc 4.4.1 will accept
597 // int a7 __attribute__((weakref));
598 // as
599 // int a7 __attribute__((weak));
600 // This looks like a bug in gcc. We reject that for now. We should revisit
601 // it if this behaviour is actually used.
602
603 if (!isStaticVarOrStaticFunciton(d)) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
605 dyn_cast<NamedDecl>(d)->getNameAsString();
606 return;
607 }
608
609 // GCC rejects
610 // static ((alias ("y"), weakref)).
611 // Should we? How to check that weakref is before or after alias?
612
613 if (Attr.getNumArgs() == 1) {
614 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
615 Arg = Arg->IgnoreParenCasts();
616 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
617
618 if (Str == 0 || Str->isWide()) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
620 << "weakref" << 1;
621 return;
622 }
623 // GCC will accept anything as the argument of weakref. Should we
624 // check for an existing decl?
Sean Huntcf807c42010-08-18 23:23:40 +0000625 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000626 }
627
Sean Huntcf807c42010-08-18 23:23:40 +0000628 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000629}
630
Chris Lattner803d0802008-06-29 00:43:07 +0000631static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000632 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000633 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000635 return;
636 }
Mike Stumpbf916502009-07-24 19:02:52 +0000637
Chris Lattner545dd342008-06-28 23:36:30 +0000638 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000639 Arg = Arg->IgnoreParenCasts();
640 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000641
Chris Lattner6b6b5372008-06-26 18:38:35 +0000642 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000643 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000644 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000645 return;
646 }
Mike Stumpbf916502009-07-24 19:02:52 +0000647
Chris Lattner6b6b5372008-06-26 18:38:35 +0000648 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000649
Sean Huntcf807c42010-08-18 23:23:40 +0000650 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000651}
652
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000653static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000654 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000655 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000656 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000658 return;
659 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000660
Chris Lattnerc5197432009-04-14 17:02:11 +0000661 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000663 << Attr.getName() << 0 /*function*/;
664 return;
665 }
666
667 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
668}
669
670static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
671 Sema &S) {
672 // Check the attribute arguments.
673 if (Attr.getNumArgs() != 0) {
674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
675 return;
676 }
677
678 if (!isa<FunctionDecl>(d)) {
679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
680 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000681 return;
682 }
Mike Stumpbf916502009-07-24 19:02:52 +0000683
Sean Huntcf807c42010-08-18 23:23:40 +0000684 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000685}
686
Ryan Flynn76168e22009-08-09 20:07:29 +0000687static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000688 // Check the attribute arguments.
Ryan Flynn76168e22009-08-09 20:07:29 +0000689 if (Attr.getNumArgs() != 0) {
690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
691 return;
692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000694 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000695 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000696 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000697 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000698 return;
699 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000700 }
701
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000702 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000703}
704
Ted Kremenekb7252322009-04-10 00:01:14 +0000705static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000706 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
707 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000708 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000709}
710
711static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
712 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000713
714 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
715 // because 'analyzer_noreturn' does not impact the type.
716
717 if (Attr.getNumArgs() != 0) {
718 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
719 return;
720 }
721
722 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
723 ValueDecl *VD = dyn_cast<ValueDecl>(d);
724 if (VD == 0 || (!VD->getType()->isBlockPointerType()
725 && !VD->getType()->isFunctionPointerType())) {
726 S.Diag(Attr.getLoc(),
727 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
728 : diag::warn_attribute_wrong_decl_type)
729 << Attr.getName() << 0 /*function*/;
730 return;
731 }
732 }
733
734 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000735}
736
John Thompson35cc9622010-08-09 21:53:52 +0000737// PS3 PPU-specific.
738static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
739 Sema &S) {
740/*
741 Returning a Vector Class in Registers
742
743 According to the PPU ABI specifications, a class with a single member of vector type is returned in
744 memory when used as the return value of a function. This results in inefficient code when implementing
745 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
746 definition. This attribute is also applicable to struct types.
747
748 Example:
749
750 struct Vector
751 {
752 __vector float xyzw;
753 } __attribute__((vecreturn));
754
755 Vector Add(Vector lhs, Vector rhs)
756 {
757 Vector result;
758 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
759 return result; // This will be returned in a register
760 }
761*/
John Thompson01add592010-09-18 01:12:07 +0000762 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000763 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
764 << Attr.getName() << 9 /*class*/;
765 return;
766 }
767
768 if (d->getAttr<VecReturnAttr>()) {
769 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
770 return;
771 }
772
John Thompson01add592010-09-18 01:12:07 +0000773 RecordDecl *record = cast<RecordDecl>(d);
774 int count = 0;
775
776 if (!isa<CXXRecordDecl>(record)) {
777 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
778 return;
779 }
780
781 if (!cast<CXXRecordDecl>(record)->isPOD()) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
783 return;
784 }
785
786 for (RecordDecl::field_iterator iter = record->field_begin(); iter != record->field_end(); iter++) {
787 if ((count == 1) || !iter->getType()->isVectorType()) {
788 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
789 return;
790 }
791 count++;
792 }
793
Sean Huntcf807c42010-08-18 23:23:40 +0000794 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000795}
796
Sean Huntbbd37c62009-11-21 08:43:09 +0000797static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
798 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
799 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000800 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000801 return;
802 }
803 // FIXME: Actually store the attribute on the declaration
804}
805
Ted Kremenek73798892008-07-25 04:39:19 +0000806static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
807 // check the attribute arguments.
808 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000810 return;
811 }
Mike Stumpbf916502009-07-24 19:02:52 +0000812
John McCallaec58602010-03-31 02:47:45 +0000813 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
814 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000815 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000816 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000817 return;
818 }
Mike Stumpbf916502009-07-24 19:02:52 +0000819
Sean Huntcf807c42010-08-18 23:23:40 +0000820 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000821}
822
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000823static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
824 // check the attribute arguments.
825 if (Attr.getNumArgs() != 0) {
826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
827 return;
828 }
Mike Stumpbf916502009-07-24 19:02:52 +0000829
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000830 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000831 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000832 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
833 return;
834 }
835 } else if (!isFunctionOrMethod(d)) {
836 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000837 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000838 return;
839 }
Mike Stumpbf916502009-07-24 19:02:52 +0000840
Sean Huntcf807c42010-08-18 23:23:40 +0000841 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000842}
843
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000844static void HandleConstructorAttr(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 << "constructor" << 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
Chris Lattnerc5197432009-04-14 17:02:11 +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) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000872}
873
874static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
875 // check the attribute arguments.
876 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000877 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
878 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000879 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000880 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000881
882 int priority = 65535; // FIXME: Do not hardcode such constants.
883 if (Attr.getNumArgs() > 0) {
884 Expr *E = static_cast<Expr *>(Attr.getArg(0));
885 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000886 if (E->isTypeDependent() || E->isValueDependent() ||
887 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000888 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000889 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000890 return;
891 }
892 priority = Idx.getZExtValue();
893 }
Mike Stumpbf916502009-07-24 19:02:52 +0000894
Anders Carlsson6782fc62008-08-22 22:10:48 +0000895 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000897 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000898 return;
899 }
900
Sean Huntcf807c42010-08-18 23:23:40 +0000901 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000902}
903
Chris Lattner803d0802008-06-29 00:43:07 +0000904static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000905 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000906 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000907 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000908 return;
909 }
Mike Stumpbf916502009-07-24 19:02:52 +0000910
Sean Huntcf807c42010-08-18 23:23:40 +0000911 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000912}
913
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000914static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
915 // check the attribute arguments.
916 if (Attr.getNumArgs() != 0) {
917 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
918 return;
919 }
Mike Stumpbf916502009-07-24 19:02:52 +0000920
Sean Huntcf807c42010-08-18 23:23:40 +0000921 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000922}
923
Chris Lattner803d0802008-06-29 00:43:07 +0000924static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000925 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000926 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000927 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000928 return;
929 }
Mike Stumpbf916502009-07-24 19:02:52 +0000930
Chris Lattner545dd342008-06-28 23:36:30 +0000931 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000932 Arg = Arg->IgnoreParenCasts();
933 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000934
Chris Lattner6b6b5372008-06-26 18:38:35 +0000935 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000936 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000937 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000938 return;
939 }
Mike Stumpbf916502009-07-24 19:02:52 +0000940
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000941 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +0000942 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +0000943
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000944 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +0000945 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000946 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +0000947 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000948 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +0000949 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000950 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +0000951 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000952 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000953 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000954 return;
955 }
Mike Stumpbf916502009-07-24 19:02:52 +0000956
Sean Huntcf807c42010-08-18 23:23:40 +0000957 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000958}
959
Chris Lattner0db29ec2009-02-14 08:09:34 +0000960static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
961 Sema &S) {
962 if (Attr.getNumArgs() != 0) {
963 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
964 return;
965 }
Mike Stumpbf916502009-07-24 19:02:52 +0000966
Chris Lattner0db29ec2009-02-14 08:09:34 +0000967 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
968 if (OCI == 0) {
969 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
970 return;
971 }
Mike Stumpbf916502009-07-24 19:02:52 +0000972
Sean Huntcf807c42010-08-18 23:23:40 +0000973 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +0000974}
975
976static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000977 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000979 return;
980 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000981 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000982 QualType T = TD->getUnderlyingType();
983 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000984 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000985 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
986 return;
987 }
988 }
Sean Huntcf807c42010-08-18 23:23:40 +0000989 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000990}
991
Mike Stumpbf916502009-07-24 19:02:52 +0000992static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000993HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
994 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000995 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000996 return;
997 }
998
999 if (!isa<FunctionDecl>(D)) {
1000 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1001 return;
1002 }
1003
Sean Huntcf807c42010-08-18 23:23:40 +00001004 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001005}
1006
Steve Naroff9eae5762008-09-18 16:44:58 +00001007static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001008 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001009 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001010 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001011 return;
1012 }
Mike Stumpbf916502009-07-24 19:02:52 +00001013
Steve Naroff9eae5762008-09-18 16:44:58 +00001014 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001015 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001016 return;
1017 }
Mike Stumpbf916502009-07-24 19:02:52 +00001018
Sean Huntcf807c42010-08-18 23:23:40 +00001019 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001020 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001021 type = BlocksAttr::ByRef;
1022 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001023 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001024 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001025 return;
1026 }
Mike Stumpbf916502009-07-24 19:02:52 +00001027
Sean Huntcf807c42010-08-18 23:23:40 +00001028 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001029}
1030
Anders Carlsson77091822008-10-05 18:05:59 +00001031static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1032 // check the attribute arguments.
1033 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1035 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001036 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001037 }
1038
Anders Carlsson77091822008-10-05 18:05:59 +00001039 int sentinel = 0;
1040 if (Attr.getNumArgs() > 0) {
1041 Expr *E = static_cast<Expr *>(Attr.getArg(0));
1042 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001043 if (E->isTypeDependent() || E->isValueDependent() ||
1044 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001045 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001046 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001047 return;
1048 }
1049 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001050
Anders Carlsson77091822008-10-05 18:05:59 +00001051 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001052 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1053 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001054 return;
1055 }
1056 }
1057
1058 int nullPos = 0;
1059 if (Attr.getNumArgs() > 1) {
1060 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1061 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001062 if (E->isTypeDependent() || E->isValueDependent() ||
1063 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001065 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001066 return;
1067 }
1068 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001069
Anders Carlsson77091822008-10-05 18:05:59 +00001070 if (nullPos > 1 || nullPos < 0) {
1071 // FIXME: This error message could be improved, it would be nice
1072 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001073 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1074 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001075 return;
1076 }
1077 }
1078
1079 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001080 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001081 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Chris Lattner897cd902009-03-17 23:03:47 +00001083 if (isa<FunctionNoProtoType>(FT)) {
1084 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1085 return;
1086 }
Mike Stumpbf916502009-07-24 19:02:52 +00001087
Chris Lattner897cd902009-03-17 23:03:47 +00001088 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001089 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001090 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001091 }
Anders Carlsson77091822008-10-05 18:05:59 +00001092 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1093 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001094 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001095 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001096 }
1097 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001098 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1099 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001100 ;
1101 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001102 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001103 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001104 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001105 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001106 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001107 int m = Ty->isFunctionPointerType() ? 0 : 1;
1108 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001109 return;
1110 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001111 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001112 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001113 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001114 return;
1115 }
Anders Carlsson77091822008-10-05 18:05:59 +00001116 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001117 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001118 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001119 return;
1120 }
Sean Huntcf807c42010-08-18 23:23:40 +00001121 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001122}
1123
Chris Lattner026dc962009-02-14 07:37:35 +00001124static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1125 // check the attribute arguments.
1126 if (Attr.getNumArgs() != 0) {
1127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1128 return;
1129 }
1130
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001131 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001132 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001133 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001134 return;
1135 }
Mike Stumpbf916502009-07-24 19:02:52 +00001136
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001137 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1138 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1139 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001140 return;
1141 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001142 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1143 if (MD->getResultType()->isVoidType()) {
1144 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1145 << Attr.getName() << 1;
1146 return;
1147 }
1148
Sean Huntcf807c42010-08-18 23:23:40 +00001149 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001150}
1151
1152static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001153 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001154 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001156 return;
1157 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001158
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001159 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001160 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001161 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1162 dyn_cast<NamedDecl>(D)->getNameAsString();
1163 return;
1164 }
1165
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001166 // TODO: could also be applied to methods?
1167 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1168 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001169 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001170 return;
1171 }
Mike Stumpbf916502009-07-24 19:02:52 +00001172
Sean Huntcf807c42010-08-18 23:23:40 +00001173 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001174}
1175
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001176static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1177 // check the attribute arguments.
1178 if (Attr.getNumArgs() != 0) {
1179 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1180 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001181 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001182
1183 // weak_import only applies to variable & function declarations.
1184 bool isDef = false;
1185 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1186 isDef = (!VD->hasExternalStorage() || VD->getInit());
1187 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001188 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001189 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1190 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001191 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001192 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001193 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001194 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001195 !isa<ObjCInterfaceDecl>(D))
1196 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001197 << Attr.getName() << 2 /*variable and function*/;
1198 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001199 }
1200
1201 // Merge should handle any subsequent violations.
1202 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001203 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001204 diag::warn_attribute_weak_import_invalid_on_definition)
1205 << "weak_import" << 2 /*variable and function*/;
1206 return;
1207 }
1208
Sean Huntcf807c42010-08-18 23:23:40 +00001209 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001210}
1211
Nate Begeman6f3d8382009-06-26 06:32:41 +00001212static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1213 Sema &S) {
1214 // Attribute has 3 arguments.
1215 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001216 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001217 return;
1218 }
1219
1220 unsigned WGSize[3];
1221 for (unsigned i = 0; i < 3; ++i) {
1222 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1223 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001224 if (E->isTypeDependent() || E->isValueDependent() ||
1225 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001226 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1227 << "reqd_work_group_size" << E->getSourceRange();
1228 return;
1229 }
1230 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1231 }
Sean Huntcf807c42010-08-18 23:23:40 +00001232 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1233 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001234 WGSize[2]));
1235}
1236
Chris Lattner026dc962009-02-14 07:37:35 +00001237static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001238 // Attribute has no arguments.
1239 if (Attr.getNumArgs() != 1) {
1240 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1241 return;
1242 }
1243
1244 // Make sure that there is a string literal as the sections's single
1245 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001246 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1247 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001248 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001249 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001250 return;
1251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Chris Lattner797c3c42009-08-10 19:03:04 +00001253 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001254 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001255 if (!Error.empty()) {
1256 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1257 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001258 return;
1259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001261 // This attribute cannot be applied to local variables.
1262 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1263 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1264 return;
1265 }
1266
Sean Huntcf807c42010-08-18 23:23:40 +00001267 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001268}
1269
Chris Lattner6b6b5372008-06-26 18:38:35 +00001270
Chris Lattner803d0802008-06-29 00:43:07 +00001271static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001272 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001273 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001275 return;
1276 }
Mike Stumpbf916502009-07-24 19:02:52 +00001277
Sean Huntcf807c42010-08-18 23:23:40 +00001278 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001279}
1280
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001281static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1282 // check the attribute arguments.
1283 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001285 return;
1286 }
Mike Stumpbf916502009-07-24 19:02:52 +00001287
Sean Huntcf807c42010-08-18 23:23:40 +00001288 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001289}
1290
1291static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1292 // check the attribute arguments.
1293 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001294 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001295 return;
1296 }
Mike Stumpbf916502009-07-24 19:02:52 +00001297
Sean Huntcf807c42010-08-18 23:23:40 +00001298 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001299}
1300
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001301static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001302 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1304 return;
1305 }
Mike Stumpbf916502009-07-24 19:02:52 +00001306
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001307 if (Attr.getNumArgs() != 0) {
1308 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1309 return;
1310 }
Mike Stumpbf916502009-07-24 19:02:52 +00001311
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001312 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001313
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001314 if (!VD || !VD->hasLocalStorage()) {
1315 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1316 return;
1317 }
Mike Stumpbf916502009-07-24 19:02:52 +00001318
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001319 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001320 // FIXME: Lookup probably isn't looking in the right place
1321 // FIXME: The lookup source location should be in the attribute, not the
1322 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001323 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001324 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001325 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001326 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001327 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001328 Attr.getParameterName();
1329 return;
1330 }
Mike Stumpbf916502009-07-24 19:02:52 +00001331
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001332 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1333 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001334 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001335 Attr.getParameterName();
1336 return;
1337 }
1338
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001339 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001340 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001341 Attr.getParameterName();
1342 return;
1343 }
Mike Stumpbf916502009-07-24 19:02:52 +00001344
Anders Carlsson89941c12009-02-07 23:16:50 +00001345 // We're currently more strict than GCC about what function types we accept.
1346 // If this ever proves to be a problem it should be easy to fix.
1347 QualType Ty = S.Context.getPointerType(VD->getType());
1348 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001349 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001350 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001351 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1352 Attr.getParameterName() << ParamTy << Ty;
1353 return;
1354 }
Mike Stumpbf916502009-07-24 19:02:52 +00001355
Sean Huntcf807c42010-08-18 23:23:40 +00001356 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001357}
1358
Mike Stumpbf916502009-07-24 19:02:52 +00001359/// Handle __attribute__((format_arg((idx)))) attribute based on
1360/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1361static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001362 if (Attr.getNumArgs() != 1) {
1363 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1364 return;
1365 }
1366 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1367 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1368 << Attr.getName() << 0 /*function*/;
1369 return;
1370 }
Mike Stumpbf916502009-07-24 19:02:52 +00001371 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1372 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001373 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1374 unsigned FirstIdx = 1;
1375 // checks for the 2nd argument
1376 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1377 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001378 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1379 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001380 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1381 << "format" << 2 << IdxExpr->getSourceRange();
1382 return;
1383 }
Mike Stumpbf916502009-07-24 19:02:52 +00001384
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001385 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1387 << "format" << 2 << IdxExpr->getSourceRange();
1388 return;
1389 }
Mike Stumpbf916502009-07-24 19:02:52 +00001390
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001391 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001392
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001393 // make sure the format string is really a string
1394 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001395
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001396 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1397 if (not_nsstring_type &&
1398 !isCFStringType(Ty, S.Context) &&
1399 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001400 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001401 // FIXME: Should highlight the actual expression that has the wrong type.
1402 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001403 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001404 << IdxExpr->getSourceRange();
1405 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001406 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001407 Ty = getFunctionOrMethodResultType(d);
1408 if (!isNSStringType(Ty, S.Context) &&
1409 !isCFStringType(Ty, S.Context) &&
1410 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001411 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001412 // FIXME: Should highlight the actual expression that has the wrong type.
1413 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001414 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001415 << IdxExpr->getSourceRange();
1416 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001417 }
1418
Sean Huntcf807c42010-08-18 23:23:40 +00001419 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001420}
1421
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001422enum FormatAttrKind {
1423 CFStringFormat,
1424 NSStringFormat,
1425 StrftimeFormat,
1426 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001427 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001428 InvalidFormat
1429};
1430
1431/// getFormatAttrKind - Map from format attribute names to supported format
1432/// types.
1433static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1434 // Check for formats that get handled specially.
1435 if (Format == "NSString")
1436 return NSStringFormat;
1437 if (Format == "CFString")
1438 return CFStringFormat;
1439 if (Format == "strftime")
1440 return StrftimeFormat;
1441
1442 // Otherwise, check for supported formats.
1443 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1444 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1445 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1446 Format == "zcmn_err")
1447 return SupportedFormat;
1448
Duncan Sandsbc525952010-03-23 14:44:19 +00001449 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1450 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001451 return IgnoredFormat;
1452
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001453 return InvalidFormat;
1454}
1455
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001456/// Handle __attribute__((init_priority(priority))) attributes based on
1457/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1458static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1459 Sema &S) {
1460 if (!S.getLangOptions().CPlusPlus) {
1461 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1462 return;
1463 }
1464
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001465 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1466 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1467 Attr.setInvalid();
1468 return;
1469 }
1470 QualType T = dyn_cast<VarDecl>(d)->getType();
1471 if (S.Context.getAsArrayType(T))
1472 T = S.Context.getBaseElementType(T);
1473 if (!T->getAs<RecordType>()) {
1474 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1475 Attr.setInvalid();
1476 return;
1477 }
1478
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001479 if (Attr.getNumArgs() != 1) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1481 Attr.setInvalid();
1482 return;
1483 }
1484 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001485
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001486 llvm::APSInt priority(32);
1487 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1488 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1489 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1490 << "init_priority" << priorityExpr->getSourceRange();
1491 Attr.setInvalid();
1492 return;
1493 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001494 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001495 if (prioritynum < 101 || prioritynum > 65535) {
1496 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1497 << priorityExpr->getSourceRange();
1498 Attr.setInvalid();
1499 return;
1500 }
Sean Huntcf807c42010-08-18 23:23:40 +00001501 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001502}
1503
Mike Stumpbf916502009-07-24 19:02:52 +00001504/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1505/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001506static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001507
Chris Lattner545dd342008-06-28 23:36:30 +00001508 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001509 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001510 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001511 return;
1512 }
1513
Chris Lattner545dd342008-06-28 23:36:30 +00001514 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001515 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001516 return;
1517 }
1518
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001519 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001520 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001521 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001522 return;
1523 }
1524
Daniel Dunbar35682492008-09-26 04:12:28 +00001525 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001526 unsigned FirstIdx = 1;
1527
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001528 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001529
1530 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001531 if (Format.startswith("__") && Format.endswith("__"))
1532 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001533
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001534 // Check for supported formats.
1535 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001536
1537 if (Kind == IgnoredFormat)
1538 return;
1539
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001540 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001541 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001542 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001543 return;
1544 }
1545
1546 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001547 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001548 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001549 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1550 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001552 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001553 return;
1554 }
1555
Anders Carlsson4fb77202009-08-25 14:12:34 +00001556 // FIXME: We should handle the implicit 'this' parameter in a more generic
1557 // way that can be used for other arguments.
1558 bool HasImplicitThisParam = false;
1559 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1560 if (MD->isInstance()) {
1561 HasImplicitThisParam = true;
1562 NumArgs++;
1563 }
1564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Chris Lattner6b6b5372008-06-26 18:38:35 +00001566 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001567 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001568 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001569 return;
1570 }
1571
1572 // FIXME: Do we need to bounds check?
1573 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001574
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001575 if (HasImplicitThisParam) {
1576 if (ArgIdx == 0) {
1577 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1578 << "a string type" << IdxExpr->getSourceRange();
1579 return;
1580 }
1581 ArgIdx--;
1582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Chris Lattner6b6b5372008-06-26 18:38:35 +00001584 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001585 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001586
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001587 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001588 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001589 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1590 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001591 return;
1592 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001593 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001594 // FIXME: do we need to check if the type is NSString*? What are the
1595 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001596 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001597 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001598 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1599 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001600 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001601 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001602 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001603 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001604 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001605 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1606 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001607 return;
1608 }
1609
1610 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001611 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001612 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001613 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1614 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001615 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001616 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001617 return;
1618 }
1619
1620 // check if the function is variadic if the 3rd argument non-zero
1621 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001622 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001623 ++NumArgs; // +1 for ...
1624 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001625 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001626 return;
1627 }
1628 }
1629
Chris Lattner3c73c412008-11-19 08:23:25 +00001630 // strftime requires FirstArg to be 0 because it doesn't read from any
1631 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001632 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001633 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001634 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1635 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001636 return;
1637 }
1638 // if 0 it disables parameter checking (to use with e.g. va_list)
1639 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001640 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001641 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001642 return;
1643 }
1644
Sean Huntcf807c42010-08-18 23:23:40 +00001645 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1646 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001647 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001648}
1649
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001650static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1651 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001652 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001653 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001655 return;
1656 }
1657
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001658 // Try to find the underlying union declaration.
1659 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001660 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001661 if (TD && TD->getUnderlyingType()->isUnionType())
1662 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1663 else
1664 RD = dyn_cast<RecordDecl>(d);
1665
1666 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001667 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001668 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001669 return;
1670 }
1671
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001672 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001673 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001674 diag::warn_transparent_union_attribute_not_definition);
1675 return;
1676 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001677
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001678 RecordDecl::field_iterator Field = RD->field_begin(),
1679 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001680 if (Field == FieldEnd) {
1681 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1682 return;
1683 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001684
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001685 FieldDecl *FirstField = *Field;
1686 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001687 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001688 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001689 diag::warn_transparent_union_attribute_floating)
1690 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001691 return;
1692 }
1693
1694 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1695 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1696 for (; Field != FieldEnd; ++Field) {
1697 QualType FieldType = Field->getType();
1698 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1699 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1700 // Warn if we drop the attribute.
1701 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001702 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001703 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001704 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001705 diag::warn_transparent_union_attribute_field_size_align)
1706 << isSize << Field->getDeclName() << FieldBits;
1707 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001708 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001709 diag::note_transparent_union_first_field_size_align)
1710 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001711 return;
1712 }
1713 }
1714
Sean Huntcf807c42010-08-18 23:23:40 +00001715 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001716}
1717
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001718static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001719 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001720 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001721 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001722 return;
1723 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001724 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1725 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001726
Chris Lattner6b6b5372008-06-26 18:38:35 +00001727 // Make sure that there is a string literal as the annotation's single
1728 // argument.
1729 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001730 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001731 return;
1732 }
Sean Huntcf807c42010-08-18 23:23:40 +00001733 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001734}
1735
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001736static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001737 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001738 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001739 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001740 return;
1741 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001742
1743 //FIXME: The C++0x version of this attribute has more limited applicabilty
1744 // than GNU's, and should error out when it is used to specify a
1745 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001746
Chris Lattner545dd342008-06-28 23:36:30 +00001747 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001748 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001749 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001750 }
Mike Stumpbf916502009-07-24 19:02:52 +00001751
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001752 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1753}
1754
1755void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1756 if (E->isTypeDependent() || E->isValueDependent()) {
1757 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001758 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001759 return;
1760 }
1761
Sean Huntcf807c42010-08-18 23:23:40 +00001762 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001763 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001764 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1765 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1766 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001767 return;
1768 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001769 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001770 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1771 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001772 return;
1773 }
1774
Sean Huntcf807c42010-08-18 23:23:40 +00001775 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1776}
1777
1778void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1779 // FIXME: Cache the number on the Attr object if non-dependent?
1780 // FIXME: Perform checking of type validity
1781 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1782 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001783}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001784
Mike Stumpbf916502009-07-24 19:02:52 +00001785/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1786/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001787///
Mike Stumpbf916502009-07-24 19:02:52 +00001788/// Despite what would be logical, the mode attribute is a decl attribute, not a
1789/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1790/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001791static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001792 // This attribute isn't documented, but glibc uses it. It changes
1793 // the width of an int or unsigned int to the specified size.
1794
1795 // Check that there aren't any arguments
1796 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001798 return;
1799 }
1800
1801 IdentifierInfo *Name = Attr.getParameterName();
1802 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001803 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001804 return;
1805 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001806
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001807 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001808
1809 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001810 if (Str.startswith("__") && Str.endswith("__"))
1811 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001812
1813 unsigned DestWidth = 0;
1814 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001815 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001816 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001817 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001818 switch (Str[0]) {
1819 case 'Q': DestWidth = 8; break;
1820 case 'H': DestWidth = 16; break;
1821 case 'S': DestWidth = 32; break;
1822 case 'D': DestWidth = 64; break;
1823 case 'X': DestWidth = 96; break;
1824 case 'T': DestWidth = 128; break;
1825 }
1826 if (Str[1] == 'F') {
1827 IntegerMode = false;
1828 } else if (Str[1] == 'C') {
1829 IntegerMode = false;
1830 ComplexMode = true;
1831 } else if (Str[1] != 'I') {
1832 DestWidth = 0;
1833 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001834 break;
1835 case 4:
1836 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1837 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001838 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001839 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001840 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001841 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001842 break;
1843 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001844 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001845 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001846 break;
1847 }
1848
1849 QualType OldTy;
1850 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1851 OldTy = TD->getUnderlyingType();
1852 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1853 OldTy = VD->getType();
1854 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001855 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1856 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001857 return;
1858 }
Eli Friedman73397492009-03-03 06:41:03 +00001859
John McCall183700f2009-09-21 23:43:11 +00001860 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001861 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1862 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001863 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001864 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1865 } else if (ComplexMode) {
1866 if (!OldTy->isComplexType())
1867 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1868 } else {
1869 if (!OldTy->isFloatingType())
1870 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1871 }
1872
Mike Stump390b4cc2009-05-16 07:39:55 +00001873 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1874 // and friends, at least with glibc.
1875 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1876 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001877 // FIXME: Make sure floating-point mappings are accurate
1878 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001879 QualType NewTy;
1880 switch (DestWidth) {
1881 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001882 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001883 return;
1884 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001885 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001886 return;
1887 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001888 if (!IntegerMode) {
1889 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1890 return;
1891 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001892 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001893 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001894 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001895 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001896 break;
1897 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001898 if (!IntegerMode) {
1899 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1900 return;
1901 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001902 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001903 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001904 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001905 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001906 break;
1907 case 32:
1908 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001909 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001910 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001911 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001912 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001913 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001914 break;
1915 case 64:
1916 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001917 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001918 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001919 if (S.Context.Target.getLongWidth() == 64)
1920 NewTy = S.Context.LongTy;
1921 else
1922 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001923 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001924 if (S.Context.Target.getLongWidth() == 64)
1925 NewTy = S.Context.UnsignedLongTy;
1926 else
1927 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001928 break;
Eli Friedman73397492009-03-03 06:41:03 +00001929 case 96:
1930 NewTy = S.Context.LongDoubleTy;
1931 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001932 case 128:
1933 if (!IntegerMode) {
1934 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1935 return;
1936 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001937 if (OldTy->isSignedIntegerType())
1938 NewTy = S.Context.Int128Ty;
1939 else
1940 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001941 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001942 }
1943
Eli Friedman73397492009-03-03 06:41:03 +00001944 if (ComplexMode) {
1945 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001946 }
1947
1948 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001949 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1950 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001951 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001952 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001953 cast<ValueDecl>(D)->setType(NewTy);
1954}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001955
Mike Stump1feade82009-08-26 22:31:08 +00001956static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001957 // check the attribute arguments.
1958 if (Attr.getNumArgs() > 0) {
1959 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1960 return;
1961 }
Anders Carlssone896d982009-02-13 08:11:52 +00001962
Anders Carlsson5bab7882009-02-19 19:16:48 +00001963 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001965 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001966 return;
1967 }
Mike Stumpbf916502009-07-24 19:02:52 +00001968
Sean Huntcf807c42010-08-18 23:23:40 +00001969 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00001970}
1971
Mike Stump1feade82009-08-26 22:31:08 +00001972static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001973 // check the attribute arguments.
1974 if (Attr.getNumArgs() != 0) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1976 return;
1977 }
Mike Stumpbf916502009-07-24 19:02:52 +00001978
Chris Lattnerc5197432009-04-14 17:02:11 +00001979 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001981 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001982 return;
1983 }
Mike Stumpbf916502009-07-24 19:02:52 +00001984
Sean Huntcf807c42010-08-18 23:23:40 +00001985 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00001986}
1987
Chris Lattner7255a2d2010-06-22 00:03:40 +00001988static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1989 Sema &S) {
1990 // check the attribute arguments.
1991 if (Attr.getNumArgs() != 0) {
1992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1993 return;
1994 }
1995
1996 if (!isa<FunctionDecl>(d)) {
1997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1998 << Attr.getName() << 0 /*function*/;
1999 return;
2000 }
2001
Sean Huntcf807c42010-08-18 23:23:40 +00002002 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002003}
2004
Chris Lattnercf2a7212009-04-20 19:12:28 +00002005static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002006 // check the attribute arguments.
2007 if (Attr.getNumArgs() != 0) {
2008 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2009 return;
2010 }
Mike Stumpbf916502009-07-24 19:02:52 +00002011
Chris Lattnerc5197432009-04-14 17:02:11 +00002012 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2013 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002014 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002015 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002016 return;
2017 }
Mike Stumpbf916502009-07-24 19:02:52 +00002018
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002019 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002020 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002021 return;
2022 }
Mike Stumpbf916502009-07-24 19:02:52 +00002023
Sean Huntcf807c42010-08-18 23:23:40 +00002024 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002025}
2026
Abramo Bagnarae215f722010-04-30 13:10:51 +00002027static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2028 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2029 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2030 assert(Attr.isInvalid() == false);
2031
2032 switch (Attr.getKind()) {
2033 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002034 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002035 return;
2036 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002037 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002038 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002039 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00002040 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002041 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002042 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00002043 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002044 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002045 case AttributeList::AT_pascal:
2046 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2047 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002048 default:
2049 llvm_unreachable("unexpected attribute kind");
2050 return;
2051 }
2052}
2053
Fariborz Jahanianee760332009-03-27 18:38:55 +00002054static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2055 // check the attribute arguments.
2056 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002057 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002058 return;
2059 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002060
Fariborz Jahanianee760332009-03-27 18:38:55 +00002061 if (!isFunctionOrMethod(d)) {
2062 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002063 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002064 return;
2065 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002066
2067 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2068 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002069 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2070 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002071 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2072 << "regparm" << NumParamsExpr->getSourceRange();
2073 return;
2074 }
2075
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002076 if (S.Context.Target.getRegParmMax() == 0) {
2077 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002078 << NumParamsExpr->getSourceRange();
2079 return;
2080 }
2081
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002082 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002083 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2084 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002085 return;
2086 }
2087
Sean Huntcf807c42010-08-18 23:23:40 +00002088 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2089 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002090}
2091
Sean Huntbbd37c62009-11-21 08:43:09 +00002092static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2093 // check the attribute arguments.
2094 if (Attr.getNumArgs() != 0) {
2095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2096 return;
2097 }
2098
2099 if (!isa<CXXRecordDecl>(d)
2100 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2101 S.Diag(Attr.getLoc(),
2102 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2103 : diag::warn_attribute_wrong_decl_type)
2104 << Attr.getName() << 7 /*virtual method or class*/;
2105 return;
2106 }
Sean Hunt7725e672009-11-25 04:20:27 +00002107
2108 // FIXME: Conform to C++0x redeclaration rules.
2109
2110 if (d->getAttr<FinalAttr>()) {
2111 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2112 return;
2113 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002114
Sean Huntcf807c42010-08-18 23:23:40 +00002115 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002116}
2117
Chris Lattner0744e5f2008-06-29 00:23:49 +00002118//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002119// C++0x member checking attributes
2120//===----------------------------------------------------------------------===//
2121
2122static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2123 if (Attr.getNumArgs() != 0) {
2124 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2125 return;
2126 }
2127
2128 if (!isa<CXXRecordDecl>(d)) {
2129 S.Diag(Attr.getLoc(),
2130 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2131 : diag::warn_attribute_wrong_decl_type)
2132 << Attr.getName() << 9 /*class*/;
2133 return;
2134 }
2135
2136 if (d->getAttr<BaseCheckAttr>()) {
2137 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2138 return;
2139 }
2140
Sean Huntcf807c42010-08-18 23:23:40 +00002141 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002142}
2143
2144static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2145 if (Attr.getNumArgs() != 0) {
2146 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2147 return;
2148 }
2149
2150 if (!isa<RecordDecl>(d->getDeclContext())) {
2151 // FIXME: It's not the type that's the problem
2152 S.Diag(Attr.getLoc(),
2153 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2154 : diag::warn_attribute_wrong_decl_type)
2155 << Attr.getName() << 11 /*member*/;
2156 return;
2157 }
2158
2159 // FIXME: Conform to C++0x redeclaration rules.
2160
2161 if (d->getAttr<HidingAttr>()) {
2162 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2163 return;
2164 }
2165
Sean Huntcf807c42010-08-18 23:23:40 +00002166 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002167}
2168
2169static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2170 if (Attr.getNumArgs() != 0) {
2171 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2172 return;
2173 }
2174
2175 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2176 // FIXME: It's not the type that's the problem
2177 S.Diag(Attr.getLoc(),
2178 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2179 : diag::warn_attribute_wrong_decl_type)
2180 << Attr.getName() << 10 /*virtual method*/;
2181 return;
2182 }
2183
2184 // FIXME: Conform to C++0x redeclaration rules.
2185
2186 if (d->getAttr<OverrideAttr>()) {
2187 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2188 return;
2189 }
2190
Sean Huntcf807c42010-08-18 23:23:40 +00002191 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002192}
2193
2194//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002195// Checker-specific attribute handlers.
2196//===----------------------------------------------------------------------===//
2197
2198static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2199 Sema &S) {
2200
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002201 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002202
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002203 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2204 RetTy = MD->getResultType();
2205 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2206 RetTy = FD->getResultType();
2207 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002208 SourceLocation L = Attr.getLoc();
2209 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2210 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002211 return;
2212 }
Mike Stumpbf916502009-07-24 19:02:52 +00002213
Ted Kremenek6217b802009-07-29 21:53:49 +00002214 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002215 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002216 SourceLocation L = Attr.getLoc();
2217 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2218 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002219 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002220 }
Mike Stumpbf916502009-07-24 19:02:52 +00002221
Ted Kremenekb71368d2009-05-09 02:44:38 +00002222 switch (Attr.getKind()) {
2223 default:
2224 assert(0 && "invalid ownership attribute");
2225 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002226 case AttributeList::AT_cf_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002227 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002228 return;
2229 case AttributeList::AT_ns_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002230 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002231 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002232 case AttributeList::AT_cf_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002233 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002234 return;
2235 case AttributeList::AT_ns_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002236 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002237 return;
2238 };
2239}
2240
Charles Davisf0122fe2010-02-16 18:27:26 +00002241static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2242 return Attr.getKind() == AttributeList::AT_dllimport ||
2243 Attr.getKind() == AttributeList::AT_dllexport;
2244}
2245
Ted Kremenekb71368d2009-05-09 02:44:38 +00002246//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002247// Top Level Sema Entry Points
2248//===----------------------------------------------------------------------===//
2249
Sebastian Redla89d82c2008-12-21 19:24:58 +00002250/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002251/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002252/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2253/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002254static void ProcessDeclAttribute(Scope *scope, Decl *D,
2255 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002256 if (Attr.isInvalid())
2257 return;
2258
Charles Davisf0122fe2010-02-16 18:27:26 +00002259 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2260 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002261 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002262 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002263 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002264 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2265 case AttributeList::AT_IBOutletCollection:
2266 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002267 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002268 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002269 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002270 // Ignore these, these are type attributes, handled by
2271 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002272 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002273 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2274 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002275 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002276 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002277 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002278 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002279 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2280 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002281 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002282 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002283 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2284 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2285 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002286 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002287 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002288 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002289 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2290 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2291 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2292 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2293 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2294 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2295 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2296 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002297 case AttributeList::AT_ownership_returns:
2298 case AttributeList::AT_ownership_takes:
2299 case AttributeList::AT_ownership_holds:
2300 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002301 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002302 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2303 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2304 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002305 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002306
2307 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002308 case AttributeList::AT_ns_returns_not_retained:
2309 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002310 case AttributeList::AT_ns_returns_retained:
2311 case AttributeList::AT_cf_returns_retained:
2312 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2313
Nate Begeman6f3d8382009-06-26 06:32:41 +00002314 case AttributeList::AT_reqd_wg_size:
2315 HandleReqdWorkGroupSize(D, Attr, S); break;
2316
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002317 case AttributeList::AT_init_priority:
2318 HandleInitPriorityAttr(D, Attr, S); break;
2319
Sean Hunt7725e672009-11-25 04:20:27 +00002320 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2321 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002322 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2323 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2324 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002325 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002326 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2327 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002328 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002329 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002330 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002331 case AttributeList::AT_transparent_union:
2332 HandleTransparentUnionAttr(D, Attr, S);
2333 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002334 case AttributeList::AT_objc_exception:
2335 HandleObjCExceptionAttr(D, Attr, S);
2336 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002337 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002338 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2339 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2340 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2341 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2342 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2343 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2344 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2345 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2346 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002347 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002348 // Just ignore
2349 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002350 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2351 HandleNoInstrumentFunctionAttr(D, Attr, S);
2352 break;
John McCall04a67a62010-02-05 21:31:56 +00002353 case AttributeList::AT_stdcall:
2354 case AttributeList::AT_cdecl:
2355 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002356 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002357 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002358 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002359 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002360 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002361 // Ask target about the attribute.
2362 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2363 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002364 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2365 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002366 break;
2367 }
2368}
2369
2370/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2371/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002372void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002373 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2374 ProcessDeclAttribute(S, D, *l, *this);
2375 }
2376
2377 // GCC accepts
2378 // static int a9 __attribute__((weakref));
2379 // but that looks really pointless. We reject it.
2380 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2381 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002382 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002383 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002384 }
2385}
2386
Ryan Flynne25ff832009-07-30 03:15:39 +00002387/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2388/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002389NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002390 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002391 NamedDecl *NewD = 0;
2392 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2393 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2394 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002395 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002396 if (FD->getQualifier()) {
2397 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2398 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2399 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002400 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2401 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2402 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002403 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002404 VD->getStorageClass(),
2405 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002406 if (VD->getQualifier()) {
2407 VarDecl *NewVD = cast<VarDecl>(NewD);
2408 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2409 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002410 }
2411 return NewD;
2412}
2413
2414/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2415/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002416void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002417 if (W.getUsed()) return; // only do this once
2418 W.setUsed(true);
2419 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2420 IdentifierInfo *NDId = ND->getIdentifier();
2421 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002422 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2423 NDId->getName()));
2424 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002425 WeakTopLevelDecl.push_back(NewD);
2426 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2427 // to insert Decl at TU scope, sorry.
2428 DeclContext *SavedContext = CurContext;
2429 CurContext = Context.getTranslationUnitDecl();
2430 PushOnScopeChains(NewD, S);
2431 CurContext = SavedContext;
2432 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002433 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002434 }
2435}
2436
Chris Lattner0744e5f2008-06-29 00:23:49 +00002437/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2438/// it, apply them to D. This is a bit tricky because PD can have attributes
2439/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002440void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002441 // Handle #pragma weak
2442 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2443 if (ND->hasLinkage()) {
2444 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2445 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002446 // Identifier referenced by #pragma weak before it was declared
2447 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002448 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2449 }
2450 }
2451 }
2452
Chris Lattner0744e5f2008-06-29 00:23:49 +00002453 // Apply decl attributes from the DeclSpec if present.
2454 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002455 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002456
Chris Lattner0744e5f2008-06-29 00:23:49 +00002457 // Walk the declarator structure, applying decl attributes that were in a type
2458 // position to the decl itself. This handles cases like:
2459 // int *__attr__(x)** D;
2460 // when X is a decl attribute.
2461 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2462 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002463 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002464
Chris Lattner0744e5f2008-06-29 00:23:49 +00002465 // Finally, apply any attributes on the decl itself.
2466 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002467 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002468}
John McCall54abf7d2009-11-04 02:18:39 +00002469
2470/// PushParsingDeclaration - Enter a new "scope" of deprecation
2471/// warnings.
2472///
2473/// The state token we use is the start index of this scope
2474/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002475Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002476 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002477 return (ParsingDeclStackState) DelayedDiagnostics.size();
2478}
2479
John McCalld226f652010-08-21 09:40:31 +00002480void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002481 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2482 ParsingDeclDepth--;
2483
2484 if (DelayedDiagnostics.empty())
2485 return;
2486
2487 unsigned SavedIndex = (unsigned) S;
2488 assert(SavedIndex <= DelayedDiagnostics.size() &&
2489 "saved index is out of bounds");
2490
John McCall58e6f342010-03-16 05:22:47 +00002491 unsigned E = DelayedDiagnostics.size();
2492
John McCall2f514482010-01-27 03:50:35 +00002493 // We only want to actually emit delayed diagnostics when we
2494 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002495 if (D) {
2496 // We really do want to start with 0 here. We get one push for a
2497 // decl spec and another for each declarator; in a decl group like:
2498 // deprecated_typedef foo, *bar, baz();
2499 // only the declarator pops will be passed decls. This is correct;
2500 // we really do need to consider delayed diagnostics from the decl spec
2501 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002502 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002503 if (DelayedDiagnostics[I].Triggered)
2504 continue;
2505
2506 switch (DelayedDiagnostics[I].Kind) {
2507 case DelayedDiagnostic::Deprecation:
2508 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2509 break;
2510
2511 case DelayedDiagnostic::Access:
2512 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2513 break;
2514 }
2515 }
2516 }
2517
John McCall58e6f342010-03-16 05:22:47 +00002518 // Destroy all the delayed diagnostics we're about to pop off.
2519 for (unsigned I = SavedIndex; I != E; ++I)
2520 DelayedDiagnostics[I].destroy();
2521
John McCall2f514482010-01-27 03:50:35 +00002522 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002523}
2524
2525static bool isDeclDeprecated(Decl *D) {
2526 do {
2527 if (D->hasAttr<DeprecatedAttr>())
2528 return true;
2529 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2530 return false;
2531}
2532
John McCall9c3087b2010-08-26 02:13:20 +00002533void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002534 Decl *Ctx) {
2535 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002536 return;
2537
John McCall2f514482010-01-27 03:50:35 +00002538 DD.Triggered = true;
2539 Diag(DD.Loc, diag::warn_deprecated)
2540 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002541}
2542
2543void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2544 // Delay if we're currently parsing a declaration.
2545 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002546 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002547 return;
2548 }
2549
2550 // Otherwise, don't warn if our current context is deprecated.
2551 if (isDeclDeprecated(cast<Decl>(CurContext)))
2552 return;
2553
2554 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2555}