blob: f30a6ff78f9ebc0a08d94660521605e6f988dd73 [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 Kremenekee1c08c2010-10-21 18:49:36 +0000389 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000390 if (NonNullArgs.empty()) {
391 // Warn the trivial case only if attribute is not coming from a
392 // macro instantiation.
393 if (Attr.getLoc().isFileID())
394 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000395 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000396 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000397 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000398
399 unsigned* start = &NonNullArgs[0];
400 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000401 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000402 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
403 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000404}
405
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000406static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
407 // This attribute must be applied to a function declaration.
408 // The first argument to the attribute must be a string,
409 // the name of the resource, for example "malloc".
410 // The following arguments must be argument indexes, the arguments must be
411 // of integer type for Returns, otherwise of pointer type.
412 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000413 // after being held. free() should be __attribute((ownership_takes)), whereas
414 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000415
416 if (!AL.getParameterName()) {
417 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
418 << AL.getName()->getName() << 1;
419 return;
420 }
421 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000422 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000423 switch (AL.getKind()) {
424 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000425 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000426 if (AL.getNumArgs() < 1) {
427 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
428 return;
429 }
Jordy Rose2a479922010-08-12 08:54:03 +0000430 break;
431 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000432 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000433 if (AL.getNumArgs() < 1) {
434 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
435 return;
436 }
Jordy Rose2a479922010-08-12 08:54:03 +0000437 break;
438 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000439 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000440 if (AL.getNumArgs() > 1) {
441 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
442 << AL.getNumArgs() + 1;
443 return;
444 }
Jordy Rose2a479922010-08-12 08:54:03 +0000445 break;
446 default:
447 // This should never happen given how we are called.
448 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000449 }
450
451 if (!isFunction(d) || !hasFunctionProto(d)) {
452 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
453 << 0 /*function*/;
454 return;
455 }
456
457 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
458
459 llvm::StringRef Module = AL.getParameterName()->getName();
460
461 // Normalize the argument, __foo__ becomes foo.
462 if (Module.startswith("__") && Module.endswith("__"))
463 Module = Module.substr(2, Module.size() - 4);
464
465 llvm::SmallVector<unsigned, 10> OwnershipArgs;
466
Jordy Rose2a479922010-08-12 08:54:03 +0000467 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
468 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000469
470 Expr *IdxExpr = static_cast<Expr *>(*I);
471 llvm::APSInt ArgNum(32);
472 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
473 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
474 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
475 << AL.getName()->getName() << IdxExpr->getSourceRange();
476 continue;
477 }
478
479 unsigned x = (unsigned) ArgNum.getZExtValue();
480
481 if (x > NumArgs || x < 1) {
482 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
483 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
484 continue;
485 }
486 --x;
487 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000488 case OwnershipAttr::Takes:
489 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000490 // Is the function argument a pointer type?
491 QualType T = getFunctionOrMethodArgType(d, x);
492 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
493 // FIXME: Should also highlight argument in decl.
494 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000495 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000496 << "pointer"
497 << IdxExpr->getSourceRange();
498 continue;
499 }
500 break;
501 }
Sean Huntcf807c42010-08-18 23:23:40 +0000502 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000503 if (AL.getNumArgs() > 1) {
504 // Is the function argument an integer type?
505 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
506 llvm::APSInt ArgNum(32);
507 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
508 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
509 S.Diag(AL.getLoc(), diag::err_ownership_type)
510 << "ownership_returns" << "integer"
511 << IdxExpr->getSourceRange();
512 return;
513 }
514 }
515 break;
516 }
Jordy Rose2a479922010-08-12 08:54:03 +0000517 default:
518 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000519 } // switch
520
521 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000522 for (specific_attr_iterator<OwnershipAttr>
523 i = d->specific_attr_begin<OwnershipAttr>(),
524 e = d->specific_attr_end<OwnershipAttr>();
525 i != e; ++i) {
526 if ((*i)->getOwnKind() != K) {
527 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
528 I!=E; ++I) {
529 if (x == *I) {
530 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
531 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000532 }
533 }
534 }
535 }
536 OwnershipArgs.push_back(x);
537 }
538
539 unsigned* start = OwnershipArgs.data();
540 unsigned size = OwnershipArgs.size();
541 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000542
543 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
544 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
545 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000546 }
Sean Huntcf807c42010-08-18 23:23:40 +0000547
548 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
549 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000550}
551
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000552static bool isStaticVarOrStaticFunciton(Decl *D) {
553 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000554 return VD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000555 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000556 return FD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000557 return false;
558}
559
560static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
561 // Check the attribute arguments.
562 if (Attr.getNumArgs() > 1) {
563 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
564 return;
565 }
566
567 // gcc rejects
568 // class c {
569 // static int a __attribute__((weakref ("v2")));
570 // static int b() __attribute__((weakref ("f3")));
571 // };
572 // and ignores the attributes of
573 // void f(void) {
574 // static int a __attribute__((weakref ("v2")));
575 // }
576 // we reject them
Sebastian Redl7a126a42010-08-31 00:36:30 +0000577 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
578 if (!Ctx->isFileContext()) {
579 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
580 dyn_cast<NamedDecl>(d)->getNameAsString();
581 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000582 }
583
584 // The GCC manual says
585 //
586 // At present, a declaration to which `weakref' is attached can only
587 // be `static'.
588 //
589 // It also says
590 //
591 // Without a TARGET,
592 // given as an argument to `weakref' or to `alias', `weakref' is
593 // equivalent to `weak'.
594 //
595 // gcc 4.4.1 will accept
596 // int a7 __attribute__((weakref));
597 // as
598 // int a7 __attribute__((weak));
599 // This looks like a bug in gcc. We reject that for now. We should revisit
600 // it if this behaviour is actually used.
601
602 if (!isStaticVarOrStaticFunciton(d)) {
603 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
604 dyn_cast<NamedDecl>(d)->getNameAsString();
605 return;
606 }
607
608 // GCC rejects
609 // static ((alias ("y"), weakref)).
610 // Should we? How to check that weakref is before or after alias?
611
612 if (Attr.getNumArgs() == 1) {
613 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
614 Arg = Arg->IgnoreParenCasts();
615 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
616
617 if (Str == 0 || Str->isWide()) {
618 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
619 << "weakref" << 1;
620 return;
621 }
622 // GCC will accept anything as the argument of weakref. Should we
623 // check for an existing decl?
Sean Huntcf807c42010-08-18 23:23:40 +0000624 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000625 }
626
Sean Huntcf807c42010-08-18 23:23:40 +0000627 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000628}
629
Chris Lattner803d0802008-06-29 00:43:07 +0000630static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000631 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000632 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000634 return;
635 }
Mike Stumpbf916502009-07-24 19:02:52 +0000636
Chris Lattner545dd342008-06-28 23:36:30 +0000637 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000638 Arg = Arg->IgnoreParenCasts();
639 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000640
Chris Lattner6b6b5372008-06-26 18:38:35 +0000641 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000642 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000643 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000644 return;
645 }
Mike Stumpbf916502009-07-24 19:02:52 +0000646
Chris Lattner6b6b5372008-06-26 18:38:35 +0000647 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000648
Sean Huntcf807c42010-08-18 23:23:40 +0000649 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000650}
651
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000652static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000653 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000654 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000655 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000657 return;
658 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000659
Chris Lattnerc5197432009-04-14 17:02:11 +0000660 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000661 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000662 << Attr.getName() << 0 /*function*/;
663 return;
664 }
665
666 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
667}
668
669static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
670 Sema &S) {
671 // Check the attribute arguments.
672 if (Attr.getNumArgs() != 0) {
673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
674 return;
675 }
676
677 if (!isa<FunctionDecl>(d)) {
678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
679 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000680 return;
681 }
Mike Stumpbf916502009-07-24 19:02:52 +0000682
Sean Huntcf807c42010-08-18 23:23:40 +0000683 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000684}
685
Ryan Flynn76168e22009-08-09 20:07:29 +0000686static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000687 // Check the attribute arguments.
Ryan Flynn76168e22009-08-09 20:07:29 +0000688 if (Attr.getNumArgs() != 0) {
689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
690 return;
691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000693 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000694 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000695 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000696 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000697 return;
698 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000699 }
700
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000701 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000702}
703
Ted Kremenekb7252322009-04-10 00:01:14 +0000704static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000705 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
706 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000707 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000708}
709
710static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
711 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000712
713 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
714 // because 'analyzer_noreturn' does not impact the type.
715
716 if (Attr.getNumArgs() != 0) {
717 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
718 return;
719 }
720
721 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
722 ValueDecl *VD = dyn_cast<ValueDecl>(d);
723 if (VD == 0 || (!VD->getType()->isBlockPointerType()
724 && !VD->getType()->isFunctionPointerType())) {
725 S.Diag(Attr.getLoc(),
726 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
727 : diag::warn_attribute_wrong_decl_type)
728 << Attr.getName() << 0 /*function*/;
729 return;
730 }
731 }
732
733 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000734}
735
John Thompson35cc9622010-08-09 21:53:52 +0000736// PS3 PPU-specific.
737static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
738 Sema &S) {
739/*
740 Returning a Vector Class in Registers
741
742 According to the PPU ABI specifications, a class with a single member of vector type is returned in
743 memory when used as the return value of a function. This results in inefficient code when implementing
744 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
745 definition. This attribute is also applicable to struct types.
746
747 Example:
748
749 struct Vector
750 {
751 __vector float xyzw;
752 } __attribute__((vecreturn));
753
754 Vector Add(Vector lhs, Vector rhs)
755 {
756 Vector result;
757 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
758 return result; // This will be returned in a register
759 }
760*/
John Thompson01add592010-09-18 01:12:07 +0000761 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000762 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
763 << Attr.getName() << 9 /*class*/;
764 return;
765 }
766
767 if (d->getAttr<VecReturnAttr>()) {
768 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
769 return;
770 }
771
John Thompson01add592010-09-18 01:12:07 +0000772 RecordDecl *record = cast<RecordDecl>(d);
773 int count = 0;
774
775 if (!isa<CXXRecordDecl>(record)) {
776 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
777 return;
778 }
779
780 if (!cast<CXXRecordDecl>(record)->isPOD()) {
781 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
782 return;
783 }
784
785 for (RecordDecl::field_iterator iter = record->field_begin(); iter != record->field_end(); iter++) {
786 if ((count == 1) || !iter->getType()->isVectorType()) {
787 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
788 return;
789 }
790 count++;
791 }
792
Sean Huntcf807c42010-08-18 23:23:40 +0000793 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000794}
795
Sean Huntbbd37c62009-11-21 08:43:09 +0000796static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
797 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000799 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000800 return;
801 }
802 // FIXME: Actually store the attribute on the declaration
803}
804
Ted Kremenek73798892008-07-25 04:39:19 +0000805static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
806 // check the attribute arguments.
807 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000809 return;
810 }
Mike Stumpbf916502009-07-24 19:02:52 +0000811
John McCallaec58602010-03-31 02:47:45 +0000812 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
813 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000814 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000815 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000816 return;
817 }
Mike Stumpbf916502009-07-24 19:02:52 +0000818
Sean Huntcf807c42010-08-18 23:23:40 +0000819 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000820}
821
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000822static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
823 // check the attribute arguments.
824 if (Attr.getNumArgs() != 0) {
825 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
826 return;
827 }
Mike Stumpbf916502009-07-24 19:02:52 +0000828
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000829 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000830 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000831 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
832 return;
833 }
834 } else if (!isFunctionOrMethod(d)) {
835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000836 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000837 return;
838 }
Mike Stumpbf916502009-07-24 19:02:52 +0000839
Sean Huntcf807c42010-08-18 23:23:40 +0000840 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000841}
842
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000843static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
844 // check the attribute arguments.
845 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
847 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000848 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000849 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000850
851 int priority = 65535; // FIXME: Do not hardcode such constants.
852 if (Attr.getNumArgs() > 0) {
853 Expr *E = static_cast<Expr *>(Attr.getArg(0));
854 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000855 if (E->isTypeDependent() || E->isValueDependent() ||
856 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000857 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000858 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000859 return;
860 }
861 priority = Idx.getZExtValue();
862 }
Mike Stumpbf916502009-07-24 19:02:52 +0000863
Chris Lattnerc5197432009-04-14 17:02:11 +0000864 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000866 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000867 return;
868 }
869
Sean Huntcf807c42010-08-18 23:23:40 +0000870 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000871}
872
873static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
874 // check the attribute arguments.
875 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000876 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
877 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000878 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000879 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000880
881 int priority = 65535; // FIXME: Do not hardcode such constants.
882 if (Attr.getNumArgs() > 0) {
883 Expr *E = static_cast<Expr *>(Attr.getArg(0));
884 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000885 if (E->isTypeDependent() || E->isValueDependent() ||
886 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000887 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000888 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000889 return;
890 }
891 priority = Idx.getZExtValue();
892 }
Mike Stumpbf916502009-07-24 19:02:52 +0000893
Anders Carlsson6782fc62008-08-22 22:10:48 +0000894 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000895 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000896 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000897 return;
898 }
899
Sean Huntcf807c42010-08-18 23:23:40 +0000900 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000901}
902
Chris Lattner803d0802008-06-29 00:43:07 +0000903static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000904 // check the attribute arguments.
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000905 int noArgs = Attr.getNumArgs();
906 if (noArgs > 1) {
907 S.Diag(Attr.getLoc(),
908 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000909 return;
910 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000911 // Handle the case where deprecated attribute has a text message.
912 StringLiteral *SE;
913 if (noArgs == 1) {
914 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
915 SE = dyn_cast<StringLiteral>(ArgExpr);
916 if (!SE) {
917 S.Diag(ArgExpr->getLocStart(),
918 diag::err_attribute_not_string) << "deprecated";
919 return;
920 }
921 }
922 else
923 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpbf916502009-07-24 19:02:52 +0000924
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000925 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
926 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000927}
928
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000929static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
930 // check the attribute arguments.
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000931 int noArgs = Attr.getNumArgs();
932 if (noArgs > 1) {
933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000934 return;
935 }
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000936 // Handle the case where unavailable attribute has a text message.
937 StringLiteral *SE;
938 if (noArgs == 1) {
939 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
940 SE = dyn_cast<StringLiteral>(ArgExpr);
941 if (!SE) {
942 S.Diag(ArgExpr->getLocStart(),
943 diag::err_attribute_not_string) << "unavailable";
944 return;
945 }
946 }
947 else
948 SE = StringLiteral::CreateEmpty(S.Context, 1);
949 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
950 SE->getString()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000951}
952
Chris Lattner803d0802008-06-29 00:43:07 +0000953static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000954 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000955 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000956 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000957 return;
958 }
Mike Stumpbf916502009-07-24 19:02:52 +0000959
Chris Lattner545dd342008-06-28 23:36:30 +0000960 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000961 Arg = Arg->IgnoreParenCasts();
962 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000963
Chris Lattner6b6b5372008-06-26 18:38:35 +0000964 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000966 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000967 return;
968 }
Mike Stumpbf916502009-07-24 19:02:52 +0000969
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000970 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +0000971 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +0000972
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000973 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +0000974 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000975 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +0000976 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000977 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +0000978 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000979 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +0000980 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000981 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000982 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000983 return;
984 }
Mike Stumpbf916502009-07-24 19:02:52 +0000985
Sean Huntcf807c42010-08-18 23:23:40 +0000986 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000987}
988
Chris Lattner0db29ec2009-02-14 08:09:34 +0000989static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
990 Sema &S) {
991 if (Attr.getNumArgs() != 0) {
992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
993 return;
994 }
Mike Stumpbf916502009-07-24 19:02:52 +0000995
Chris Lattner0db29ec2009-02-14 08:09:34 +0000996 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
997 if (OCI == 0) {
998 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
999 return;
1000 }
Mike Stumpbf916502009-07-24 19:02:52 +00001001
Sean Huntcf807c42010-08-18 23:23:40 +00001002 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001003}
1004
1005static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001006 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001007 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001008 return;
1009 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001010 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001011 QualType T = TD->getUnderlyingType();
1012 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001013 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001014 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1015 return;
1016 }
1017 }
Sean Huntcf807c42010-08-18 23:23:40 +00001018 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001019}
1020
Mike Stumpbf916502009-07-24 19:02:52 +00001021static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001022HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1023 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001024 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001025 return;
1026 }
1027
1028 if (!isa<FunctionDecl>(D)) {
1029 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1030 return;
1031 }
1032
Sean Huntcf807c42010-08-18 23:23:40 +00001033 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001034}
1035
Steve Naroff9eae5762008-09-18 16:44:58 +00001036static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001037 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001039 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001040 return;
1041 }
Mike Stumpbf916502009-07-24 19:02:52 +00001042
Steve Naroff9eae5762008-09-18 16:44:58 +00001043 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001044 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001045 return;
1046 }
Mike Stumpbf916502009-07-24 19:02:52 +00001047
Sean Huntcf807c42010-08-18 23:23:40 +00001048 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001049 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001050 type = BlocksAttr::ByRef;
1051 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001052 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001053 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001054 return;
1055 }
Mike Stumpbf916502009-07-24 19:02:52 +00001056
Sean Huntcf807c42010-08-18 23:23:40 +00001057 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001058}
1059
Anders Carlsson77091822008-10-05 18:05:59 +00001060static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1061 // check the attribute arguments.
1062 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001063 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1064 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001065 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001066 }
1067
Anders Carlsson77091822008-10-05 18:05:59 +00001068 int sentinel = 0;
1069 if (Attr.getNumArgs() > 0) {
1070 Expr *E = static_cast<Expr *>(Attr.getArg(0));
1071 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001072 if (E->isTypeDependent() || E->isValueDependent() ||
1073 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001074 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001075 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001076 return;
1077 }
1078 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001079
Anders Carlsson77091822008-10-05 18:05:59 +00001080 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001081 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1082 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001083 return;
1084 }
1085 }
1086
1087 int nullPos = 0;
1088 if (Attr.getNumArgs() > 1) {
1089 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1090 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001091 if (E->isTypeDependent() || E->isValueDependent() ||
1092 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001094 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001095 return;
1096 }
1097 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001098
Anders Carlsson77091822008-10-05 18:05:59 +00001099 if (nullPos > 1 || nullPos < 0) {
1100 // FIXME: This error message could be improved, it would be nice
1101 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001102 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1103 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001104 return;
1105 }
1106 }
1107
1108 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001109 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001110 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001111
Chris Lattner897cd902009-03-17 23:03:47 +00001112 if (isa<FunctionNoProtoType>(FT)) {
1113 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1114 return;
1115 }
Mike Stumpbf916502009-07-24 19:02:52 +00001116
Chris Lattner897cd902009-03-17 23:03:47 +00001117 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001118 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001119 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001120 }
Anders Carlsson77091822008-10-05 18:05:59 +00001121 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1122 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001123 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001124 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001125 }
1126 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001127 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1128 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001129 ;
1130 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001131 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001132 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001133 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001134 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001135 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001136 int m = Ty->isFunctionPointerType() ? 0 : 1;
1137 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001138 return;
1139 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001140 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001141 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001142 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001143 return;
1144 }
Anders Carlsson77091822008-10-05 18:05:59 +00001145 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001146 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001147 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001148 return;
1149 }
Sean Huntcf807c42010-08-18 23:23:40 +00001150 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001151}
1152
Chris Lattner026dc962009-02-14 07:37:35 +00001153static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1154 // check the attribute arguments.
1155 if (Attr.getNumArgs() != 0) {
1156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1157 return;
1158 }
1159
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001160 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001162 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001163 return;
1164 }
Mike Stumpbf916502009-07-24 19:02:52 +00001165
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001166 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1167 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1168 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001169 return;
1170 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001171 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1172 if (MD->getResultType()->isVoidType()) {
1173 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1174 << Attr.getName() << 1;
1175 return;
1176 }
1177
Sean Huntcf807c42010-08-18 23:23:40 +00001178 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001179}
1180
1181static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001182 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001183 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001184 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001185 return;
1186 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001187
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001188 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001189 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001190 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1191 dyn_cast<NamedDecl>(D)->getNameAsString();
1192 return;
1193 }
1194
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001195 // TODO: could also be applied to methods?
1196 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1197 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001198 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001199 return;
1200 }
Mike Stumpbf916502009-07-24 19:02:52 +00001201
Sean Huntcf807c42010-08-18 23:23:40 +00001202 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001203}
1204
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001205static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1206 // check the attribute arguments.
1207 if (Attr.getNumArgs() != 0) {
1208 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1209 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001210 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001211
1212 // weak_import only applies to variable & function declarations.
1213 bool isDef = false;
1214 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1215 isDef = (!VD->hasExternalStorage() || VD->getInit());
1216 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001217 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001218 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1219 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001220 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001221 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001222 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001223 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001224 !isa<ObjCInterfaceDecl>(D))
1225 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001226 << Attr.getName() << 2 /*variable and function*/;
1227 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001228 }
1229
1230 // Merge should handle any subsequent violations.
1231 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001232 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001233 diag::warn_attribute_weak_import_invalid_on_definition)
1234 << "weak_import" << 2 /*variable and function*/;
1235 return;
1236 }
1237
Sean Huntcf807c42010-08-18 23:23:40 +00001238 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001239}
1240
Nate Begeman6f3d8382009-06-26 06:32:41 +00001241static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1242 Sema &S) {
1243 // Attribute has 3 arguments.
1244 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001246 return;
1247 }
1248
1249 unsigned WGSize[3];
1250 for (unsigned i = 0; i < 3; ++i) {
1251 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1252 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001253 if (E->isTypeDependent() || E->isValueDependent() ||
1254 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001255 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1256 << "reqd_work_group_size" << E->getSourceRange();
1257 return;
1258 }
1259 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1260 }
Sean Huntcf807c42010-08-18 23:23:40 +00001261 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1262 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001263 WGSize[2]));
1264}
1265
Chris Lattner026dc962009-02-14 07:37:35 +00001266static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001267 // Attribute has no arguments.
1268 if (Attr.getNumArgs() != 1) {
1269 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1270 return;
1271 }
1272
1273 // Make sure that there is a string literal as the sections's single
1274 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001275 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1276 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001277 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001278 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001279 return;
1280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Chris Lattner797c3c42009-08-10 19:03:04 +00001282 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001283 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001284 if (!Error.empty()) {
1285 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1286 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001287 return;
1288 }
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001290 // This attribute cannot be applied to local variables.
1291 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1292 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1293 return;
1294 }
1295
Sean Huntcf807c42010-08-18 23:23:40 +00001296 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001297}
1298
Chris Lattner6b6b5372008-06-26 18:38:35 +00001299
Chris Lattner803d0802008-06-29 00:43:07 +00001300static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001301 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001302 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001304 return;
1305 }
Mike Stumpbf916502009-07-24 19:02:52 +00001306
Sean Huntcf807c42010-08-18 23:23:40 +00001307 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001308}
1309
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001310static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1311 // check the attribute arguments.
1312 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001314 return;
1315 }
Mike Stumpbf916502009-07-24 19:02:52 +00001316
Sean Huntcf807c42010-08-18 23:23:40 +00001317 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001318}
1319
1320static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1321 // check the attribute arguments.
1322 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001323 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001324 return;
1325 }
Mike Stumpbf916502009-07-24 19:02:52 +00001326
Sean Huntcf807c42010-08-18 23:23:40 +00001327 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001328}
1329
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001330static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001331 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1333 return;
1334 }
Mike Stumpbf916502009-07-24 19:02:52 +00001335
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001336 if (Attr.getNumArgs() != 0) {
1337 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1338 return;
1339 }
Mike Stumpbf916502009-07-24 19:02:52 +00001340
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001341 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001342
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001343 if (!VD || !VD->hasLocalStorage()) {
1344 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1345 return;
1346 }
Mike Stumpbf916502009-07-24 19:02:52 +00001347
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001348 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001349 // FIXME: Lookup probably isn't looking in the right place
1350 // FIXME: The lookup source location should be in the attribute, not the
1351 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001352 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001353 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001354 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001355 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001356 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001357 Attr.getParameterName();
1358 return;
1359 }
Mike Stumpbf916502009-07-24 19:02:52 +00001360
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001361 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1362 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001363 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001364 Attr.getParameterName();
1365 return;
1366 }
1367
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001368 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001369 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001370 Attr.getParameterName();
1371 return;
1372 }
Mike Stumpbf916502009-07-24 19:02:52 +00001373
Anders Carlsson89941c12009-02-07 23:16:50 +00001374 // We're currently more strict than GCC about what function types we accept.
1375 // If this ever proves to be a problem it should be easy to fix.
1376 QualType Ty = S.Context.getPointerType(VD->getType());
1377 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001378 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001379 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001380 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1381 Attr.getParameterName() << ParamTy << Ty;
1382 return;
1383 }
Mike Stumpbf916502009-07-24 19:02:52 +00001384
Sean Huntcf807c42010-08-18 23:23:40 +00001385 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001386}
1387
Mike Stumpbf916502009-07-24 19:02:52 +00001388/// Handle __attribute__((format_arg((idx)))) attribute based on
1389/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1390static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001391 if (Attr.getNumArgs() != 1) {
1392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1393 return;
1394 }
1395 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1396 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1397 << Attr.getName() << 0 /*function*/;
1398 return;
1399 }
Mike Stumpbf916502009-07-24 19:02:52 +00001400 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1401 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001402 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1403 unsigned FirstIdx = 1;
1404 // checks for the 2nd argument
1405 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1406 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001407 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1408 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001409 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1410 << "format" << 2 << IdxExpr->getSourceRange();
1411 return;
1412 }
Mike Stumpbf916502009-07-24 19:02:52 +00001413
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001414 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1415 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1416 << "format" << 2 << IdxExpr->getSourceRange();
1417 return;
1418 }
Mike Stumpbf916502009-07-24 19:02:52 +00001419
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001420 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001421
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001422 // make sure the format string is really a string
1423 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001424
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001425 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1426 if (not_nsstring_type &&
1427 !isCFStringType(Ty, S.Context) &&
1428 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001429 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001430 // FIXME: Should highlight the actual expression that has the wrong type.
1431 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001432 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001433 << IdxExpr->getSourceRange();
1434 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001435 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001436 Ty = getFunctionOrMethodResultType(d);
1437 if (!isNSStringType(Ty, S.Context) &&
1438 !isCFStringType(Ty, S.Context) &&
1439 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001440 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001441 // FIXME: Should highlight the actual expression that has the wrong type.
1442 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001443 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001444 << IdxExpr->getSourceRange();
1445 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001446 }
1447
Sean Huntcf807c42010-08-18 23:23:40 +00001448 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001449}
1450
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001451enum FormatAttrKind {
1452 CFStringFormat,
1453 NSStringFormat,
1454 StrftimeFormat,
1455 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001456 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001457 InvalidFormat
1458};
1459
1460/// getFormatAttrKind - Map from format attribute names to supported format
1461/// types.
1462static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1463 // Check for formats that get handled specially.
1464 if (Format == "NSString")
1465 return NSStringFormat;
1466 if (Format == "CFString")
1467 return CFStringFormat;
1468 if (Format == "strftime")
1469 return StrftimeFormat;
1470
1471 // Otherwise, check for supported formats.
1472 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1473 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1474 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1475 Format == "zcmn_err")
1476 return SupportedFormat;
1477
Duncan Sandsbc525952010-03-23 14:44:19 +00001478 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1479 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001480 return IgnoredFormat;
1481
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001482 return InvalidFormat;
1483}
1484
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001485/// Handle __attribute__((init_priority(priority))) attributes based on
1486/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1487static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1488 Sema &S) {
1489 if (!S.getLangOptions().CPlusPlus) {
1490 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1491 return;
1492 }
1493
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001494 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1495 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1496 Attr.setInvalid();
1497 return;
1498 }
1499 QualType T = dyn_cast<VarDecl>(d)->getType();
1500 if (S.Context.getAsArrayType(T))
1501 T = S.Context.getBaseElementType(T);
1502 if (!T->getAs<RecordType>()) {
1503 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1504 Attr.setInvalid();
1505 return;
1506 }
1507
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001508 if (Attr.getNumArgs() != 1) {
1509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1510 Attr.setInvalid();
1511 return;
1512 }
1513 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001514
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001515 llvm::APSInt priority(32);
1516 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1517 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1518 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1519 << "init_priority" << priorityExpr->getSourceRange();
1520 Attr.setInvalid();
1521 return;
1522 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001523 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001524 if (prioritynum < 101 || prioritynum > 65535) {
1525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1526 << priorityExpr->getSourceRange();
1527 Attr.setInvalid();
1528 return;
1529 }
Sean Huntcf807c42010-08-18 23:23:40 +00001530 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001531}
1532
Mike Stumpbf916502009-07-24 19:02:52 +00001533/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1534/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001535static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001536
Chris Lattner545dd342008-06-28 23:36:30 +00001537 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001539 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001540 return;
1541 }
1542
Chris Lattner545dd342008-06-28 23:36:30 +00001543 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001544 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001545 return;
1546 }
1547
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001548 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001550 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001551 return;
1552 }
1553
Daniel Dunbar35682492008-09-26 04:12:28 +00001554 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001555 unsigned FirstIdx = 1;
1556
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001557 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001558
1559 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001560 if (Format.startswith("__") && Format.endswith("__"))
1561 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001562
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001563 // Check for supported formats.
1564 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001565
1566 if (Kind == IgnoredFormat)
1567 return;
1568
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001569 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001570 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001571 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001572 return;
1573 }
1574
1575 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001576 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001577 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001578 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1579 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001580 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001581 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001582 return;
1583 }
1584
Anders Carlsson4fb77202009-08-25 14:12:34 +00001585 // FIXME: We should handle the implicit 'this' parameter in a more generic
1586 // way that can be used for other arguments.
1587 bool HasImplicitThisParam = false;
1588 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1589 if (MD->isInstance()) {
1590 HasImplicitThisParam = true;
1591 NumArgs++;
1592 }
1593 }
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Chris Lattner6b6b5372008-06-26 18:38:35 +00001595 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001597 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001598 return;
1599 }
1600
1601 // FIXME: Do we need to bounds check?
1602 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001603
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001604 if (HasImplicitThisParam) {
1605 if (ArgIdx == 0) {
1606 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1607 << "a string type" << IdxExpr->getSourceRange();
1608 return;
1609 }
1610 ArgIdx--;
1611 }
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Chris Lattner6b6b5372008-06-26 18:38:35 +00001613 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001614 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001615
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001616 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001617 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001618 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1619 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001620 return;
1621 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001622 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001623 // FIXME: do we need to check if the type is NSString*? What are the
1624 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001625 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001626 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001627 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1628 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001629 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001630 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001631 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001632 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001633 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001634 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1635 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001636 return;
1637 }
1638
1639 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001640 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001641 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001642 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1643 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001644 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001645 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001646 return;
1647 }
1648
1649 // check if the function is variadic if the 3rd argument non-zero
1650 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001651 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001652 ++NumArgs; // +1 for ...
1653 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001654 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001655 return;
1656 }
1657 }
1658
Chris Lattner3c73c412008-11-19 08:23:25 +00001659 // strftime requires FirstArg to be 0 because it doesn't read from any
1660 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001661 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001662 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001663 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1664 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001665 return;
1666 }
1667 // if 0 it disables parameter checking (to use with e.g. va_list)
1668 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001669 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001670 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001671 return;
1672 }
1673
Sean Huntcf807c42010-08-18 23:23:40 +00001674 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1675 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001676 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001677}
1678
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001679static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1680 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001681 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001682 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001683 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001684 return;
1685 }
1686
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001687 // Try to find the underlying union declaration.
1688 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001689 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001690 if (TD && TD->getUnderlyingType()->isUnionType())
1691 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1692 else
1693 RD = dyn_cast<RecordDecl>(d);
1694
1695 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001696 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001697 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001698 return;
1699 }
1700
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001701 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001702 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001703 diag::warn_transparent_union_attribute_not_definition);
1704 return;
1705 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001706
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001707 RecordDecl::field_iterator Field = RD->field_begin(),
1708 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001709 if (Field == FieldEnd) {
1710 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1711 return;
1712 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001713
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001714 FieldDecl *FirstField = *Field;
1715 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001716 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001717 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001718 diag::warn_transparent_union_attribute_floating)
1719 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001720 return;
1721 }
1722
1723 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1724 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1725 for (; Field != FieldEnd; ++Field) {
1726 QualType FieldType = Field->getType();
1727 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1728 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1729 // Warn if we drop the attribute.
1730 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001731 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001732 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001733 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001734 diag::warn_transparent_union_attribute_field_size_align)
1735 << isSize << Field->getDeclName() << FieldBits;
1736 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001737 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001738 diag::note_transparent_union_first_field_size_align)
1739 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001740 return;
1741 }
1742 }
1743
Sean Huntcf807c42010-08-18 23:23:40 +00001744 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001745}
1746
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001747static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001748 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001749 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001751 return;
1752 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001753 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1754 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001755
Chris Lattner6b6b5372008-06-26 18:38:35 +00001756 // Make sure that there is a string literal as the annotation's single
1757 // argument.
1758 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001759 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001760 return;
1761 }
Sean Huntcf807c42010-08-18 23:23:40 +00001762 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001763}
1764
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001765static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001766 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001767 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001769 return;
1770 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001771
1772 //FIXME: The C++0x version of this attribute has more limited applicabilty
1773 // than GNU's, and should error out when it is used to specify a
1774 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001775
Chris Lattner545dd342008-06-28 23:36:30 +00001776 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001777 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001778 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001779 }
Mike Stumpbf916502009-07-24 19:02:52 +00001780
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001781 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1782}
1783
1784void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1785 if (E->isTypeDependent() || E->isValueDependent()) {
1786 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001787 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001788 return;
1789 }
1790
Sean Huntcf807c42010-08-18 23:23:40 +00001791 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001792 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001793 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1794 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1795 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001796 return;
1797 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001798 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001799 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1800 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001801 return;
1802 }
1803
Sean Huntcf807c42010-08-18 23:23:40 +00001804 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1805}
1806
1807void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1808 // FIXME: Cache the number on the Attr object if non-dependent?
1809 // FIXME: Perform checking of type validity
1810 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1811 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001812}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001813
Mike Stumpbf916502009-07-24 19:02:52 +00001814/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1815/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001816///
Mike Stumpbf916502009-07-24 19:02:52 +00001817/// Despite what would be logical, the mode attribute is a decl attribute, not a
1818/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1819/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001820static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001821 // This attribute isn't documented, but glibc uses it. It changes
1822 // the width of an int or unsigned int to the specified size.
1823
1824 // Check that there aren't any arguments
1825 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001827 return;
1828 }
1829
1830 IdentifierInfo *Name = Attr.getParameterName();
1831 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001832 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001833 return;
1834 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001835
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001836 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001837
1838 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001839 if (Str.startswith("__") && Str.endswith("__"))
1840 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001841
1842 unsigned DestWidth = 0;
1843 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001844 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001845 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001846 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001847 switch (Str[0]) {
1848 case 'Q': DestWidth = 8; break;
1849 case 'H': DestWidth = 16; break;
1850 case 'S': DestWidth = 32; break;
1851 case 'D': DestWidth = 64; break;
1852 case 'X': DestWidth = 96; break;
1853 case 'T': DestWidth = 128; break;
1854 }
1855 if (Str[1] == 'F') {
1856 IntegerMode = false;
1857 } else if (Str[1] == 'C') {
1858 IntegerMode = false;
1859 ComplexMode = true;
1860 } else if (Str[1] != 'I') {
1861 DestWidth = 0;
1862 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001863 break;
1864 case 4:
1865 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1866 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001867 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001868 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001869 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001870 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001871 break;
1872 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001873 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001874 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001875 break;
1876 }
1877
1878 QualType OldTy;
1879 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1880 OldTy = TD->getUnderlyingType();
1881 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1882 OldTy = VD->getType();
1883 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001884 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1885 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001886 return;
1887 }
Eli Friedman73397492009-03-03 06:41:03 +00001888
John McCall183700f2009-09-21 23:43:11 +00001889 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001890 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1891 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001892 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001893 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1894 } else if (ComplexMode) {
1895 if (!OldTy->isComplexType())
1896 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1897 } else {
1898 if (!OldTy->isFloatingType())
1899 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1900 }
1901
Mike Stump390b4cc2009-05-16 07:39:55 +00001902 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1903 // and friends, at least with glibc.
1904 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1905 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001906 // FIXME: Make sure floating-point mappings are accurate
1907 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001908 QualType NewTy;
1909 switch (DestWidth) {
1910 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001911 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001912 return;
1913 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001914 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001915 return;
1916 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001917 if (!IntegerMode) {
1918 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1919 return;
1920 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001921 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001922 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001923 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001924 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001925 break;
1926 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001927 if (!IntegerMode) {
1928 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1929 return;
1930 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001931 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001932 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001933 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001934 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001935 break;
1936 case 32:
1937 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001938 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001939 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001940 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001941 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001942 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001943 break;
1944 case 64:
1945 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001946 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001947 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001948 if (S.Context.Target.getLongWidth() == 64)
1949 NewTy = S.Context.LongTy;
1950 else
1951 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001952 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001953 if (S.Context.Target.getLongWidth() == 64)
1954 NewTy = S.Context.UnsignedLongTy;
1955 else
1956 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001957 break;
Eli Friedman73397492009-03-03 06:41:03 +00001958 case 96:
1959 NewTy = S.Context.LongDoubleTy;
1960 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001961 case 128:
1962 if (!IntegerMode) {
1963 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1964 return;
1965 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001966 if (OldTy->isSignedIntegerType())
1967 NewTy = S.Context.Int128Ty;
1968 else
1969 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001970 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001971 }
1972
Eli Friedman73397492009-03-03 06:41:03 +00001973 if (ComplexMode) {
1974 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001975 }
1976
1977 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001978 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1979 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001980 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001981 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001982 cast<ValueDecl>(D)->setType(NewTy);
1983}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001984
Mike Stump1feade82009-08-26 22:31:08 +00001985static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001986 // check the attribute arguments.
1987 if (Attr.getNumArgs() > 0) {
1988 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1989 return;
1990 }
Anders Carlssone896d982009-02-13 08:11:52 +00001991
Anders Carlsson5bab7882009-02-19 19:16:48 +00001992 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001993 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001994 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001995 return;
1996 }
Mike Stumpbf916502009-07-24 19:02:52 +00001997
Sean Huntcf807c42010-08-18 23:23:40 +00001998 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00001999}
2000
Mike Stump1feade82009-08-26 22:31:08 +00002001static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002002 // check the attribute arguments.
2003 if (Attr.getNumArgs() != 0) {
2004 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2005 return;
2006 }
Mike Stumpbf916502009-07-24 19:02:52 +00002007
Chris Lattnerc5197432009-04-14 17:02:11 +00002008 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002009 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002010 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002011 return;
2012 }
Mike Stumpbf916502009-07-24 19:02:52 +00002013
Sean Huntcf807c42010-08-18 23:23:40 +00002014 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002015}
2016
Chris Lattner7255a2d2010-06-22 00:03:40 +00002017static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2018 Sema &S) {
2019 // check the attribute arguments.
2020 if (Attr.getNumArgs() != 0) {
2021 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2022 return;
2023 }
2024
2025 if (!isa<FunctionDecl>(d)) {
2026 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2027 << Attr.getName() << 0 /*function*/;
2028 return;
2029 }
2030
Sean Huntcf807c42010-08-18 23:23:40 +00002031 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002032}
2033
Chris Lattnercf2a7212009-04-20 19:12:28 +00002034static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002035 // check the attribute arguments.
2036 if (Attr.getNumArgs() != 0) {
2037 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2038 return;
2039 }
Mike Stumpbf916502009-07-24 19:02:52 +00002040
Chris Lattnerc5197432009-04-14 17:02:11 +00002041 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2042 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002043 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002044 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002045 return;
2046 }
Mike Stumpbf916502009-07-24 19:02:52 +00002047
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002048 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002049 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002050 return;
2051 }
Mike Stumpbf916502009-07-24 19:02:52 +00002052
Sean Huntcf807c42010-08-18 23:23:40 +00002053 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002054}
2055
Abramo Bagnarae215f722010-04-30 13:10:51 +00002056static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2057 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2058 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2059 assert(Attr.isInvalid() == false);
2060
2061 switch (Attr.getKind()) {
2062 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002063 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002064 return;
2065 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002066 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002067 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002068 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00002069 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002070 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002071 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00002072 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002073 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002074 case AttributeList::AT_pascal:
2075 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2076 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002077 default:
2078 llvm_unreachable("unexpected attribute kind");
2079 return;
2080 }
2081}
2082
Fariborz Jahanianee760332009-03-27 18:38:55 +00002083static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2084 // check the attribute arguments.
2085 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002086 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002087 return;
2088 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002089
Fariborz Jahanianee760332009-03-27 18:38:55 +00002090 if (!isFunctionOrMethod(d)) {
2091 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002092 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002093 return;
2094 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002095
2096 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2097 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002098 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2099 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002100 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2101 << "regparm" << NumParamsExpr->getSourceRange();
2102 return;
2103 }
2104
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002105 if (S.Context.Target.getRegParmMax() == 0) {
2106 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002107 << NumParamsExpr->getSourceRange();
2108 return;
2109 }
2110
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002111 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002112 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2113 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002114 return;
2115 }
2116
Sean Huntcf807c42010-08-18 23:23:40 +00002117 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2118 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002119}
2120
Sean Huntbbd37c62009-11-21 08:43:09 +00002121static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2122 // check the attribute arguments.
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 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2130 S.Diag(Attr.getLoc(),
2131 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2132 : diag::warn_attribute_wrong_decl_type)
2133 << Attr.getName() << 7 /*virtual method or class*/;
2134 return;
2135 }
Sean Hunt7725e672009-11-25 04:20:27 +00002136
2137 // FIXME: Conform to C++0x redeclaration rules.
2138
2139 if (d->getAttr<FinalAttr>()) {
2140 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2141 return;
2142 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002143
Sean Huntcf807c42010-08-18 23:23:40 +00002144 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002145}
2146
Chris Lattner0744e5f2008-06-29 00:23:49 +00002147//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002148// C++0x member checking attributes
2149//===----------------------------------------------------------------------===//
2150
2151static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2152 if (Attr.getNumArgs() != 0) {
2153 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2154 return;
2155 }
2156
2157 if (!isa<CXXRecordDecl>(d)) {
2158 S.Diag(Attr.getLoc(),
2159 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2160 : diag::warn_attribute_wrong_decl_type)
2161 << Attr.getName() << 9 /*class*/;
2162 return;
2163 }
2164
2165 if (d->getAttr<BaseCheckAttr>()) {
2166 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2167 return;
2168 }
2169
Sean Huntcf807c42010-08-18 23:23:40 +00002170 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002171}
2172
2173static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2174 if (Attr.getNumArgs() != 0) {
2175 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2176 return;
2177 }
2178
2179 if (!isa<RecordDecl>(d->getDeclContext())) {
2180 // FIXME: It's not the type that's the problem
2181 S.Diag(Attr.getLoc(),
2182 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2183 : diag::warn_attribute_wrong_decl_type)
2184 << Attr.getName() << 11 /*member*/;
2185 return;
2186 }
2187
2188 // FIXME: Conform to C++0x redeclaration rules.
2189
2190 if (d->getAttr<HidingAttr>()) {
2191 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2192 return;
2193 }
2194
Sean Huntcf807c42010-08-18 23:23:40 +00002195 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002196}
2197
2198static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2199 if (Attr.getNumArgs() != 0) {
2200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2201 return;
2202 }
2203
2204 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2205 // FIXME: It's not the type that's the problem
2206 S.Diag(Attr.getLoc(),
2207 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2208 : diag::warn_attribute_wrong_decl_type)
2209 << Attr.getName() << 10 /*virtual method*/;
2210 return;
2211 }
2212
2213 // FIXME: Conform to C++0x redeclaration rules.
2214
2215 if (d->getAttr<OverrideAttr>()) {
2216 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2217 return;
2218 }
2219
Sean Huntcf807c42010-08-18 23:23:40 +00002220 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002221}
2222
2223//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002224// Checker-specific attribute handlers.
2225//===----------------------------------------------------------------------===//
2226
2227static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2228 Sema &S) {
2229
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002230 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002231
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002232 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2233 RetTy = MD->getResultType();
2234 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2235 RetTy = FD->getResultType();
2236 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002237 SourceLocation L = Attr.getLoc();
2238 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2239 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002240 return;
2241 }
Mike Stumpbf916502009-07-24 19:02:52 +00002242
Ted Kremenek6217b802009-07-29 21:53:49 +00002243 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002244 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002245 SourceLocation L = Attr.getLoc();
2246 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2247 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002248 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002249 }
Mike Stumpbf916502009-07-24 19:02:52 +00002250
Ted Kremenekb71368d2009-05-09 02:44:38 +00002251 switch (Attr.getKind()) {
2252 default:
2253 assert(0 && "invalid ownership attribute");
2254 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002255 case AttributeList::AT_cf_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002256 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002257 return;
2258 case AttributeList::AT_ns_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002259 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002260 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002261 case AttributeList::AT_cf_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002262 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002263 return;
2264 case AttributeList::AT_ns_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002265 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002266 return;
2267 };
2268}
2269
Charles Davisf0122fe2010-02-16 18:27:26 +00002270static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2271 return Attr.getKind() == AttributeList::AT_dllimport ||
2272 Attr.getKind() == AttributeList::AT_dllexport;
2273}
2274
Ted Kremenekb71368d2009-05-09 02:44:38 +00002275//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002276// Top Level Sema Entry Points
2277//===----------------------------------------------------------------------===//
2278
Sebastian Redla89d82c2008-12-21 19:24:58 +00002279/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002280/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002281/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2282/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002283static void ProcessDeclAttribute(Scope *scope, Decl *D,
2284 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002285 if (Attr.isInvalid())
2286 return;
2287
Charles Davisf0122fe2010-02-16 18:27:26 +00002288 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2289 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002290 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002291 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002292 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002293 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2294 case AttributeList::AT_IBOutletCollection:
2295 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002296 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002297 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002298 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002299 // Ignore these, these are type attributes, handled by
2300 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002301 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002302 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2303 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002304 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002305 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002306 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002307 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002308 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2309 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002310 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002311 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002312 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2313 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2314 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002315 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002316 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002317 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002318 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2319 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2320 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2321 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2322 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2323 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2324 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2325 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002326 case AttributeList::AT_ownership_returns:
2327 case AttributeList::AT_ownership_takes:
2328 case AttributeList::AT_ownership_holds:
2329 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002330 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002331 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2332 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2333 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002334 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002335
2336 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002337 case AttributeList::AT_ns_returns_not_retained:
2338 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002339 case AttributeList::AT_ns_returns_retained:
2340 case AttributeList::AT_cf_returns_retained:
2341 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2342
Nate Begeman6f3d8382009-06-26 06:32:41 +00002343 case AttributeList::AT_reqd_wg_size:
2344 HandleReqdWorkGroupSize(D, Attr, S); break;
2345
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002346 case AttributeList::AT_init_priority:
2347 HandleInitPriorityAttr(D, Attr, S); break;
2348
Sean Hunt7725e672009-11-25 04:20:27 +00002349 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2350 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002351 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2352 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2353 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002354 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002355 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2356 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002357 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002358 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002359 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002360 case AttributeList::AT_transparent_union:
2361 HandleTransparentUnionAttr(D, Attr, S);
2362 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002363 case AttributeList::AT_objc_exception:
2364 HandleObjCExceptionAttr(D, Attr, S);
2365 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002366 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002367 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2368 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2369 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2370 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2371 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2372 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2373 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2374 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2375 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002376 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002377 // Just ignore
2378 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002379 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2380 HandleNoInstrumentFunctionAttr(D, Attr, S);
2381 break;
John McCall04a67a62010-02-05 21:31:56 +00002382 case AttributeList::AT_stdcall:
2383 case AttributeList::AT_cdecl:
2384 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002385 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002386 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002387 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002388 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002389 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002390 // Ask target about the attribute.
2391 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2392 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002393 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2394 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002395 break;
2396 }
2397}
2398
2399/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2400/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002401void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002402 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2403 ProcessDeclAttribute(S, D, *l, *this);
2404 }
2405
2406 // GCC accepts
2407 // static int a9 __attribute__((weakref));
2408 // but that looks really pointless. We reject it.
2409 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2410 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002411 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002412 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002413 }
2414}
2415
Ryan Flynne25ff832009-07-30 03:15:39 +00002416/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2417/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002418NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002419 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002420 NamedDecl *NewD = 0;
2421 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2422 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2423 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002424 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002425 if (FD->getQualifier()) {
2426 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2427 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2428 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002429 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2430 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2431 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002432 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002433 VD->getStorageClass(),
2434 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002435 if (VD->getQualifier()) {
2436 VarDecl *NewVD = cast<VarDecl>(NewD);
2437 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2438 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002439 }
2440 return NewD;
2441}
2442
2443/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2444/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002445void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002446 if (W.getUsed()) return; // only do this once
2447 W.setUsed(true);
2448 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2449 IdentifierInfo *NDId = ND->getIdentifier();
2450 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002451 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2452 NDId->getName()));
2453 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002454 WeakTopLevelDecl.push_back(NewD);
2455 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2456 // to insert Decl at TU scope, sorry.
2457 DeclContext *SavedContext = CurContext;
2458 CurContext = Context.getTranslationUnitDecl();
2459 PushOnScopeChains(NewD, S);
2460 CurContext = SavedContext;
2461 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002462 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002463 }
2464}
2465
Chris Lattner0744e5f2008-06-29 00:23:49 +00002466/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2467/// it, apply them to D. This is a bit tricky because PD can have attributes
2468/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002469void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002470 // Handle #pragma weak
2471 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2472 if (ND->hasLinkage()) {
2473 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2474 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002475 // Identifier referenced by #pragma weak before it was declared
2476 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002477 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2478 }
2479 }
2480 }
2481
Chris Lattner0744e5f2008-06-29 00:23:49 +00002482 // Apply decl attributes from the DeclSpec if present.
2483 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002484 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002485
Chris Lattner0744e5f2008-06-29 00:23:49 +00002486 // Walk the declarator structure, applying decl attributes that were in a type
2487 // position to the decl itself. This handles cases like:
2488 // int *__attr__(x)** D;
2489 // when X is a decl attribute.
2490 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2491 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002492 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002493
Chris Lattner0744e5f2008-06-29 00:23:49 +00002494 // Finally, apply any attributes on the decl itself.
2495 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002496 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002497}
John McCall54abf7d2009-11-04 02:18:39 +00002498
2499/// PushParsingDeclaration - Enter a new "scope" of deprecation
2500/// warnings.
2501///
2502/// The state token we use is the start index of this scope
2503/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002504Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002505 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002506 return (ParsingDeclStackState) DelayedDiagnostics.size();
2507}
2508
John McCalld226f652010-08-21 09:40:31 +00002509void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002510 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2511 ParsingDeclDepth--;
2512
2513 if (DelayedDiagnostics.empty())
2514 return;
2515
2516 unsigned SavedIndex = (unsigned) S;
2517 assert(SavedIndex <= DelayedDiagnostics.size() &&
2518 "saved index is out of bounds");
2519
John McCall58e6f342010-03-16 05:22:47 +00002520 unsigned E = DelayedDiagnostics.size();
2521
John McCall2f514482010-01-27 03:50:35 +00002522 // We only want to actually emit delayed diagnostics when we
2523 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002524 if (D) {
2525 // We really do want to start with 0 here. We get one push for a
2526 // decl spec and another for each declarator; in a decl group like:
2527 // deprecated_typedef foo, *bar, baz();
2528 // only the declarator pops will be passed decls. This is correct;
2529 // we really do need to consider delayed diagnostics from the decl spec
2530 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002531 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002532 if (DelayedDiagnostics[I].Triggered)
2533 continue;
2534
2535 switch (DelayedDiagnostics[I].Kind) {
2536 case DelayedDiagnostic::Deprecation:
2537 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2538 break;
2539
2540 case DelayedDiagnostic::Access:
2541 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2542 break;
2543 }
2544 }
2545 }
2546
John McCall58e6f342010-03-16 05:22:47 +00002547 // Destroy all the delayed diagnostics we're about to pop off.
2548 for (unsigned I = SavedIndex; I != E; ++I)
2549 DelayedDiagnostics[I].destroy();
2550
John McCall2f514482010-01-27 03:50:35 +00002551 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002552}
2553
2554static bool isDeclDeprecated(Decl *D) {
2555 do {
2556 if (D->hasAttr<DeprecatedAttr>())
2557 return true;
2558 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2559 return false;
2560}
2561
John McCall9c3087b2010-08-26 02:13:20 +00002562void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002563 Decl *Ctx) {
2564 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002565 return;
2566
John McCall2f514482010-01-27 03:50:35 +00002567 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002568 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002569 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002570 << DD.getDeprecationDecl()->getDeclName()
2571 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002572 else
2573 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002574 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002575}
2576
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002577void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002578 SourceLocation Loc) {
John McCall54abf7d2009-11-04 02:18:39 +00002579 // Delay if we're currently parsing a declaration.
2580 if (ParsingDeclDepth) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002581 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2582 Message));
John McCall54abf7d2009-11-04 02:18:39 +00002583 return;
2584 }
2585
2586 // Otherwise, don't warn if our current context is deprecated.
2587 if (isDeclDeprecated(cast<Decl>(CurContext)))
2588 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002589 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002590 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2591 << Message;
2592 else
2593 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002594}