blob: 3ae681a17d006c12fc304e196aa058668d971962 [file] [log] [blame]
Chris Lattner2c6fcf52008-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
14#include "Sema.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattner58418ff2008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremenek527042b2009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000044
John McCall9dd450b2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000046}
47
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopes518e3702009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000062}
63
Fariborz Jahanian4447e172009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000076}
77
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000084 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099}
100
Ted Kremenek527042b2009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000106
Chris Lattnera4997152009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000108}
109
Ted Kremenek527042b2009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremenek527042b2009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000121 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000122 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000130 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000131
John McCall96fa4842010-05-17 21:00:27 +0000132 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
133 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000134 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000135
John McCall96fa4842010-05-17 21:00:27 +0000136 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar980c6692008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
Daniel Dunbar980c6692008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000153 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattner58418ff2008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar032db472008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000172 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000173 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000200
Douglas Gregor758a8692009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000203 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000204}
205
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000212
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlsson68e0b682009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000222 else
Anders Carlsson68e0b682009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000224 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000226}
227
Ted Kremenek1f672822010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000234
Ted Kremenek1f672822010-02-18 03:08:58 +0000235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
238 d->addAttr(::new (S.Context) IBActionAttr());
239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000255 d->addAttr(::new (S.Context) IBOutletAttr());
256 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000257 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000260}
261
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000262static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000263 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
264 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000265 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000267 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000268 return;
269 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000270
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000271 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000272
273 // The nonnull attribute only applies to pointers.
274 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000275
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000276 for (AttributeList::arg_iterator I=Attr.arg_begin(),
277 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000278
279
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000280 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000281 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000282 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000283 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
284 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000285 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
286 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000287 return;
288 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000289
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000290 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000291
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000292 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000293 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000294 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000295 return;
296 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000297
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000298 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000299
300 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000301 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000302 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000303 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000304 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
305 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000306 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000307 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000308
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000309 NonNullArgs.push_back(x);
310 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000311
312 // If no arguments were specified to __attribute__((nonnull)) then all pointer
313 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000314 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000315 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
316 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000317 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000318 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000319 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000320
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000321 if (NonNullArgs.empty()) {
322 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
323 return;
324 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000325 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000326
327 unsigned* start = &NonNullArgs[0];
328 unsigned size = NonNullArgs.size();
329 std::sort(start, start + size);
Ted Kremenek510ee252010-02-11 07:31:47 +0000330 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000331}
332
Rafael Espindolac18086a2010-02-23 22:00:30 +0000333static bool isStaticVarOrStaticFunciton(Decl *D) {
334 if (VarDecl *VD = dyn_cast<VarDecl>(D))
335 return VD->getStorageClass() == VarDecl::Static;
336 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
337 return FD->getStorageClass() == FunctionDecl::Static;
338 return false;
339}
340
341static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
342 // Check the attribute arguments.
343 if (Attr.getNumArgs() > 1) {
344 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
345 return;
346 }
347
348 // gcc rejects
349 // class c {
350 // static int a __attribute__((weakref ("v2")));
351 // static int b() __attribute__((weakref ("f3")));
352 // };
353 // and ignores the attributes of
354 // void f(void) {
355 // static int a __attribute__((weakref ("v2")));
356 // }
357 // we reject them
358 if (const DeclContext *Ctx = d->getDeclContext()) {
359 Ctx = Ctx->getLookupContext();
360 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
361 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
362 dyn_cast<NamedDecl>(d)->getNameAsString();
363 return;
364 }
365 }
366
367 // The GCC manual says
368 //
369 // At present, a declaration to which `weakref' is attached can only
370 // be `static'.
371 //
372 // It also says
373 //
374 // Without a TARGET,
375 // given as an argument to `weakref' or to `alias', `weakref' is
376 // equivalent to `weak'.
377 //
378 // gcc 4.4.1 will accept
379 // int a7 __attribute__((weakref));
380 // as
381 // int a7 __attribute__((weak));
382 // This looks like a bug in gcc. We reject that for now. We should revisit
383 // it if this behaviour is actually used.
384
385 if (!isStaticVarOrStaticFunciton(d)) {
386 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
387 dyn_cast<NamedDecl>(d)->getNameAsString();
388 return;
389 }
390
391 // GCC rejects
392 // static ((alias ("y"), weakref)).
393 // Should we? How to check that weakref is before or after alias?
394
395 if (Attr.getNumArgs() == 1) {
396 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
397 Arg = Arg->IgnoreParenCasts();
398 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
399
400 if (Str == 0 || Str->isWide()) {
401 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
402 << "weakref" << 1;
403 return;
404 }
405 // GCC will accept anything as the argument of weakref. Should we
406 // check for an existing decl?
407 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
408 }
409
410 d->addAttr(::new (S.Context) WeakRefAttr());
411}
412
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000413static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000414 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000415 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000416 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000417 return;
418 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000419
Chris Lattner4a927cb2008-06-28 23:36:30 +0000420 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000421 Arg = Arg->IgnoreParenCasts();
422 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000423
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000424 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000425 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000426 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000427 return;
428 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000429
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000430 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000431
Ted Kremenek7f4945a2010-02-11 05:28:37 +0000432 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000433}
434
Mike Stumpd3bb5572009-07-24 19:02:52 +0000435static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000436 Sema &S) {
437 // check the attribute arguments.
438 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000440 return;
441 }
Anders Carlsson88097122009-02-19 19:16:48 +0000442
Chris Lattner4225e232009-04-14 17:02:11 +0000443 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000445 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000446 return;
447 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000448
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000449 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000450}
451
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000452static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
453 // check the attribute arguments.
454 if (Attr.getNumArgs() != 0) {
455 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
456 return;
457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Ted Kremenek08479ae2009-08-15 00:51:46 +0000459 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000460 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000461 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
462 d->addAttr(::new (S.Context) MallocAttr());
463 return;
464 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000465 }
466
Ted Kremenek08479ae2009-08-15 00:51:46 +0000467 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000468}
469
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000470static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnara50099372010-04-30 13:10:51 +0000471 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000472 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000473 if (Attr.getNumArgs() != 0) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000474 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000475 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000476 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000477
Mike Stump88788fe2009-04-29 19:03:13 +0000478 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
479 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stumpfeb19452009-12-15 03:11:10 +0000480 if (VD == 0 || (!VD->getType()->isBlockPointerType()
481 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000482 S.Diag(Attr.getLoc(),
483 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
484 : diag::warn_attribute_wrong_decl_type)
485 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000486 return false;
487 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000488 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000489
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000490 return true;
491}
492
493static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000494 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
495 assert(Attr.isInvalid() == false);
496 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000497}
498
499static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
500 Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000501 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000502 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000503}
504
Alexis Hunt96d5c762009-11-21 08:43:09 +0000505static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
506 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
507 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000508 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000509 return;
510 }
511 // FIXME: Actually store the attribute on the declaration
512}
513
Ted Kremenek39c59a82008-07-25 04:39:19 +0000514static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
515 // check the attribute arguments.
516 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000517 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000518 return;
519 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000520
John McCallcef15822010-03-31 02:47:45 +0000521 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
522 !isa<TypeDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000523 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000524 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000525 return;
526 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000527
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000528 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000529}
530
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000531static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
532 // check the attribute arguments.
533 if (Attr.getNumArgs() != 0) {
534 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
535 return;
536 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000537
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000538 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000539 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000540 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
541 return;
542 }
543 } else if (!isFunctionOrMethod(d)) {
544 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000545 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000546 return;
547 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000548
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000549 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000550}
551
Daniel Dunbar032db472008-07-31 22:40:48 +0000552static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
553 // check the attribute arguments.
554 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000555 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
556 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000557 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000558 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000559
560 int priority = 65535; // FIXME: Do not hardcode such constants.
561 if (Attr.getNumArgs() > 0) {
562 Expr *E = static_cast<Expr *>(Attr.getArg(0));
563 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000564 if (E->isTypeDependent() || E->isValueDependent() ||
565 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000566 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000567 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000568 return;
569 }
570 priority = Idx.getZExtValue();
571 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000572
Chris Lattner4225e232009-04-14 17:02:11 +0000573 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000574 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000575 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000576 return;
577 }
578
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000579 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000580}
581
582static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
583 // check the attribute arguments.
584 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
586 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000587 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000588 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000589
590 int priority = 65535; // FIXME: Do not hardcode such constants.
591 if (Attr.getNumArgs() > 0) {
592 Expr *E = static_cast<Expr *>(Attr.getArg(0));
593 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000594 if (E->isTypeDependent() || E->isValueDependent() ||
595 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000597 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000598 return;
599 }
600 priority = Idx.getZExtValue();
601 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000602
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000603 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000604 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000605 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000606 return;
607 }
608
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000609 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000610}
611
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000612static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000613 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000614 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000616 return;
617 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000618
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000619 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000620}
621
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000622static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
623 // check the attribute arguments.
624 if (Attr.getNumArgs() != 0) {
625 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
626 return;
627 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000628
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000629 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000630}
631
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000632static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000633 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000634 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000635 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000636 return;
637 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000638
Chris Lattner4a927cb2008-06-28 23:36:30 +0000639 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000640 Arg = Arg->IgnoreParenCasts();
641 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000642
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000643 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000644 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000645 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000646 return;
647 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000648
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000649 llvm::StringRef TypeStr = Str->getString();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000650 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000651
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000652 if (TypeStr == "default")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000653 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000654 else if (TypeStr == "hidden")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000655 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000656 else if (TypeStr == "internal")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000657 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000658 else if (TypeStr == "protected")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000659 type = VisibilityAttr::ProtectedVisibility;
660 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000661 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000662 return;
663 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000664
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000665 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000666}
667
Chris Lattner677a3582009-02-14 08:09:34 +0000668static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
669 Sema &S) {
670 if (Attr.getNumArgs() != 0) {
671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
672 return;
673 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000674
Chris Lattner677a3582009-02-14 08:09:34 +0000675 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
676 if (OCI == 0) {
677 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
678 return;
679 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000680
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000681 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000682}
683
684static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000685 if (Attr.getNumArgs() != 0) {
686 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
687 return;
688 }
Chris Lattner677a3582009-02-14 08:09:34 +0000689 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000690 QualType T = TD->getUnderlyingType();
691 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000692 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000693 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
694 return;
695 }
696 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000697 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000698}
699
Mike Stumpd3bb5572009-07-24 19:02:52 +0000700static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000701HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
702 if (Attr.getNumArgs() != 0) {
703 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
704 return;
705 }
706
707 if (!isa<FunctionDecl>(D)) {
708 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
709 return;
710 }
711
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000712 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000713}
714
Steve Naroff3405a732008-09-18 16:44:58 +0000715static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000716 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000717 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000718 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000719 return;
720 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000721
Steve Naroff3405a732008-09-18 16:44:58 +0000722 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000723 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000724 return;
725 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000726
Steve Naroff3405a732008-09-18 16:44:58 +0000727 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000728 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000729 type = BlocksAttr::ByRef;
730 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000731 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000732 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000733 return;
734 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000735
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000736 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000737}
738
Anders Carlssonc181b012008-10-05 18:05:59 +0000739static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
740 // check the attribute arguments.
741 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000742 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
743 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000744 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000745 }
746
Anders Carlssonc181b012008-10-05 18:05:59 +0000747 int sentinel = 0;
748 if (Attr.getNumArgs() > 0) {
749 Expr *E = static_cast<Expr *>(Attr.getArg(0));
750 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000751 if (E->isTypeDependent() || E->isValueDependent() ||
752 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000753 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000754 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000755 return;
756 }
757 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000758
Anders Carlssonc181b012008-10-05 18:05:59 +0000759 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000760 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
761 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000762 return;
763 }
764 }
765
766 int nullPos = 0;
767 if (Attr.getNumArgs() > 1) {
768 Expr *E = static_cast<Expr *>(Attr.getArg(1));
769 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000770 if (E->isTypeDependent() || E->isValueDependent() ||
771 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000772 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000773 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000774 return;
775 }
776 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000777
Anders Carlssonc181b012008-10-05 18:05:59 +0000778 if (nullPos > 1 || nullPos < 0) {
779 // FIXME: This error message could be improved, it would be nice
780 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000781 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
782 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000783 return;
784 }
785 }
786
787 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000788 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000789 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000790
Chris Lattner9363e312009-03-17 23:03:47 +0000791 if (isa<FunctionNoProtoType>(FT)) {
792 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
793 return;
794 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000795
Chris Lattner9363e312009-03-17 23:03:47 +0000796 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000797 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000798 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000799 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000800 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
801 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000802 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000803 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000804 }
805 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000806 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
807 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000808 ;
809 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000810 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000811 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000812 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000813 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000814 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000815 int m = Ty->isFunctionPointerType() ? 0 : 1;
816 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000817 return;
818 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000819 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000821 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000822 return;
823 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000824 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000825 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000826 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000827 return;
828 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000829 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000830}
831
Chris Lattner237f2752009-02-14 07:37:35 +0000832static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
833 // check the attribute arguments.
834 if (Attr.getNumArgs() != 0) {
835 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
836 return;
837 }
838
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000839 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +0000840 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000841 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000842 return;
843 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000844
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000845 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
846 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
847 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +0000848 return;
849 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000850 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
851 if (MD->getResultType()->isVoidType()) {
852 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
853 << Attr.getName() << 1;
854 return;
855 }
856
Nuno Lopes518e3702009-12-20 23:11:08 +0000857 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000858}
859
860static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000861 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000862 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000863 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000864 return;
865 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000866
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000867 /* weak only applies to non-static declarations */
Rafael Espindolac18086a2010-02-23 22:00:30 +0000868 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000869 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
870 dyn_cast<NamedDecl>(D)->getNameAsString();
871 return;
872 }
873
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000874 // TODO: could also be applied to methods?
875 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000877 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000878 return;
879 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000880
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000881 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000882}
883
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000884static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
885 // check the attribute arguments.
886 if (Attr.getNumArgs() != 0) {
887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
888 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000889 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000890
891 // weak_import only applies to variable & function declarations.
892 bool isDef = false;
893 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
894 isDef = (!VD->hasExternalStorage() || VD->getInit());
895 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000896 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000897 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
898 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000899 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000900 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +0000901 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +0000902 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +0000903 !isa<ObjCInterfaceDecl>(D))
904 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +0000905 << Attr.getName() << 2 /*variable and function*/;
906 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000907 }
908
909 // Merge should handle any subsequent violations.
910 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000911 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000912 diag::warn_attribute_weak_import_invalid_on_definition)
913 << "weak_import" << 2 /*variable and function*/;
914 return;
915 }
916
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000917 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000918}
919
Nate Begemanf2758702009-06-26 06:32:41 +0000920static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
921 Sema &S) {
922 // Attribute has 3 arguments.
923 if (Attr.getNumArgs() != 3) {
924 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
925 return;
926 }
927
928 unsigned WGSize[3];
929 for (unsigned i = 0; i < 3; ++i) {
930 Expr *E = static_cast<Expr *>(Attr.getArg(i));
931 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000932 if (E->isTypeDependent() || E->isValueDependent() ||
933 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +0000934 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
935 << "reqd_work_group_size" << E->getSourceRange();
936 return;
937 }
938 WGSize[i] = (unsigned) ArgNum.getZExtValue();
939 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000940 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000941 WGSize[2]));
942}
943
Chris Lattner237f2752009-02-14 07:37:35 +0000944static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000945 // Attribute has no arguments.
946 if (Attr.getNumArgs() != 1) {
947 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
948 return;
949 }
950
951 // Make sure that there is a string literal as the sections's single
952 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +0000953 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
954 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +0000955 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +0000956 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +0000957 return;
958 }
Mike Stump11289f42009-09-09 15:08:12 +0000959
Chris Lattner30ba6742009-08-10 19:03:04 +0000960 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +0000961 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +0000962 if (!Error.empty()) {
963 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
964 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +0000965 return;
966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967
Chris Lattner20aee9b2010-01-12 20:58:53 +0000968 // This attribute cannot be applied to local variables.
969 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
970 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
971 return;
972 }
973
Ted Kremenek7f4945a2010-02-11 05:28:37 +0000974 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +0000975}
976
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000977
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000978static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000979 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000980 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000981 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000982 return;
983 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000984
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000985 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000986}
987
Anders Carlssonb8316282008-10-05 23:32:53 +0000988static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
989 // check the attribute arguments.
990 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000991 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +0000992 return;
993 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000994
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000995 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +0000996}
997
998static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
999 // check the attribute arguments.
1000 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001001 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001002 return;
1003 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001004
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001005 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001006}
1007
Anders Carlssond277d792009-01-31 01:16:18 +00001008static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001009 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1011 return;
1012 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001013
Anders Carlssond277d792009-01-31 01:16:18 +00001014 if (Attr.getNumArgs() != 0) {
1015 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1016 return;
1017 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001018
Anders Carlssond277d792009-01-31 01:16:18 +00001019 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001020
Anders Carlssond277d792009-01-31 01:16:18 +00001021 if (!VD || !VD->hasLocalStorage()) {
1022 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1023 return;
1024 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001025
Anders Carlssond277d792009-01-31 01:16:18 +00001026 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001027 // FIXME: Lookup probably isn't looking in the right place
1028 // FIXME: The lookup source location should be in the attribute, not the
1029 // start of the attribute.
John McCall9f3059a2009-10-09 21:13:30 +00001030 NamedDecl *CleanupDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001031 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCall9f3059a2009-10-09 21:13:30 +00001032 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001033 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001035 Attr.getParameterName();
1036 return;
1037 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001038
Anders Carlssond277d792009-01-31 01:16:18 +00001039 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1040 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001041 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001042 Attr.getParameterName();
1043 return;
1044 }
1045
Anders Carlssond277d792009-01-31 01:16:18 +00001046 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001047 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001048 Attr.getParameterName();
1049 return;
1050 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001051
Anders Carlsson723f55d2009-02-07 23:16:50 +00001052 // We're currently more strict than GCC about what function types we accept.
1053 // If this ever proves to be a problem it should be easy to fix.
1054 QualType Ty = S.Context.getPointerType(VD->getType());
1055 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001056 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001057 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001058 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1059 Attr.getParameterName() << ParamTy << Ty;
1060 return;
1061 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001062
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001063 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001064}
1065
Mike Stumpd3bb5572009-07-24 19:02:52 +00001066/// Handle __attribute__((format_arg((idx)))) attribute based on
1067/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1068static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001069 if (Attr.getNumArgs() != 1) {
1070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1071 return;
1072 }
1073 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1074 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1075 << Attr.getName() << 0 /*function*/;
1076 return;
1077 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001078 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1079 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001080 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1081 unsigned FirstIdx = 1;
1082 // checks for the 2nd argument
1083 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1084 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001085 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1086 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1088 << "format" << 2 << IdxExpr->getSourceRange();
1089 return;
1090 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001091
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001092 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1093 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1094 << "format" << 2 << IdxExpr->getSourceRange();
1095 return;
1096 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001097
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001098 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001099
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001100 // make sure the format string is really a string
1101 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001102
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001103 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1104 if (not_nsstring_type &&
1105 !isCFStringType(Ty, S.Context) &&
1106 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001107 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001108 // FIXME: Should highlight the actual expression that has the wrong type.
1109 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001110 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001111 << IdxExpr->getSourceRange();
1112 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001113 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001114 Ty = getFunctionOrMethodResultType(d);
1115 if (!isNSStringType(Ty, S.Context) &&
1116 !isCFStringType(Ty, S.Context) &&
1117 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001118 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001119 // FIXME: Should highlight the actual expression that has the wrong type.
1120 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001121 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001122 << IdxExpr->getSourceRange();
1123 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001124 }
1125
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001126 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001127}
1128
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001129enum FormatAttrKind {
1130 CFStringFormat,
1131 NSStringFormat,
1132 StrftimeFormat,
1133 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001134 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001135 InvalidFormat
1136};
1137
1138/// getFormatAttrKind - Map from format attribute names to supported format
1139/// types.
1140static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1141 // Check for formats that get handled specially.
1142 if (Format == "NSString")
1143 return NSStringFormat;
1144 if (Format == "CFString")
1145 return CFStringFormat;
1146 if (Format == "strftime")
1147 return StrftimeFormat;
1148
1149 // Otherwise, check for supported formats.
1150 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1151 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1152 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1153 Format == "zcmn_err")
1154 return SupportedFormat;
1155
Duncan Sandsde4fe352010-03-23 14:44:19 +00001156 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1157 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001158 return IgnoredFormat;
1159
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001160 return InvalidFormat;
1161}
1162
Mike Stumpd3bb5572009-07-24 19:02:52 +00001163/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1164/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001165static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001166
Chris Lattner4a927cb2008-06-28 23:36:30 +00001167 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001169 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001170 return;
1171 }
1172
Chris Lattner4a927cb2008-06-28 23:36:30 +00001173 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001174 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001175 return;
1176 }
1177
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001178 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001179 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001180 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001181 return;
1182 }
1183
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001184 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001185 unsigned FirstIdx = 1;
1186
Daniel Dunbar07d07852009-10-18 21:17:35 +00001187 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001188
1189 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001190 if (Format.startswith("__") && Format.endswith("__"))
1191 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001192
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001193 // Check for supported formats.
1194 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001195
1196 if (Kind == IgnoredFormat)
1197 return;
1198
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001199 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001200 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001201 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001202 return;
1203 }
1204
1205 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001206 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001207 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001208 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1209 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001210 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001211 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001212 return;
1213 }
1214
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001215 // FIXME: We should handle the implicit 'this' parameter in a more generic
1216 // way that can be used for other arguments.
1217 bool HasImplicitThisParam = false;
1218 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1219 if (MD->isInstance()) {
1220 HasImplicitThisParam = true;
1221 NumArgs++;
1222 }
1223 }
Mike Stump11289f42009-09-09 15:08:12 +00001224
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001225 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001226 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001227 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001228 return;
1229 }
1230
1231 // FIXME: Do we need to bounds check?
1232 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001233
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001234 if (HasImplicitThisParam) {
1235 if (ArgIdx == 0) {
1236 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1237 << "a string type" << IdxExpr->getSourceRange();
1238 return;
1239 }
1240 ArgIdx--;
1241 }
Mike Stump11289f42009-09-09 15:08:12 +00001242
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001243 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001244 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001245
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001246 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001247 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001248 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1249 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001250 return;
1251 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001252 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001253 // FIXME: do we need to check if the type is NSString*? What are the
1254 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001255 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001256 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001257 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1258 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001259 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001260 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001261 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001262 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001263 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001264 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1265 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001266 return;
1267 }
1268
1269 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001270 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001271 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001272 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1273 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001274 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001275 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001276 return;
1277 }
1278
1279 // check if the function is variadic if the 3rd argument non-zero
1280 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001281 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001282 ++NumArgs; // +1 for ...
1283 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001284 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001285 return;
1286 }
1287 }
1288
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001289 // strftime requires FirstArg to be 0 because it doesn't read from any
1290 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001291 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001292 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001293 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1294 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001295 return;
1296 }
1297 // if 0 it disables parameter checking (to use with e.g. va_list)
1298 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001299 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001300 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001301 return;
1302 }
1303
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001304 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001305 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001306}
1307
Chris Lattnera663a0a2008-06-29 00:28:59 +00001308static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1309 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001310 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001311 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001312 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001313 return;
1314 }
1315
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001316 // Try to find the underlying union declaration.
1317 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001318 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001319 if (TD && TD->getUnderlyingType()->isUnionType())
1320 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1321 else
1322 RD = dyn_cast<RecordDecl>(d);
1323
1324 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001325 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001326 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001327 return;
1328 }
1329
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001330 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001331 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001332 diag::warn_transparent_union_attribute_not_definition);
1333 return;
1334 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001335
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001336 RecordDecl::field_iterator Field = RD->field_begin(),
1337 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001338 if (Field == FieldEnd) {
1339 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1340 return;
1341 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001342
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001343 FieldDecl *FirstField = *Field;
1344 QualType FirstType = FirstField->getType();
1345 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001346 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001347 diag::warn_transparent_union_attribute_floating);
1348 return;
1349 }
1350
1351 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1352 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1353 for (; Field != FieldEnd; ++Field) {
1354 QualType FieldType = Field->getType();
1355 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1356 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1357 // Warn if we drop the attribute.
1358 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001359 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001360 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001361 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001362 diag::warn_transparent_union_attribute_field_size_align)
1363 << isSize << Field->getDeclName() << FieldBits;
1364 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001365 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001366 diag::note_transparent_union_first_field_size_align)
1367 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001368 return;
1369 }
1370 }
1371
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001372 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001373}
1374
Chris Lattnera663a0a2008-06-29 00:28:59 +00001375static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001376 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001377 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001379 return;
1380 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001381 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1382 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001383
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001384 // Make sure that there is a string literal as the annotation's single
1385 // argument.
1386 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001387 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001388 return;
1389 }
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001390 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001391}
1392
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001393static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001394 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001395 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001396 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001397 return;
1398 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001399
1400 //FIXME: The C++0x version of this attribute has more limited applicabilty
1401 // than GNU's, and should error out when it is used to specify a
1402 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001403
1404 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001405 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001406 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001407 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001408 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001409 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001410 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001411 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001412
Chris Lattner4627b742008-06-28 23:50:44 +00001413 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1414 llvm::APSInt Alignment(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001415 if (alignmentExpr->isTypeDependent() || alignmentExpr->isValueDependent() ||
1416 !alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001417 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1418 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001419 return;
1420 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001421 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001422 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001423 << alignmentExpr->getSourceRange();
1424 return;
1425 }
1426
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001427 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001428}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001429
Mike Stumpd3bb5572009-07-24 19:02:52 +00001430/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1431/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001432///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001433/// Despite what would be logical, the mode attribute is a decl attribute, not a
1434/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1435/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001436static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001437 // This attribute isn't documented, but glibc uses it. It changes
1438 // the width of an int or unsigned int to the specified size.
1439
1440 // Check that there aren't any arguments
1441 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001442 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001443 return;
1444 }
1445
1446 IdentifierInfo *Name = Attr.getParameterName();
1447 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001448 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001449 return;
1450 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001451
Daniel Dunbar07d07852009-10-18 21:17:35 +00001452 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001453
1454 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001455 if (Str.startswith("__") && Str.endswith("__"))
1456 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001457
1458 unsigned DestWidth = 0;
1459 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001460 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001461 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001462 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001463 switch (Str[0]) {
1464 case 'Q': DestWidth = 8; break;
1465 case 'H': DestWidth = 16; break;
1466 case 'S': DestWidth = 32; break;
1467 case 'D': DestWidth = 64; break;
1468 case 'X': DestWidth = 96; break;
1469 case 'T': DestWidth = 128; break;
1470 }
1471 if (Str[1] == 'F') {
1472 IntegerMode = false;
1473 } else if (Str[1] == 'C') {
1474 IntegerMode = false;
1475 ComplexMode = true;
1476 } else if (Str[1] != 'I') {
1477 DestWidth = 0;
1478 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001479 break;
1480 case 4:
1481 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1482 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001483 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001484 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001485 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001486 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001487 break;
1488 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001489 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001490 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001491 break;
1492 }
1493
1494 QualType OldTy;
1495 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1496 OldTy = TD->getUnderlyingType();
1497 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1498 OldTy = VD->getType();
1499 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001500 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1501 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001502 return;
1503 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001504
John McCall9dd450b2009-09-21 23:43:11 +00001505 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001506 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1507 else if (IntegerMode) {
1508 if (!OldTy->isIntegralType())
1509 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1510 } else if (ComplexMode) {
1511 if (!OldTy->isComplexType())
1512 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1513 } else {
1514 if (!OldTy->isFloatingType())
1515 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1516 }
1517
Mike Stump87c57ac2009-05-16 07:39:55 +00001518 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1519 // and friends, at least with glibc.
1520 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1521 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001522 // FIXME: Make sure floating-point mappings are accurate
1523 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001524 QualType NewTy;
1525 switch (DestWidth) {
1526 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001527 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001528 return;
1529 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001530 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001531 return;
1532 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001533 if (!IntegerMode) {
1534 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1535 return;
1536 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001537 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001538 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001539 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001540 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001541 break;
1542 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001543 if (!IntegerMode) {
1544 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1545 return;
1546 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001547 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001548 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001549 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001550 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001551 break;
1552 case 32:
1553 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001554 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001555 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001556 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001557 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001558 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001559 break;
1560 case 64:
1561 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001562 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001563 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00001564 if (S.Context.Target.getLongWidth() == 64)
1565 NewTy = S.Context.LongTy;
1566 else
1567 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001568 else
Chandler Carruth72343702010-01-26 06:39:24 +00001569 if (S.Context.Target.getLongWidth() == 64)
1570 NewTy = S.Context.UnsignedLongTy;
1571 else
1572 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001573 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001574 case 96:
1575 NewTy = S.Context.LongDoubleTy;
1576 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001577 case 128:
1578 if (!IntegerMode) {
1579 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1580 return;
1581 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00001582 if (OldTy->isSignedIntegerType())
1583 NewTy = S.Context.Int128Ty;
1584 else
1585 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001586 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001587 }
1588
Eli Friedman4735374e2009-03-03 06:41:03 +00001589 if (ComplexMode) {
1590 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001591 }
1592
1593 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001594 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1595 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001596 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001597 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001598 cast<ValueDecl>(D)->setType(NewTy);
1599}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001600
Mike Stump3722f582009-08-26 22:31:08 +00001601static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001602 // check the attribute arguments.
1603 if (Attr.getNumArgs() > 0) {
1604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1605 return;
1606 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001607
Anders Carlsson88097122009-02-19 19:16:48 +00001608 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001609 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001610 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001611 return;
1612 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001613
Mike Stump3722f582009-08-26 22:31:08 +00001614 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001615}
1616
Mike Stump3722f582009-08-26 22:31:08 +00001617static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001618 // check the attribute arguments.
1619 if (Attr.getNumArgs() != 0) {
1620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1621 return;
1622 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001623
Chris Lattner4225e232009-04-14 17:02:11 +00001624 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001626 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001627 return;
1628 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001629
Mike Stump3722f582009-08-26 22:31:08 +00001630 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001631}
1632
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001633static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001634 // check the attribute arguments.
1635 if (Attr.getNumArgs() != 0) {
1636 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1637 return;
1638 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001639
Chris Lattner4225e232009-04-14 17:02:11 +00001640 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1641 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001643 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001644 return;
1645 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001646
Douglas Gregor35b57532009-10-27 21:01:01 +00001647 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001648 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001649 return;
1650 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001651
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001652 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001653}
1654
Abramo Bagnara50099372010-04-30 13:10:51 +00001655static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1656 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1657 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1658 assert(Attr.isInvalid() == false);
1659
1660 switch (Attr.getKind()) {
1661 case AttributeList::AT_fastcall:
1662 d->addAttr(::new (S.Context) FastCallAttr());
1663 return;
1664 case AttributeList::AT_stdcall:
1665 d->addAttr(::new (S.Context) StdCallAttr());
1666 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00001667 case AttributeList::AT_thiscall:
1668 d->addAttr(::new (S.Context) ThisCallAttr());
Abramo Bagnara50099372010-04-30 13:10:51 +00001669 case AttributeList::AT_cdecl:
1670 d->addAttr(::new (S.Context) CDeclAttr());
1671 return;
1672 default:
1673 llvm_unreachable("unexpected attribute kind");
1674 return;
1675 }
1676}
1677
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001678static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1679 // check the attribute arguments.
1680 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001682 return;
1683 }
Eli Friedman7044b762009-03-27 21:06:47 +00001684
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001685 if (!isFunctionOrMethod(d)) {
1686 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001687 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001688 return;
1689 }
Eli Friedman7044b762009-03-27 21:06:47 +00001690
1691 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1692 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001693 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1694 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman7044b762009-03-27 21:06:47 +00001695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1696 << "regparm" << NumParamsExpr->getSourceRange();
1697 return;
1698 }
1699
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001700 if (S.Context.Target.getRegParmMax() == 0) {
1701 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001702 << NumParamsExpr->getSourceRange();
1703 return;
1704 }
1705
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001706 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001707 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1708 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001709 return;
1710 }
1711
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001712 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001713}
1714
Alexis Hunt96d5c762009-11-21 08:43:09 +00001715static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1716 // check the attribute arguments.
1717 if (Attr.getNumArgs() != 0) {
1718 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1719 return;
1720 }
1721
1722 if (!isa<CXXRecordDecl>(d)
1723 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1724 S.Diag(Attr.getLoc(),
1725 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1726 : diag::warn_attribute_wrong_decl_type)
1727 << Attr.getName() << 7 /*virtual method or class*/;
1728 return;
1729 }
Alexis Hunt54a02542009-11-25 04:20:27 +00001730
1731 // FIXME: Conform to C++0x redeclaration rules.
1732
1733 if (d->getAttr<FinalAttr>()) {
1734 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1735 return;
1736 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001737
1738 d->addAttr(::new (S.Context) FinalAttr());
1739}
1740
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001741//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00001742// C++0x member checking attributes
1743//===----------------------------------------------------------------------===//
1744
1745static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1746 if (Attr.getNumArgs() != 0) {
1747 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1748 return;
1749 }
1750
1751 if (!isa<CXXRecordDecl>(d)) {
1752 S.Diag(Attr.getLoc(),
1753 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1754 : diag::warn_attribute_wrong_decl_type)
1755 << Attr.getName() << 9 /*class*/;
1756 return;
1757 }
1758
1759 if (d->getAttr<BaseCheckAttr>()) {
1760 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1761 return;
1762 }
1763
1764 d->addAttr(::new (S.Context) BaseCheckAttr());
1765}
1766
1767static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1768 if (Attr.getNumArgs() != 0) {
1769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1770 return;
1771 }
1772
1773 if (!isa<RecordDecl>(d->getDeclContext())) {
1774 // FIXME: It's not the type that's the problem
1775 S.Diag(Attr.getLoc(),
1776 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1777 : diag::warn_attribute_wrong_decl_type)
1778 << Attr.getName() << 11 /*member*/;
1779 return;
1780 }
1781
1782 // FIXME: Conform to C++0x redeclaration rules.
1783
1784 if (d->getAttr<HidingAttr>()) {
1785 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1786 return;
1787 }
1788
1789 d->addAttr(::new (S.Context) HidingAttr());
1790}
1791
1792static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1793 if (Attr.getNumArgs() != 0) {
1794 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1795 return;
1796 }
1797
1798 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1799 // FIXME: It's not the type that's the problem
1800 S.Diag(Attr.getLoc(),
1801 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1802 : diag::warn_attribute_wrong_decl_type)
1803 << Attr.getName() << 10 /*virtual method*/;
1804 return;
1805 }
1806
1807 // FIXME: Conform to C++0x redeclaration rules.
1808
1809 if (d->getAttr<OverrideAttr>()) {
1810 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1811 return;
1812 }
1813
1814 d->addAttr(::new (S.Context) OverrideAttr());
1815}
1816
1817//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001818// Checker-specific attribute handlers.
1819//===----------------------------------------------------------------------===//
1820
1821static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1822 Sema &S) {
1823
Ted Kremenek3b204e42009-05-13 21:07:32 +00001824 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001825
Ted Kremenek3b204e42009-05-13 21:07:32 +00001826 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1827 RetTy = MD->getResultType();
1828 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1829 RetTy = FD->getResultType();
1830 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001831 SourceLocation L = Attr.getLoc();
1832 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1833 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001834 return;
1835 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001836
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001837 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001838 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001839 SourceLocation L = Attr.getLoc();
1840 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1841 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001842 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001843 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001844
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001845 switch (Attr.getKind()) {
1846 default:
1847 assert(0 && "invalid ownership attribute");
1848 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00001849 case AttributeList::AT_cf_returns_not_retained:
1850 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1851 return;
1852 case AttributeList::AT_ns_returns_not_retained:
1853 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1854 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001855 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001856 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001857 return;
1858 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001859 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001860 return;
1861 };
1862}
1863
Charles Davis163855f2010-02-16 18:27:26 +00001864static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1865 return Attr.getKind() == AttributeList::AT_dllimport ||
1866 Attr.getKind() == AttributeList::AT_dllexport;
1867}
1868
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001869//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001870// Top Level Sema Entry Points
1871//===----------------------------------------------------------------------===//
1872
Sebastian Redlfc24b632008-12-21 19:24:58 +00001873/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001874/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001875/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1876/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001877static void ProcessDeclAttribute(Scope *scope, Decl *D,
1878 const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +00001879 if (Attr.isInvalid())
1880 return;
1881
Charles Davis163855f2010-02-16 18:27:26 +00001882 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1883 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00001884 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001885 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001886 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
1887 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001888 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001889 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00001890 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001891 // Ignore these, these are type attributes, handled by
1892 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001893 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001894 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1895 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001896 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001897 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001898 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001899 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001900 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1901 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001902 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001903 HandleDependencyAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001904 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1905 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1906 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001907 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001908 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001909 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001910 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1911 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1912 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1913 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1914 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1915 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1916 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1917 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1918 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1919 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1920 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001921
1922 // Checker-specific.
Ted Kremenekd9c66632010-02-18 00:05:45 +00001923 case AttributeList::AT_ns_returns_not_retained:
1924 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001925 case AttributeList::AT_ns_returns_retained:
1926 case AttributeList::AT_cf_returns_retained:
1927 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1928
Nate Begemanf2758702009-06-26 06:32:41 +00001929 case AttributeList::AT_reqd_wg_size:
1930 HandleReqdWorkGroupSize(D, Attr, S); break;
1931
Alexis Hunt54a02542009-11-25 04:20:27 +00001932 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1933 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001934 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1935 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1936 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001937 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00001938 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1939 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001940 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001941 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001942 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001943 case AttributeList::AT_transparent_union:
1944 HandleTransparentUnionAttr(D, Attr, S);
1945 break;
Chris Lattner677a3582009-02-14 08:09:34 +00001946 case AttributeList::AT_objc_exception:
1947 HandleObjCExceptionAttr(D, Attr, S);
1948 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001949 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001950 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1951 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1952 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1953 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1954 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1955 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1956 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1957 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1958 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001959 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00001960 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00001961 // Just ignore
1962 break;
John McCallab26cfa2010-02-05 21:31:56 +00001963 case AttributeList::AT_stdcall:
1964 case AttributeList::AT_cdecl:
1965 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001966 case AttributeList::AT_thiscall:
Abramo Bagnara50099372010-04-30 13:10:51 +00001967 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00001968 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001969 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001970 // Ask target about the attribute.
1971 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1972 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1973 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001974 break;
1975 }
1976}
1977
1978/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1979/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00001980void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001981 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
1982 ProcessDeclAttribute(S, D, *l, *this);
1983 }
1984
1985 // GCC accepts
1986 // static int a9 __attribute__((weakref));
1987 // but that looks really pointless. We reject it.
1988 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
1989 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
1990 dyn_cast<NamedDecl>(D)->getNameAsString();
1991 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001992 }
1993}
1994
Ryan Flynn7d470f32009-07-30 03:15:39 +00001995/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1996/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00001997NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001998 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00001999 NamedDecl *NewD = 0;
2000 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2001 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2002 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002003 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002004 if (FD->getQualifier()) {
2005 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2006 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2007 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002008 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2009 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2010 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002011 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002012 VD->getStorageClass(),
2013 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002014 if (VD->getQualifier()) {
2015 VarDecl *NewVD = cast<VarDecl>(NewD);
2016 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2017 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002018 }
2019 return NewD;
2020}
2021
2022/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2023/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002024void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002025 if (W.getUsed()) return; // only do this once
2026 W.setUsed(true);
2027 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2028 IdentifierInfo *NDId = ND->getIdentifier();
2029 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek7f4945a2010-02-11 05:28:37 +00002030 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnere6eab982009-09-08 18:10:11 +00002031 NewD->addAttr(::new (Context) WeakAttr());
2032 WeakTopLevelDecl.push_back(NewD);
2033 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2034 // to insert Decl at TU scope, sorry.
2035 DeclContext *SavedContext = CurContext;
2036 CurContext = Context.getTranslationUnitDecl();
2037 PushOnScopeChains(NewD, S);
2038 CurContext = SavedContext;
2039 } else { // just add weak to existing
2040 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002041 }
2042}
2043
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002044/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2045/// it, apply them to D. This is a bit tricky because PD can have attributes
2046/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002047void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002048 // Handle #pragma weak
2049 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2050 if (ND->hasLinkage()) {
2051 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2052 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002053 // Identifier referenced by #pragma weak before it was declared
2054 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002055 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2056 }
2057 }
2058 }
2059
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002060 // Apply decl attributes from the DeclSpec if present.
2061 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002062 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002063
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002064 // Walk the declarator structure, applying decl attributes that were in a type
2065 // position to the decl itself. This handles cases like:
2066 // int *__attr__(x)** D;
2067 // when X is a decl attribute.
2068 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2069 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002070 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002071
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002072 // Finally, apply any attributes on the decl itself.
2073 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002074 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002075}
John McCall28a6aea2009-11-04 02:18:39 +00002076
2077/// PushParsingDeclaration - Enter a new "scope" of deprecation
2078/// warnings.
2079///
2080/// The state token we use is the start index of this scope
2081/// on the warning stack.
2082Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2083 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002084 return (ParsingDeclStackState) DelayedDiagnostics.size();
2085}
2086
2087void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2088 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2089 ParsingDeclDepth--;
2090
2091 if (DelayedDiagnostics.empty())
2092 return;
2093
2094 unsigned SavedIndex = (unsigned) S;
2095 assert(SavedIndex <= DelayedDiagnostics.size() &&
2096 "saved index is out of bounds");
2097
John McCall1064d7e2010-03-16 05:22:47 +00002098 unsigned E = DelayedDiagnostics.size();
2099
John McCall86121512010-01-27 03:50:35 +00002100 // We only want to actually emit delayed diagnostics when we
2101 // successfully parsed a decl.
2102 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2103 if (D) {
2104 // We really do want to start with 0 here. We get one push for a
2105 // decl spec and another for each declarator; in a decl group like:
2106 // deprecated_typedef foo, *bar, baz();
2107 // only the declarator pops will be passed decls. This is correct;
2108 // we really do need to consider delayed diagnostics from the decl spec
2109 // for each of the different declarations.
John McCall1064d7e2010-03-16 05:22:47 +00002110 for (unsigned I = 0; I != E; ++I) {
John McCall86121512010-01-27 03:50:35 +00002111 if (DelayedDiagnostics[I].Triggered)
2112 continue;
2113
2114 switch (DelayedDiagnostics[I].Kind) {
2115 case DelayedDiagnostic::Deprecation:
2116 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2117 break;
2118
2119 case DelayedDiagnostic::Access:
2120 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2121 break;
2122 }
2123 }
2124 }
2125
John McCall1064d7e2010-03-16 05:22:47 +00002126 // Destroy all the delayed diagnostics we're about to pop off.
2127 for (unsigned I = SavedIndex; I != E; ++I)
2128 DelayedDiagnostics[I].destroy();
2129
John McCall86121512010-01-27 03:50:35 +00002130 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002131}
2132
2133static bool isDeclDeprecated(Decl *D) {
2134 do {
2135 if (D->hasAttr<DeprecatedAttr>())
2136 return true;
2137 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2138 return false;
2139}
2140
John McCall86121512010-01-27 03:50:35 +00002141void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2142 Decl *Ctx) {
2143 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002144 return;
2145
John McCall86121512010-01-27 03:50:35 +00002146 DD.Triggered = true;
2147 Diag(DD.Loc, diag::warn_deprecated)
2148 << DD.DeprecationData.Decl->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002149}
2150
2151void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2152 // Delay if we're currently parsing a declaration.
2153 if (ParsingDeclDepth) {
John McCall86121512010-01-27 03:50:35 +00002154 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall28a6aea2009-11-04 02:18:39 +00002155 return;
2156 }
2157
2158 // Otherwise, don't warn if our current context is deprecated.
2159 if (isDeclDeprecated(cast<Decl>(CurContext)))
2160 return;
2161
2162 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2163}