blob: d12dec4561c1ffcafbd7addfba7380f183999c12 [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))
121 return BD->IsVariadic();
122 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 McCall9dd450b2009-09-21 23:43:11 +0000132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000133 if (!ClsT)
134 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000135
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000136 IdentifierInfo* ClsName = ClsT->getDecl()->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();
153 if (RD->getTagKind() != TagDecl::TK_struct)
154 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);
283 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
285 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000286 return;
287 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000288
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000289 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000290
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000291 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000293 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000294 return;
295 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000296
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000297 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000298
299 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000300 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000301 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000302 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000303 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
304 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000305 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000306 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000307
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000308 NonNullArgs.push_back(x);
309 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000310
311 // If no arguments were specified to __attribute__((nonnull)) then all pointer
312 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000313 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000314 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
315 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000316 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000317 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000318 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000319
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000320 if (NonNullArgs.empty()) {
321 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
322 return;
323 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000324 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000325
326 unsigned* start = &NonNullArgs[0];
327 unsigned size = NonNullArgs.size();
328 std::sort(start, start + size);
Ted Kremenek510ee252010-02-11 07:31:47 +0000329 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000330}
331
Rafael Espindolac18086a2010-02-23 22:00:30 +0000332static bool isStaticVarOrStaticFunciton(Decl *D) {
333 if (VarDecl *VD = dyn_cast<VarDecl>(D))
334 return VD->getStorageClass() == VarDecl::Static;
335 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
336 return FD->getStorageClass() == FunctionDecl::Static;
337 return false;
338}
339
340static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
341 // Check the attribute arguments.
342 if (Attr.getNumArgs() > 1) {
343 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
344 return;
345 }
346
347 // gcc rejects
348 // class c {
349 // static int a __attribute__((weakref ("v2")));
350 // static int b() __attribute__((weakref ("f3")));
351 // };
352 // and ignores the attributes of
353 // void f(void) {
354 // static int a __attribute__((weakref ("v2")));
355 // }
356 // we reject them
357 if (const DeclContext *Ctx = d->getDeclContext()) {
358 Ctx = Ctx->getLookupContext();
359 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
360 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
361 dyn_cast<NamedDecl>(d)->getNameAsString();
362 return;
363 }
364 }
365
366 // The GCC manual says
367 //
368 // At present, a declaration to which `weakref' is attached can only
369 // be `static'.
370 //
371 // It also says
372 //
373 // Without a TARGET,
374 // given as an argument to `weakref' or to `alias', `weakref' is
375 // equivalent to `weak'.
376 //
377 // gcc 4.4.1 will accept
378 // int a7 __attribute__((weakref));
379 // as
380 // int a7 __attribute__((weak));
381 // This looks like a bug in gcc. We reject that for now. We should revisit
382 // it if this behaviour is actually used.
383
384 if (!isStaticVarOrStaticFunciton(d)) {
385 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
386 dyn_cast<NamedDecl>(d)->getNameAsString();
387 return;
388 }
389
390 // GCC rejects
391 // static ((alias ("y"), weakref)).
392 // Should we? How to check that weakref is before or after alias?
393
394 if (Attr.getNumArgs() == 1) {
395 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
396 Arg = Arg->IgnoreParenCasts();
397 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
398
399 if (Str == 0 || Str->isWide()) {
400 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
401 << "weakref" << 1;
402 return;
403 }
404 // GCC will accept anything as the argument of weakref. Should we
405 // check for an existing decl?
406 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
407 }
408
409 d->addAttr(::new (S.Context) WeakRefAttr());
410}
411
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000412static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000413 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000414 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000416 return;
417 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000418
Chris Lattner4a927cb2008-06-28 23:36:30 +0000419 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000420 Arg = Arg->IgnoreParenCasts();
421 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000422
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000423 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000424 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000425 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000426 return;
427 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000428
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000429 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000430
Ted Kremenek7f4945a2010-02-11 05:28:37 +0000431 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000432}
433
Mike Stumpd3bb5572009-07-24 19:02:52 +0000434static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000435 Sema &S) {
436 // check the attribute arguments.
437 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000439 return;
440 }
Anders Carlsson88097122009-02-19 19:16:48 +0000441
Chris Lattner4225e232009-04-14 17:02:11 +0000442 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000444 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000445 return;
446 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000447
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000448 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000449}
450
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000451static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
452 // check the attribute arguments.
453 if (Attr.getNumArgs() != 0) {
454 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
455 return;
456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenek08479ae2009-08-15 00:51:46 +0000458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000459 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000460 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
461 d->addAttr(::new (S.Context) MallocAttr());
462 return;
463 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000464 }
465
Ted Kremenek08479ae2009-08-15 00:51:46 +0000466 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000467}
468
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000469static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000470 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000471 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000472 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000473 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000474 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000475 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000476
Mike Stump88788fe2009-04-29 19:03:13 +0000477 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
478 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stumpfeb19452009-12-15 03:11:10 +0000479 if (VD == 0 || (!VD->getType()->isBlockPointerType()
480 && !VD->getType()->isFunctionPointerType())) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000481 S.Diag(Attr.getLoc(),
482 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
483 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000484 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000485 return false;
486 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000487 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000488
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000489 return true;
490}
491
492static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekee0934e2010-03-26 22:57:10 +0000493 // NOTE: We don't add the attribute to a FunctionDecl because the noreturn
494 // trait will be part of the function's type.
495
John McCallab26cfa2010-02-05 21:31:56 +0000496 // Don't apply as a decl attribute to ValueDecl.
497 // FIXME: probably ought to diagnose this.
498 if (isa<ValueDecl>(d))
499 return;
500
Mike Stumpd3bb5572009-07-24 19:02:52 +0000501 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000502 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000503}
504
505static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
506 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000507 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000508 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000509}
510
Alexis Hunt96d5c762009-11-21 08:43:09 +0000511static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
512 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
513 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000514 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000515 return;
516 }
517 // FIXME: Actually store the attribute on the declaration
518}
519
Ted Kremenek39c59a82008-07-25 04:39:19 +0000520static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
521 // check the attribute arguments.
522 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000524 return;
525 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000526
Ted Kremenek6bf658a2010-02-25 03:26:51 +0000527 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000528 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000529 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000530 return;
531 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000532
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000533 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000534}
535
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000536static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
537 // check the attribute arguments.
538 if (Attr.getNumArgs() != 0) {
539 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
540 return;
541 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000542
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000543 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000544 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000545 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
546 return;
547 }
548 } else if (!isFunctionOrMethod(d)) {
549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000550 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000551 return;
552 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000553
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000554 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000555}
556
Daniel Dunbar032db472008-07-31 22:40:48 +0000557static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
558 // check the attribute arguments.
559 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000560 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
561 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000562 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000563 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000564
565 int priority = 65535; // FIXME: Do not hardcode such constants.
566 if (Attr.getNumArgs() > 0) {
567 Expr *E = static_cast<Expr *>(Attr.getArg(0));
568 llvm::APSInt Idx(32);
569 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000570 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000571 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000572 return;
573 }
574 priority = Idx.getZExtValue();
575 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000576
Chris Lattner4225e232009-04-14 17:02:11 +0000577 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000578 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000579 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000580 return;
581 }
582
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000583 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000584}
585
586static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
587 // check the attribute arguments.
588 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
590 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000591 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000592 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000593
594 int priority = 65535; // FIXME: Do not hardcode such constants.
595 if (Attr.getNumArgs() > 0) {
596 Expr *E = static_cast<Expr *>(Attr.getArg(0));
597 llvm::APSInt Idx(32);
598 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000599 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000600 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000601 return;
602 }
603 priority = Idx.getZExtValue();
604 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000605
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000606 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000608 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000609 return;
610 }
611
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000612 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000613}
614
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000615static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000616 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000617 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000618 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000619 return;
620 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000621
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000622 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000623}
624
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000625static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
626 // check the attribute arguments.
627 if (Attr.getNumArgs() != 0) {
628 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
629 return;
630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000631
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000632 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000633}
634
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000635static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000636 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000637 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000639 return;
640 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000641
Chris Lattner4a927cb2008-06-28 23:36:30 +0000642 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000643 Arg = Arg->IgnoreParenCasts();
644 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000645
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000646 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000647 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000648 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000649 return;
650 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000651
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000652 llvm::StringRef TypeStr = Str->getString();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000653 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000654
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000655 if (TypeStr == "default")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000656 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000657 else if (TypeStr == "hidden")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000658 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000659 else if (TypeStr == "internal")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000660 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000661 else if (TypeStr == "protected")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000662 type = VisibilityAttr::ProtectedVisibility;
663 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000664 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000665 return;
666 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000667
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000668 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000669}
670
Chris Lattner677a3582009-02-14 08:09:34 +0000671static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
672 Sema &S) {
673 if (Attr.getNumArgs() != 0) {
674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
675 return;
676 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000677
Chris Lattner677a3582009-02-14 08:09:34 +0000678 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
679 if (OCI == 0) {
680 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
681 return;
682 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000683
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000684 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000685}
686
687static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000688 if (Attr.getNumArgs() != 0) {
689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
690 return;
691 }
Chris Lattner677a3582009-02-14 08:09:34 +0000692 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000693 QualType T = TD->getUnderlyingType();
694 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000695 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000696 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
697 return;
698 }
699 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000700 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000701}
702
Mike Stumpd3bb5572009-07-24 19:02:52 +0000703static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000704HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
705 if (Attr.getNumArgs() != 0) {
706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
707 return;
708 }
709
710 if (!isa<FunctionDecl>(D)) {
711 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
712 return;
713 }
714
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000715 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000716}
717
Steve Naroff3405a732008-09-18 16:44:58 +0000718static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000719 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000720 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000721 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000722 return;
723 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000724
Steve Naroff3405a732008-09-18 16:44:58 +0000725 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000726 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000727 return;
728 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000729
Steve Naroff3405a732008-09-18 16:44:58 +0000730 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000731 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000732 type = BlocksAttr::ByRef;
733 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000734 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000735 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000736 return;
737 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000738
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000739 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000740}
741
Anders Carlssonc181b012008-10-05 18:05:59 +0000742static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
743 // check the attribute arguments.
744 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000745 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
746 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000747 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000748 }
749
Anders Carlssonc181b012008-10-05 18:05:59 +0000750 int sentinel = 0;
751 if (Attr.getNumArgs() > 0) {
752 Expr *E = static_cast<Expr *>(Attr.getArg(0));
753 llvm::APSInt Idx(32);
754 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000755 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000756 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000757 return;
758 }
759 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000760
Anders Carlssonc181b012008-10-05 18:05:59 +0000761 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000762 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
763 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000764 return;
765 }
766 }
767
768 int nullPos = 0;
769 if (Attr.getNumArgs() > 1) {
770 Expr *E = static_cast<Expr *>(Attr.getArg(1));
771 llvm::APSInt Idx(32);
772 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000773 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000774 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000775 return;
776 }
777 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000778
Anders Carlssonc181b012008-10-05 18:05:59 +0000779 if (nullPos > 1 || nullPos < 0) {
780 // FIXME: This error message could be improved, it would be nice
781 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000782 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
783 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000784 return;
785 }
786 }
787
788 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000789 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000790 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000791
Chris Lattner9363e312009-03-17 23:03:47 +0000792 if (isa<FunctionNoProtoType>(FT)) {
793 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
794 return;
795 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000796
Chris Lattner9363e312009-03-17 23:03:47 +0000797 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000798 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000799 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000800 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000801 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
802 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000803 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000804 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000805 }
806 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000807 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
808 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000809 ;
810 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000811 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000812 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000813 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000814 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000815 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000816 int m = Ty->isFunctionPointerType() ? 0 : 1;
817 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000818 return;
819 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000820 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000822 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000823 return;
824 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000825 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000826 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000827 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000828 return;
829 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000830 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000831}
832
Chris Lattner237f2752009-02-14 07:37:35 +0000833static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
834 // check the attribute arguments.
835 if (Attr.getNumArgs() != 0) {
836 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
837 return;
838 }
839
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000840 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +0000841 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000842 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000843 return;
844 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000845
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000846 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
847 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
848 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +0000849 return;
850 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000851 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
852 if (MD->getResultType()->isVoidType()) {
853 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
854 << Attr.getName() << 1;
855 return;
856 }
857
Nuno Lopes518e3702009-12-20 23:11:08 +0000858 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000859}
860
861static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000862 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000863 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000865 return;
866 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000867
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000868 /* weak only applies to non-static declarations */
Rafael Espindolac18086a2010-02-23 22:00:30 +0000869 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000870 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
871 dyn_cast<NamedDecl>(D)->getNameAsString();
872 return;
873 }
874
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000875 // TODO: could also be applied to methods?
876 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
877 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000878 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000879 return;
880 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000881
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000882 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000883}
884
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000885static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
886 // check the attribute arguments.
887 if (Attr.getNumArgs() != 0) {
888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
889 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000890 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000891
892 // weak_import only applies to variable & function declarations.
893 bool isDef = false;
894 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
895 isDef = (!VD->hasExternalStorage() || VD->getInit());
896 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000897 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000898 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
899 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000900 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000901 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000902 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000903 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000904 return;
905 }
906
907 // Merge should handle any subsequent violations.
908 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000909 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000910 diag::warn_attribute_weak_import_invalid_on_definition)
911 << "weak_import" << 2 /*variable and function*/;
912 return;
913 }
914
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000915 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000916}
917
Nate Begemanf2758702009-06-26 06:32:41 +0000918static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
919 Sema &S) {
920 // Attribute has 3 arguments.
921 if (Attr.getNumArgs() != 3) {
922 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
923 return;
924 }
925
926 unsigned WGSize[3];
927 for (unsigned i = 0; i < 3; ++i) {
928 Expr *E = static_cast<Expr *>(Attr.getArg(i));
929 llvm::APSInt ArgNum(32);
930 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
931 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
932 << "reqd_work_group_size" << E->getSourceRange();
933 return;
934 }
935 WGSize[i] = (unsigned) ArgNum.getZExtValue();
936 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000937 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000938 WGSize[2]));
939}
940
Chris Lattner237f2752009-02-14 07:37:35 +0000941static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000942 // Attribute has no arguments.
943 if (Attr.getNumArgs() != 1) {
944 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
945 return;
946 }
947
948 // Make sure that there is a string literal as the sections's single
949 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +0000950 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
951 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +0000952 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +0000953 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +0000954 return;
955 }
Mike Stump11289f42009-09-09 15:08:12 +0000956
Chris Lattner30ba6742009-08-10 19:03:04 +0000957 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +0000958 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +0000959 if (!Error.empty()) {
960 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
961 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +0000962 return;
963 }
Mike Stump11289f42009-09-09 15:08:12 +0000964
Chris Lattner20aee9b2010-01-12 20:58:53 +0000965 // This attribute cannot be applied to local variables.
966 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
967 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
968 return;
969 }
970
Ted Kremenek7f4945a2010-02-11 05:28:37 +0000971 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +0000972}
973
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000974
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000975static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000976 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000977 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000979 return;
980 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000981
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000982 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000983}
984
Anders Carlssonb8316282008-10-05 23:32:53 +0000985static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
986 // check the attribute arguments.
987 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000988 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +0000989 return;
990 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000991
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000992 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +0000993}
994
995static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
996 // check the attribute arguments.
997 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000998 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +0000999 return;
1000 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001001
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001002 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001003}
1004
Anders Carlssond277d792009-01-31 01:16:18 +00001005static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001006 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001007 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1008 return;
1009 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001010
Anders Carlssond277d792009-01-31 01:16:18 +00001011 if (Attr.getNumArgs() != 0) {
1012 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1013 return;
1014 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001015
Anders Carlssond277d792009-01-31 01:16:18 +00001016 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001017
Anders Carlssond277d792009-01-31 01:16:18 +00001018 if (!VD || !VD->hasLocalStorage()) {
1019 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1020 return;
1021 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001022
Anders Carlssond277d792009-01-31 01:16:18 +00001023 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +00001024 NamedDecl *CleanupDecl
1025 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1026 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001027 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001028 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001029 Attr.getParameterName();
1030 return;
1031 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001032
Anders Carlssond277d792009-01-31 01:16:18 +00001033 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1034 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001036 Attr.getParameterName();
1037 return;
1038 }
1039
Anders Carlssond277d792009-01-31 01:16:18 +00001040 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001041 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001042 Attr.getParameterName();
1043 return;
1044 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001045
Anders Carlsson723f55d2009-02-07 23:16:50 +00001046 // We're currently more strict than GCC about what function types we accept.
1047 // If this ever proves to be a problem it should be easy to fix.
1048 QualType Ty = S.Context.getPointerType(VD->getType());
1049 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001050 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001051 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001052 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1053 Attr.getParameterName() << ParamTy << Ty;
1054 return;
1055 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001056
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001057 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001058}
1059
Mike Stumpd3bb5572009-07-24 19:02:52 +00001060/// Handle __attribute__((format_arg((idx)))) attribute based on
1061/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1062static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001063 if (Attr.getNumArgs() != 1) {
1064 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1065 return;
1066 }
1067 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1068 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1069 << Attr.getName() << 0 /*function*/;
1070 return;
1071 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001072 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1073 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001074 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1075 unsigned FirstIdx = 1;
1076 // checks for the 2nd argument
1077 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1078 llvm::APSInt Idx(32);
1079 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1080 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1081 << "format" << 2 << IdxExpr->getSourceRange();
1082 return;
1083 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001084
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001085 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1087 << "format" << 2 << IdxExpr->getSourceRange();
1088 return;
1089 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001090
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001091 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001092
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001093 // make sure the format string is really a string
1094 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001095
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001096 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1097 if (not_nsstring_type &&
1098 !isCFStringType(Ty, S.Context) &&
1099 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001100 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001101 // FIXME: Should highlight the actual expression that has the wrong type.
1102 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001103 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001104 << IdxExpr->getSourceRange();
1105 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001106 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001107 Ty = getFunctionOrMethodResultType(d);
1108 if (!isNSStringType(Ty, S.Context) &&
1109 !isCFStringType(Ty, S.Context) &&
1110 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001111 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001112 // FIXME: Should highlight the actual expression that has the wrong type.
1113 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001114 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001115 << IdxExpr->getSourceRange();
1116 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001117 }
1118
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001119 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001120}
1121
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001122enum FormatAttrKind {
1123 CFStringFormat,
1124 NSStringFormat,
1125 StrftimeFormat,
1126 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001127 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001128 InvalidFormat
1129};
1130
1131/// getFormatAttrKind - Map from format attribute names to supported format
1132/// types.
1133static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1134 // Check for formats that get handled specially.
1135 if (Format == "NSString")
1136 return NSStringFormat;
1137 if (Format == "CFString")
1138 return CFStringFormat;
1139 if (Format == "strftime")
1140 return StrftimeFormat;
1141
1142 // Otherwise, check for supported formats.
1143 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1144 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1145 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1146 Format == "zcmn_err")
1147 return SupportedFormat;
1148
Duncan Sandsde4fe352010-03-23 14:44:19 +00001149 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1150 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001151 return IgnoredFormat;
1152
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001153 return InvalidFormat;
1154}
1155
Mike Stumpd3bb5572009-07-24 19:02:52 +00001156/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1157/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001158static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001159
Chris Lattner4a927cb2008-06-28 23:36:30 +00001160 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001162 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001163 return;
1164 }
1165
Chris Lattner4a927cb2008-06-28 23:36:30 +00001166 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001167 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001168 return;
1169 }
1170
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001171 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001173 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001174 return;
1175 }
1176
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001177 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001178 unsigned FirstIdx = 1;
1179
Daniel Dunbar07d07852009-10-18 21:17:35 +00001180 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001181
1182 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001183 if (Format.startswith("__") && Format.endswith("__"))
1184 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001185
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001186 // Check for supported formats.
1187 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001188
1189 if (Kind == IgnoredFormat)
1190 return;
1191
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001192 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001193 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001194 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001195 return;
1196 }
1197
1198 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001199 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001200 llvm::APSInt Idx(32);
1201 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001202 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001203 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001204 return;
1205 }
1206
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001207 // FIXME: We should handle the implicit 'this' parameter in a more generic
1208 // way that can be used for other arguments.
1209 bool HasImplicitThisParam = false;
1210 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1211 if (MD->isInstance()) {
1212 HasImplicitThisParam = true;
1213 NumArgs++;
1214 }
1215 }
Mike Stump11289f42009-09-09 15:08:12 +00001216
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001217 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001218 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001219 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001220 return;
1221 }
1222
1223 // FIXME: Do we need to bounds check?
1224 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001225
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001226 if (HasImplicitThisParam) {
1227 if (ArgIdx == 0) {
1228 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1229 << "a string type" << IdxExpr->getSourceRange();
1230 return;
1231 }
1232 ArgIdx--;
1233 }
Mike Stump11289f42009-09-09 15:08:12 +00001234
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001235 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001236 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001237
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001238 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001239 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001240 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1241 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001242 return;
1243 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001244 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001245 // FIXME: do we need to check if the type is NSString*? What are the
1246 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001247 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001248 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1250 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001251 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001252 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001253 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001254 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001255 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001256 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1257 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001258 return;
1259 }
1260
1261 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001262 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001263 llvm::APSInt FirstArg(32);
1264 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001266 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001267 return;
1268 }
1269
1270 // check if the function is variadic if the 3rd argument non-zero
1271 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001272 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001273 ++NumArgs; // +1 for ...
1274 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001275 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001276 return;
1277 }
1278 }
1279
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001280 // strftime requires FirstArg to be 0 because it doesn't read from any
1281 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001282 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001283 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001284 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1285 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001286 return;
1287 }
1288 // if 0 it disables parameter checking (to use with e.g. va_list)
1289 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001290 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001291 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001292 return;
1293 }
1294
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001295 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001296 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001297}
1298
Chris Lattnera663a0a2008-06-29 00:28:59 +00001299static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1300 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001301 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001302 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001304 return;
1305 }
1306
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001307 // Try to find the underlying union declaration.
1308 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001309 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001310 if (TD && TD->getUnderlyingType()->isUnionType())
1311 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1312 else
1313 RD = dyn_cast<RecordDecl>(d);
1314
1315 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001316 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001317 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001318 return;
1319 }
1320
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001321 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001322 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001323 diag::warn_transparent_union_attribute_not_definition);
1324 return;
1325 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001326
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001327 RecordDecl::field_iterator Field = RD->field_begin(),
1328 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001329 if (Field == FieldEnd) {
1330 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1331 return;
1332 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001333
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001334 FieldDecl *FirstField = *Field;
1335 QualType FirstType = FirstField->getType();
1336 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001337 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001338 diag::warn_transparent_union_attribute_floating);
1339 return;
1340 }
1341
1342 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1343 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1344 for (; Field != FieldEnd; ++Field) {
1345 QualType FieldType = Field->getType();
1346 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1347 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1348 // Warn if we drop the attribute.
1349 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001350 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001351 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001352 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001353 diag::warn_transparent_union_attribute_field_size_align)
1354 << isSize << Field->getDeclName() << FieldBits;
1355 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001356 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001357 diag::note_transparent_union_first_field_size_align)
1358 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001359 return;
1360 }
1361 }
1362
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001363 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001364}
1365
Chris Lattnera663a0a2008-06-29 00:28:59 +00001366static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001367 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001368 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001370 return;
1371 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001372 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1373 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001374
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001375 // Make sure that there is a string literal as the annotation's single
1376 // argument.
1377 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001378 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001379 return;
1380 }
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001381 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001382}
1383
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001384static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001385 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001386 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001387 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001388 return;
1389 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001390
1391 //FIXME: The C++0x version of this attribute has more limited applicabilty
1392 // than GNU's, and should error out when it is used to specify a
1393 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001394
1395 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001396 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001397 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001398 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001399 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001400 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001401 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001403
Chris Lattner4627b742008-06-28 23:50:44 +00001404 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1405 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001406 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001407 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1408 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001409 return;
1410 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001411 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001412 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001413 << alignmentExpr->getSourceRange();
1414 return;
1415 }
1416
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001417 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001418}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001419
Mike Stumpd3bb5572009-07-24 19:02:52 +00001420/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1421/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001422///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001423/// Despite what would be logical, the mode attribute is a decl attribute, not a
1424/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1425/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001426static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001427 // This attribute isn't documented, but glibc uses it. It changes
1428 // the width of an int or unsigned int to the specified size.
1429
1430 // Check that there aren't any arguments
1431 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001433 return;
1434 }
1435
1436 IdentifierInfo *Name = Attr.getParameterName();
1437 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001438 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001439 return;
1440 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001441
Daniel Dunbar07d07852009-10-18 21:17:35 +00001442 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001443
1444 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001445 if (Str.startswith("__") && Str.endswith("__"))
1446 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001447
1448 unsigned DestWidth = 0;
1449 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001450 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001451 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001452 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001453 switch (Str[0]) {
1454 case 'Q': DestWidth = 8; break;
1455 case 'H': DestWidth = 16; break;
1456 case 'S': DestWidth = 32; break;
1457 case 'D': DestWidth = 64; break;
1458 case 'X': DestWidth = 96; break;
1459 case 'T': DestWidth = 128; break;
1460 }
1461 if (Str[1] == 'F') {
1462 IntegerMode = false;
1463 } else if (Str[1] == 'C') {
1464 IntegerMode = false;
1465 ComplexMode = true;
1466 } else if (Str[1] != 'I') {
1467 DestWidth = 0;
1468 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001469 break;
1470 case 4:
1471 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1472 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001473 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001474 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001475 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001476 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001477 break;
1478 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001479 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001480 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001481 break;
1482 }
1483
1484 QualType OldTy;
1485 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1486 OldTy = TD->getUnderlyingType();
1487 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1488 OldTy = VD->getType();
1489 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001490 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1491 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001492 return;
1493 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001494
John McCall9dd450b2009-09-21 23:43:11 +00001495 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001496 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1497 else if (IntegerMode) {
1498 if (!OldTy->isIntegralType())
1499 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1500 } else if (ComplexMode) {
1501 if (!OldTy->isComplexType())
1502 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1503 } else {
1504 if (!OldTy->isFloatingType())
1505 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1506 }
1507
Mike Stump87c57ac2009-05-16 07:39:55 +00001508 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1509 // and friends, at least with glibc.
1510 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1511 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001512 // FIXME: Make sure floating-point mappings are accurate
1513 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001514 QualType NewTy;
1515 switch (DestWidth) {
1516 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001517 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001518 return;
1519 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001520 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001521 return;
1522 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001523 if (!IntegerMode) {
1524 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1525 return;
1526 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001527 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001528 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001529 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001530 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001531 break;
1532 case 16:
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.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001539 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001540 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001541 break;
1542 case 32:
1543 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001544 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001545 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001546 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001547 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001548 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001549 break;
1550 case 64:
1551 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001552 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001553 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00001554 if (S.Context.Target.getLongWidth() == 64)
1555 NewTy = S.Context.LongTy;
1556 else
1557 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001558 else
Chandler Carruth72343702010-01-26 06:39:24 +00001559 if (S.Context.Target.getLongWidth() == 64)
1560 NewTy = S.Context.UnsignedLongTy;
1561 else
1562 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001563 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001564 case 96:
1565 NewTy = S.Context.LongDoubleTy;
1566 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001567 case 128:
1568 if (!IntegerMode) {
1569 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1570 return;
1571 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00001572 if (OldTy->isSignedIntegerType())
1573 NewTy = S.Context.Int128Ty;
1574 else
1575 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001576 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001577 }
1578
Eli Friedman4735374e2009-03-03 06:41:03 +00001579 if (ComplexMode) {
1580 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001581 }
1582
1583 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001584 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1585 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001586 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001587 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001588 cast<ValueDecl>(D)->setType(NewTy);
1589}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001590
Mike Stump3722f582009-08-26 22:31:08 +00001591static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001592 // check the attribute arguments.
1593 if (Attr.getNumArgs() > 0) {
1594 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1595 return;
1596 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001597
Anders Carlsson88097122009-02-19 19:16:48 +00001598 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001600 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001601 return;
1602 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001603
Mike Stump3722f582009-08-26 22:31:08 +00001604 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001605}
1606
Mike Stump3722f582009-08-26 22:31:08 +00001607static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001608 // check the attribute arguments.
1609 if (Attr.getNumArgs() != 0) {
1610 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1611 return;
1612 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001613
Chris Lattner4225e232009-04-14 17:02:11 +00001614 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001616 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001617 return;
1618 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001619
Mike Stump3722f582009-08-26 22:31:08 +00001620 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001621}
1622
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001623static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001624 // check the attribute arguments.
1625 if (Attr.getNumArgs() != 0) {
1626 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1627 return;
1628 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001629
Chris Lattner4225e232009-04-14 17:02:11 +00001630 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1631 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001633 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001634 return;
1635 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001636
Douglas Gregor35b57532009-10-27 21:01:01 +00001637 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001638 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001639 return;
1640 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001641
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001642 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001643}
1644
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001645static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1646 // check the attribute arguments.
1647 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001648 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001649 return;
1650 }
Eli Friedman7044b762009-03-27 21:06:47 +00001651
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001652 if (!isFunctionOrMethod(d)) {
1653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001654 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001655 return;
1656 }
Eli Friedman7044b762009-03-27 21:06:47 +00001657
1658 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1659 llvm::APSInt NumParams(32);
1660 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1662 << "regparm" << NumParamsExpr->getSourceRange();
1663 return;
1664 }
1665
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001666 if (S.Context.Target.getRegParmMax() == 0) {
1667 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001668 << NumParamsExpr->getSourceRange();
1669 return;
1670 }
1671
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001672 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001673 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1674 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001675 return;
1676 }
1677
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001678 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001679}
1680
Alexis Hunt96d5c762009-11-21 08:43:09 +00001681static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1682 // check the attribute arguments.
1683 if (Attr.getNumArgs() != 0) {
1684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1685 return;
1686 }
1687
1688 if (!isa<CXXRecordDecl>(d)
1689 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1690 S.Diag(Attr.getLoc(),
1691 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1692 : diag::warn_attribute_wrong_decl_type)
1693 << Attr.getName() << 7 /*virtual method or class*/;
1694 return;
1695 }
Alexis Hunt54a02542009-11-25 04:20:27 +00001696
1697 // FIXME: Conform to C++0x redeclaration rules.
1698
1699 if (d->getAttr<FinalAttr>()) {
1700 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1701 return;
1702 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001703
1704 d->addAttr(::new (S.Context) FinalAttr());
1705}
1706
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001707//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00001708// C++0x member checking attributes
1709//===----------------------------------------------------------------------===//
1710
1711static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1712 if (Attr.getNumArgs() != 0) {
1713 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1714 return;
1715 }
1716
1717 if (!isa<CXXRecordDecl>(d)) {
1718 S.Diag(Attr.getLoc(),
1719 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1720 : diag::warn_attribute_wrong_decl_type)
1721 << Attr.getName() << 9 /*class*/;
1722 return;
1723 }
1724
1725 if (d->getAttr<BaseCheckAttr>()) {
1726 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1727 return;
1728 }
1729
1730 d->addAttr(::new (S.Context) BaseCheckAttr());
1731}
1732
1733static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1734 if (Attr.getNumArgs() != 0) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1736 return;
1737 }
1738
1739 if (!isa<RecordDecl>(d->getDeclContext())) {
1740 // FIXME: It's not the type that's the problem
1741 S.Diag(Attr.getLoc(),
1742 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1743 : diag::warn_attribute_wrong_decl_type)
1744 << Attr.getName() << 11 /*member*/;
1745 return;
1746 }
1747
1748 // FIXME: Conform to C++0x redeclaration rules.
1749
1750 if (d->getAttr<HidingAttr>()) {
1751 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1752 return;
1753 }
1754
1755 d->addAttr(::new (S.Context) HidingAttr());
1756}
1757
1758static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1759 if (Attr.getNumArgs() != 0) {
1760 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1761 return;
1762 }
1763
1764 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1765 // FIXME: It's not the type that's the problem
1766 S.Diag(Attr.getLoc(),
1767 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1768 : diag::warn_attribute_wrong_decl_type)
1769 << Attr.getName() << 10 /*virtual method*/;
1770 return;
1771 }
1772
1773 // FIXME: Conform to C++0x redeclaration rules.
1774
1775 if (d->getAttr<OverrideAttr>()) {
1776 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1777 return;
1778 }
1779
1780 d->addAttr(::new (S.Context) OverrideAttr());
1781}
1782
1783//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001784// Checker-specific attribute handlers.
1785//===----------------------------------------------------------------------===//
1786
1787static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1788 Sema &S) {
1789
Ted Kremenek3b204e42009-05-13 21:07:32 +00001790 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001791
Ted Kremenek3b204e42009-05-13 21:07:32 +00001792 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1793 RetTy = MD->getResultType();
1794 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1795 RetTy = FD->getResultType();
1796 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001797 SourceLocation L = Attr.getLoc();
1798 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1799 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001800 return;
1801 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001802
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001803 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001804 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001805 SourceLocation L = Attr.getLoc();
1806 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1807 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001808 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001809 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001810
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001811 switch (Attr.getKind()) {
1812 default:
1813 assert(0 && "invalid ownership attribute");
1814 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00001815 case AttributeList::AT_cf_returns_not_retained:
1816 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1817 return;
1818 case AttributeList::AT_ns_returns_not_retained:
1819 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1820 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001821 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001822 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001823 return;
1824 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001825 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001826 return;
1827 };
1828}
1829
Charles Davis163855f2010-02-16 18:27:26 +00001830static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1831 return Attr.getKind() == AttributeList::AT_dllimport ||
1832 Attr.getKind() == AttributeList::AT_dllexport;
1833}
1834
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001835//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001836// Top Level Sema Entry Points
1837//===----------------------------------------------------------------------===//
1838
Sebastian Redlfc24b632008-12-21 19:24:58 +00001839/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001840/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001841/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1842/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001843static void ProcessDeclAttribute(Scope *scope, Decl *D,
1844 const AttributeList &Attr, Sema &S) {
Charles Davis163855f2010-02-16 18:27:26 +00001845 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1846 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00001847 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001848 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001849 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
1850 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001851 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001852 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00001853 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001854 // Ignore these, these are type attributes, handled by
1855 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001856 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001857 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1858 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001859 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001860 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001861 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001862 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001863 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1864 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001865 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001866 HandleDependencyAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001867 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1868 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1869 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001870 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001871 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001872 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001873 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1874 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1875 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1876 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1877 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1878 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1879 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1880 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1881 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1882 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1883 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001884
1885 // Checker-specific.
Ted Kremenekd9c66632010-02-18 00:05:45 +00001886 case AttributeList::AT_ns_returns_not_retained:
1887 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001888 case AttributeList::AT_ns_returns_retained:
1889 case AttributeList::AT_cf_returns_retained:
1890 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1891
Nate Begemanf2758702009-06-26 06:32:41 +00001892 case AttributeList::AT_reqd_wg_size:
1893 HandleReqdWorkGroupSize(D, Attr, S); break;
1894
Alexis Hunt54a02542009-11-25 04:20:27 +00001895 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1896 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001897 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1898 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1899 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001900 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00001901 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1902 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001903 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001904 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001905 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001906 case AttributeList::AT_transparent_union:
1907 HandleTransparentUnionAttr(D, Attr, S);
1908 break;
Chris Lattner677a3582009-02-14 08:09:34 +00001909 case AttributeList::AT_objc_exception:
1910 HandleObjCExceptionAttr(D, Attr, S);
1911 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001912 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001913 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1914 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1915 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1916 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1917 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1918 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1919 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1920 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1921 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001922 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00001923 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00001924 // Just ignore
1925 break;
John McCallab26cfa2010-02-05 21:31:56 +00001926 case AttributeList::AT_stdcall:
1927 case AttributeList::AT_cdecl:
1928 case AttributeList::AT_fastcall:
1929 // These are all treated as type attributes.
1930 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001931 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001932 // Ask target about the attribute.
1933 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1934 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1935 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001936 break;
1937 }
1938}
1939
1940/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1941/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00001942void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001943 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
1944 ProcessDeclAttribute(S, D, *l, *this);
1945 }
1946
1947 // GCC accepts
1948 // static int a9 __attribute__((weakref));
1949 // but that looks really pointless. We reject it.
1950 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
1951 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
1952 dyn_cast<NamedDecl>(D)->getNameAsString();
1953 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001954 }
1955}
1956
Ryan Flynn7d470f32009-07-30 03:15:39 +00001957/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1958/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00001959NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001960 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00001961 NamedDecl *NewD = 0;
1962 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1963 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1964 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00001965 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00001966 if (FD->getQualifier()) {
1967 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
1968 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
1969 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00001970 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1971 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1972 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00001973 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001974 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00001975 if (VD->getQualifier()) {
1976 VarDecl *NewVD = cast<VarDecl>(NewD);
1977 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
1978 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00001979 }
1980 return NewD;
1981}
1982
1983/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1984/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00001985void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00001986 if (W.getUsed()) return; // only do this once
1987 W.setUsed(true);
1988 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1989 IdentifierInfo *NDId = ND->getIdentifier();
1990 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001991 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnere6eab982009-09-08 18:10:11 +00001992 NewD->addAttr(::new (Context) WeakAttr());
1993 WeakTopLevelDecl.push_back(NewD);
1994 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1995 // to insert Decl at TU scope, sorry.
1996 DeclContext *SavedContext = CurContext;
1997 CurContext = Context.getTranslationUnitDecl();
1998 PushOnScopeChains(NewD, S);
1999 CurContext = SavedContext;
2000 } else { // just add weak to existing
2001 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002002 }
2003}
2004
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002005/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2006/// it, apply them to D. This is a bit tricky because PD can have attributes
2007/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002008void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002009 // Handle #pragma weak
2010 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2011 if (ND->hasLinkage()) {
2012 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2013 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002014 // Identifier referenced by #pragma weak before it was declared
2015 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002016 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2017 }
2018 }
2019 }
2020
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002021 // Apply decl attributes from the DeclSpec if present.
2022 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002023 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002024
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002025 // Walk the declarator structure, applying decl attributes that were in a type
2026 // position to the decl itself. This handles cases like:
2027 // int *__attr__(x)** D;
2028 // when X is a decl attribute.
2029 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2030 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002031 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002032
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002033 // Finally, apply any attributes on the decl itself.
2034 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002035 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002036}
John McCall28a6aea2009-11-04 02:18:39 +00002037
2038/// PushParsingDeclaration - Enter a new "scope" of deprecation
2039/// warnings.
2040///
2041/// The state token we use is the start index of this scope
2042/// on the warning stack.
2043Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2044 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002045 return (ParsingDeclStackState) DelayedDiagnostics.size();
2046}
2047
2048void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2049 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2050 ParsingDeclDepth--;
2051
2052 if (DelayedDiagnostics.empty())
2053 return;
2054
2055 unsigned SavedIndex = (unsigned) S;
2056 assert(SavedIndex <= DelayedDiagnostics.size() &&
2057 "saved index is out of bounds");
2058
John McCall1064d7e2010-03-16 05:22:47 +00002059 unsigned E = DelayedDiagnostics.size();
2060
John McCall86121512010-01-27 03:50:35 +00002061 // We only want to actually emit delayed diagnostics when we
2062 // successfully parsed a decl.
2063 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2064 if (D) {
2065 // We really do want to start with 0 here. We get one push for a
2066 // decl spec and another for each declarator; in a decl group like:
2067 // deprecated_typedef foo, *bar, baz();
2068 // only the declarator pops will be passed decls. This is correct;
2069 // we really do need to consider delayed diagnostics from the decl spec
2070 // for each of the different declarations.
John McCall1064d7e2010-03-16 05:22:47 +00002071 for (unsigned I = 0; I != E; ++I) {
John McCall86121512010-01-27 03:50:35 +00002072 if (DelayedDiagnostics[I].Triggered)
2073 continue;
2074
2075 switch (DelayedDiagnostics[I].Kind) {
2076 case DelayedDiagnostic::Deprecation:
2077 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2078 break;
2079
2080 case DelayedDiagnostic::Access:
2081 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2082 break;
2083 }
2084 }
2085 }
2086
John McCall1064d7e2010-03-16 05:22:47 +00002087 // Destroy all the delayed diagnostics we're about to pop off.
2088 for (unsigned I = SavedIndex; I != E; ++I)
2089 DelayedDiagnostics[I].destroy();
2090
John McCall86121512010-01-27 03:50:35 +00002091 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002092}
2093
2094static bool isDeclDeprecated(Decl *D) {
2095 do {
2096 if (D->hasAttr<DeprecatedAttr>())
2097 return true;
2098 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2099 return false;
2100}
2101
John McCall86121512010-01-27 03:50:35 +00002102void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2103 Decl *Ctx) {
2104 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002105 return;
2106
John McCall86121512010-01-27 03:50:35 +00002107 DD.Triggered = true;
2108 Diag(DD.Loc, diag::warn_deprecated)
2109 << DD.DeprecationData.Decl->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002110}
2111
2112void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2113 // Delay if we're currently parsing a declaration.
2114 if (ParsingDeclDepth) {
John McCall86121512010-01-27 03:50:35 +00002115 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall28a6aea2009-11-04 02:18:39 +00002116 return;
2117 }
2118
2119 // Otherwise, don't warn if our current context is deprecated.
2120 if (isDeclDeprecated(cast<Decl>(CurContext)))
2121 return;
2122
2123 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2124}