blob: 681a3b7813ab9921b8c49697914980ea5b87d2f2 [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 Kremenek8e3704d2008-07-15 22:26:48 +0000228static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
229 // 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 Kremenek8e3704d2008-07-15 22:26:48 +0000235 // The IBOutlet attribute only applies to instance variables of Objective-C
236 // classes.
Ted Kremenek2620a062009-02-17 22:20:20 +0000237 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000238 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000239 else
Ted Kremenek2620a062009-02-17 22:20:20 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000241}
242
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000243static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000244 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
245 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000246 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000248 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000249 return;
250 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000251
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000252 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000253
254 // The nonnull attribute only applies to pointers.
255 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000256
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000257 for (AttributeList::arg_iterator I=Attr.arg_begin(),
258 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000259
260
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000261 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000262 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000263 llvm::APSInt ArgNum(32);
264 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
266 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000267 return;
268 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000269
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000270 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000271
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000272 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000274 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000275 return;
276 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000277
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000278 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000279
280 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000281 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000282 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000283 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000284 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
285 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000286 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000287 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000288
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000289 NonNullArgs.push_back(x);
290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000291
292 // If no arguments were specified to __attribute__((nonnull)) then all pointer
293 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000294 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000295 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
296 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000297 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000298 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000299 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000300
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000301 if (NonNullArgs.empty()) {
302 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
303 return;
304 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000305 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000306
307 unsigned* start = &NonNullArgs[0];
308 unsigned size = NonNullArgs.size();
309 std::sort(start, start + size);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000310 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000311}
312
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000313static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000314 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000315 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000317 return;
318 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000319
Chris Lattner4a927cb2008-06-28 23:36:30 +0000320 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000321 Arg = Arg->IgnoreParenCasts();
322 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000323
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000324 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000326 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000327 return;
328 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000329
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000330 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000331
Benjamin Kramer5f089122009-11-30 17:08:26 +0000332 d->addAttr(::new (S.Context) AliasAttr(Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000333}
334
Mike Stumpd3bb5572009-07-24 19:02:52 +0000335static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000336 Sema &S) {
337 // check the attribute arguments.
338 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000340 return;
341 }
Anders Carlsson88097122009-02-19 19:16:48 +0000342
Chris Lattner4225e232009-04-14 17:02:11 +0000343 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000344 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000345 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000346 return;
347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000348
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000349 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000350}
351
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000352static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
353 // check the attribute arguments.
354 if (Attr.getNumArgs() != 0) {
355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
356 return;
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenek08479ae2009-08-15 00:51:46 +0000359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000360 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000361 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
362 d->addAttr(::new (S.Context) MallocAttr());
363 return;
364 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000365 }
366
Ted Kremenek08479ae2009-08-15 00:51:46 +0000367 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000368}
369
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000370static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000371 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000372 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000373 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000375 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000376 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000377
Mike Stump88788fe2009-04-29 19:03:13 +0000378 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
379 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stumpfeb19452009-12-15 03:11:10 +0000380 if (VD == 0 || (!VD->getType()->isBlockPointerType()
381 && !VD->getType()->isFunctionPointerType())) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000382 S.Diag(Attr.getLoc(),
383 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
384 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000385 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000386 return false;
387 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000388 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000389
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000390 return true;
391}
392
393static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000394 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000395 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000396}
397
398static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
399 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000400 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000401 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000402}
403
Alexis Hunt96d5c762009-11-21 08:43:09 +0000404static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
405 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
407 << Attr.getName() << 8; /*function, method, or parameter*/
408 return;
409 }
410 // FIXME: Actually store the attribute on the declaration
411}
412
Ted Kremenek39c59a82008-07-25 04:39:19 +0000413static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
414 // check the attribute arguments.
415 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000416 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000417 return;
418 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000419
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000420 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000421 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000422 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000423 return;
424 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000425
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000426 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000427}
428
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000429static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
430 // check the attribute arguments.
431 if (Attr.getNumArgs() != 0) {
432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
433 return;
434 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000435
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000436 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000437 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000438 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
439 return;
440 }
441 } else if (!isFunctionOrMethod(d)) {
442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000443 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000444 return;
445 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000446
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000447 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000448}
449
Daniel Dunbar032db472008-07-31 22:40:48 +0000450static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
451 // check the attribute arguments.
452 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
454 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000455 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000456 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000457
458 int priority = 65535; // FIXME: Do not hardcode such constants.
459 if (Attr.getNumArgs() > 0) {
460 Expr *E = static_cast<Expr *>(Attr.getArg(0));
461 llvm::APSInt Idx(32);
462 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000464 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000465 return;
466 }
467 priority = Idx.getZExtValue();
468 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000469
Chris Lattner4225e232009-04-14 17:02:11 +0000470 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000472 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000473 return;
474 }
475
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000476 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000477}
478
479static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
480 // check the attribute arguments.
481 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000482 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
483 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000484 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000485 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000486
487 int priority = 65535; // FIXME: Do not hardcode such constants.
488 if (Attr.getNumArgs() > 0) {
489 Expr *E = static_cast<Expr *>(Attr.getArg(0));
490 llvm::APSInt Idx(32);
491 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000492 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000493 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000494 return;
495 }
496 priority = Idx.getZExtValue();
497 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000498
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000499 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000500 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000501 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000502 return;
503 }
504
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000505 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000506}
507
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000508static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000509 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000510 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000512 return;
513 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000514
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000515 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000516}
517
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000518static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
519 // check the attribute arguments.
520 if (Attr.getNumArgs() != 0) {
521 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
522 return;
523 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000524
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000525 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000526}
527
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000528static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000529 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000530 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000531 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000532 return;
533 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000534
Chris Lattner4a927cb2008-06-28 23:36:30 +0000535 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000536 Arg = Arg->IgnoreParenCasts();
537 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000538
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000539 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000541 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000542 return;
543 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000544
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000545 llvm::StringRef TypeStr = Str->getString();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000546 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000547
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000548 if (TypeStr == "default")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000549 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000550 else if (TypeStr == "hidden")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000551 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000552 else if (TypeStr == "internal")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000553 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000554 else if (TypeStr == "protected")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000555 type = VisibilityAttr::ProtectedVisibility;
556 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000557 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000558 return;
559 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000560
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000561 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000562}
563
Chris Lattner677a3582009-02-14 08:09:34 +0000564static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
565 Sema &S) {
566 if (Attr.getNumArgs() != 0) {
567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
568 return;
569 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000570
Chris Lattner677a3582009-02-14 08:09:34 +0000571 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
572 if (OCI == 0) {
573 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
574 return;
575 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000576
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000577 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000578}
579
580static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000581 if (Attr.getNumArgs() != 0) {
582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
583 return;
584 }
Chris Lattner677a3582009-02-14 08:09:34 +0000585 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000586 QualType T = TD->getUnderlyingType();
587 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000588 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000589 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
590 return;
591 }
592 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000593 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000594}
595
Mike Stumpd3bb5572009-07-24 19:02:52 +0000596static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000597HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
598 if (Attr.getNumArgs() != 0) {
599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
600 return;
601 }
602
603 if (!isa<FunctionDecl>(D)) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
605 return;
606 }
607
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000608 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000609}
610
Steve Naroff3405a732008-09-18 16:44:58 +0000611static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000612 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000613 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000614 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000615 return;
616 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000617
Steve Naroff3405a732008-09-18 16:44:58 +0000618 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000620 return;
621 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000622
Steve Naroff3405a732008-09-18 16:44:58 +0000623 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000624 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000625 type = BlocksAttr::ByRef;
626 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000627 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000628 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000629 return;
630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000631
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000632 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000633}
634
Anders Carlssonc181b012008-10-05 18:05:59 +0000635static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
636 // check the attribute arguments.
637 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
639 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000640 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000641 }
642
Anders Carlssonc181b012008-10-05 18:05:59 +0000643 int sentinel = 0;
644 if (Attr.getNumArgs() > 0) {
645 Expr *E = static_cast<Expr *>(Attr.getArg(0));
646 llvm::APSInt Idx(32);
647 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000649 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000650 return;
651 }
652 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000653
Anders Carlssonc181b012008-10-05 18:05:59 +0000654 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000655 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
656 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000657 return;
658 }
659 }
660
661 int nullPos = 0;
662 if (Attr.getNumArgs() > 1) {
663 Expr *E = static_cast<Expr *>(Attr.getArg(1));
664 llvm::APSInt Idx(32);
665 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000667 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000668 return;
669 }
670 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000671
Anders Carlssonc181b012008-10-05 18:05:59 +0000672 if (nullPos > 1 || nullPos < 0) {
673 // FIXME: This error message could be improved, it would be nice
674 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000675 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
676 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000677 return;
678 }
679 }
680
681 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000682 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000683 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000684
Chris Lattner9363e312009-03-17 23:03:47 +0000685 if (isa<FunctionNoProtoType>(FT)) {
686 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
687 return;
688 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000689
Chris Lattner9363e312009-03-17 23:03:47 +0000690 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000691 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000692 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000693 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000694 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
695 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000696 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000697 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000698 }
699 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000700 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
701 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000702 ;
703 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000704 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000705 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000706 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000707 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000708 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000709 int m = Ty->isFunctionPointerType() ? 0 : 1;
710 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000711 return;
712 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000713 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000714 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000715 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000716 return;
717 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000718 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000720 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000721 return;
722 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000723 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000724}
725
Chris Lattner237f2752009-02-14 07:37:35 +0000726static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
727 // check the attribute arguments.
728 if (Attr.getNumArgs() != 0) {
729 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
730 return;
731 }
732
Nuno Lopes518e3702009-12-20 23:11:08 +0000733 if (!isFunctionOrMethod(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +0000734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000735 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000736 return;
737 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000738
Nuno Lopes56abcbd2009-12-22 23:59:52 +0000739 if (getFunctionType(D)->getResultType()->isVoidType()) {
740 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
741 << Attr.getName();
742 return;
743 }
744
Nuno Lopes518e3702009-12-20 23:11:08 +0000745 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000746}
747
748static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000749 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000750 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000752 return;
753 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000754
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000755 /* weak only applies to non-static declarations */
756 bool isStatic = false;
757 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
758 isStatic = VD->getStorageClass() == VarDecl::Static;
759 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
760 isStatic = FD->getStorageClass() == FunctionDecl::Static;
761 }
762 if (isStatic) {
763 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
764 dyn_cast<NamedDecl>(D)->getNameAsString();
765 return;
766 }
767
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000768 // TODO: could also be applied to methods?
769 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000771 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000772 return;
773 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000774
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000775 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000776}
777
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000778static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
779 // check the attribute arguments.
780 if (Attr.getNumArgs() != 0) {
781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
782 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000783 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000784
785 // weak_import only applies to variable & function declarations.
786 bool isDef = false;
787 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
788 isDef = (!VD->hasExternalStorage() || VD->getInit());
789 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000790 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000791 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
792 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000793 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000794 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000795 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000796 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000797 return;
798 }
799
800 // Merge should handle any subsequent violations.
801 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000802 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000803 diag::warn_attribute_weak_import_invalid_on_definition)
804 << "weak_import" << 2 /*variable and function*/;
805 return;
806 }
807
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000808 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000809}
810
Chris Lattner237f2752009-02-14 07:37:35 +0000811static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000812 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000813 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000814 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000815 return;
816 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000817
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000818 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000819 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000820 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000821 return;
822 }
823
Chris Lattner237f2752009-02-14 07:37:35 +0000824 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000825 if (!FD) {
826 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000827 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000828 return;
829 }
830
831 // Currently, the dllimport attribute is ignored for inlined functions.
832 // Warning is emitted.
Douglas Gregor35b57532009-10-27 21:01:01 +0000833 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000834 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
835 return;
836 }
837
838 // The attribute is also overridden by a subsequent declaration as dllexport.
839 // Warning is emitted.
840 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
841 nextAttr = nextAttr->getNext()) {
842 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
844 return;
845 }
846 }
847
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000848 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000849 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
850 return;
851 }
852
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000853 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000854}
855
Chris Lattner237f2752009-02-14 07:37:35 +0000856static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000857 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000858 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000860 return;
861 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000862
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000863 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000864 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000865 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000866 return;
867 }
868
Chris Lattner237f2752009-02-14 07:37:35 +0000869 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000870 if (!FD) {
871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000872 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000873 return;
874 }
875
Mike Stumpd3bb5572009-07-24 19:02:52 +0000876 // Currently, the dllexport attribute is ignored for inlined functions, unless
877 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor35b57532009-10-27 21:01:01 +0000878 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000879 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
881 return;
882 }
883
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000884 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000885}
886
Nate Begemanf2758702009-06-26 06:32:41 +0000887static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
888 Sema &S) {
889 // Attribute has 3 arguments.
890 if (Attr.getNumArgs() != 3) {
891 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
892 return;
893 }
894
895 unsigned WGSize[3];
896 for (unsigned i = 0; i < 3; ++i) {
897 Expr *E = static_cast<Expr *>(Attr.getArg(i));
898 llvm::APSInt ArgNum(32);
899 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
901 << "reqd_work_group_size" << E->getSourceRange();
902 return;
903 }
904 WGSize[i] = (unsigned) ArgNum.getZExtValue();
905 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000906 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000907 WGSize[2]));
908}
909
Chris Lattner237f2752009-02-14 07:37:35 +0000910static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000911 // Attribute has no arguments.
912 if (Attr.getNumArgs() != 1) {
913 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
914 return;
915 }
916
917 // Make sure that there is a string literal as the sections's single
918 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +0000919 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
920 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +0000921 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +0000922 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +0000923 return;
924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattner30ba6742009-08-10 19:03:04 +0000926 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +0000927 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +0000928 if (!Error.empty()) {
929 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
930 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +0000931 return;
932 }
Mike Stump11289f42009-09-09 15:08:12 +0000933
Chris Lattner20aee9b2010-01-12 20:58:53 +0000934 // This attribute cannot be applied to local variables.
935 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
936 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
937 return;
938 }
939
940 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +0000941}
942
Eli Friedmane4310c82009-11-09 18:38:53 +0000943static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
944 // Attribute has no arguments.
945 if (Attr.getNumArgs() != 0) {
946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
947 return;
948 }
949
950 // Attribute can be applied only to functions.
John McCallcddbad02010-02-04 05:44:44 +0000951 // If we try to apply it to a function pointer, don't warn, but don't
952 // do anything, either. All the function-pointer stuff is handled in
953 // SemaType.cpp.
954 ValueDecl *VD = dyn_cast<ValueDecl>(d);
955 if (VD && VD->getType()->isFunctionPointerType())
956 return;
Eli Friedmane4310c82009-11-09 18:38:53 +0000957 if (!isa<FunctionDecl>(d)) {
958 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
959 << Attr.getName() << 0 /*function*/;
960 return;
961 }
962
963 // cdecl and fastcall attributes are mutually incompatible.
964 if (d->getAttr<FastCallAttr>()) {
965 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
966 << "cdecl" << "fastcall";
967 return;
968 }
969
970 // cdecl and stdcall attributes are mutually incompatible.
971 if (d->getAttr<StdCallAttr>()) {
972 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
973 << "cdecl" << "stdcall";
974 return;
975 }
976
977 d->addAttr(::new (S.Context) CDeclAttr());
978}
979
980
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000981static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000982 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000983 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000984 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000985 return;
986 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000987
988 // Attribute can be applied only to functions.
John McCallcddbad02010-02-04 05:44:44 +0000989 // If we try to apply it to a function pointer, don't warn, but don't
Charles Davise4e604b2010-02-05 17:53:51 +0000990 // do anything, either. All the function-pointer stuff is handled in
991 // SemaType.cpp.
John McCallcddbad02010-02-04 05:44:44 +0000992 ValueDecl *VD = dyn_cast<ValueDecl>(d);
993 if (VD && VD->getType()->isFunctionPointerType())
994 return;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000995 if (!isa<FunctionDecl>(d)) {
996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000997 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000998 return;
999 }
1000
1001 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001002 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001003 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1004 << "stdcall" << "fastcall";
1005 return;
1006 }
1007
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001008 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001009}
1010
John McCall4976fd42009-11-04 03:36:09 +00001011/// Diagnose the use of a non-standard calling convention on the given
1012/// function.
1013static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1014 SourceLocation Loc, Sema &S) {
1015 if (!D->hasPrototype()) {
1016 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1017 return;
1018 }
1019
1020 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1021 if (T->isVariadic()) {
1022 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1023 return;
1024 }
1025}
1026
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001027static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001028 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001029 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001030 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001031 return;
1032 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001033
John McCallcddbad02010-02-04 05:44:44 +00001034 // If we try to apply it to a function pointer, don't warn, but don't
Charles Davise4e604b2010-02-05 17:53:51 +00001035 // do anything, either. All the function-pointer stuff is handled in
1036 // SemaType.cpp.
John McCallcddbad02010-02-04 05:44:44 +00001037 ValueDecl *VD = dyn_cast<ValueDecl>(d);
1038 if (VD && VD->getType()->isFunctionPointerType())
1039 return;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001040 if (!isa<FunctionDecl>(d)) {
1041 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001042 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001043 return;
1044 }
1045
John McCall4976fd42009-11-04 03:36:09 +00001046 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1047
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001048 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001049 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001050 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1051 << "fastcall" << "stdcall";
1052 return;
1053 }
1054
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001055 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001056}
1057
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001058static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001059 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001060 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001061 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001062 return;
1063 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001064
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001065 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001066}
1067
Anders Carlssonb8316282008-10-05 23:32:53 +00001068static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1069 // check the attribute arguments.
1070 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001071 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001072 return;
1073 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001074
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001075 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001076}
1077
1078static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1079 // check the attribute arguments.
1080 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001081 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001082 return;
1083 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001084
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001085 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001086}
1087
Anders Carlssond277d792009-01-31 01:16:18 +00001088static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001089 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1091 return;
1092 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001093
Anders Carlssond277d792009-01-31 01:16:18 +00001094 if (Attr.getNumArgs() != 0) {
1095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1096 return;
1097 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001098
Anders Carlssond277d792009-01-31 01:16:18 +00001099 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001100
Anders Carlssond277d792009-01-31 01:16:18 +00001101 if (!VD || !VD->hasLocalStorage()) {
1102 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1103 return;
1104 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001105
Anders Carlssond277d792009-01-31 01:16:18 +00001106 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +00001107 NamedDecl *CleanupDecl
1108 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1109 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001110 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001111 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001112 Attr.getParameterName();
1113 return;
1114 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001115
Anders Carlssond277d792009-01-31 01:16:18 +00001116 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1117 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001118 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001119 Attr.getParameterName();
1120 return;
1121 }
1122
Anders Carlssond277d792009-01-31 01:16:18 +00001123 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001124 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001125 Attr.getParameterName();
1126 return;
1127 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001128
Anders Carlsson723f55d2009-02-07 23:16:50 +00001129 // We're currently more strict than GCC about what function types we accept.
1130 // If this ever proves to be a problem it should be easy to fix.
1131 QualType Ty = S.Context.getPointerType(VD->getType());
1132 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001133 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001134 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001135 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1136 Attr.getParameterName() << ParamTy << Ty;
1137 return;
1138 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001139
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001140 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001141}
1142
Mike Stumpd3bb5572009-07-24 19:02:52 +00001143/// Handle __attribute__((format_arg((idx)))) attribute based on
1144/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1145static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001146 if (Attr.getNumArgs() != 1) {
1147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1148 return;
1149 }
1150 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1151 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1152 << Attr.getName() << 0 /*function*/;
1153 return;
1154 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1156 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001157 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1158 unsigned FirstIdx = 1;
1159 // checks for the 2nd argument
1160 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1161 llvm::APSInt Idx(32);
1162 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1163 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1164 << "format" << 2 << IdxExpr->getSourceRange();
1165 return;
1166 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001167
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001168 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1170 << "format" << 2 << IdxExpr->getSourceRange();
1171 return;
1172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001173
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001174 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001175
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001176 // make sure the format string is really a string
1177 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001178
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001179 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1180 if (not_nsstring_type &&
1181 !isCFStringType(Ty, S.Context) &&
1182 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001183 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001184 // FIXME: Should highlight the actual expression that has the wrong type.
1185 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001186 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001187 << IdxExpr->getSourceRange();
1188 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001189 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001190 Ty = getFunctionOrMethodResultType(d);
1191 if (!isNSStringType(Ty, S.Context) &&
1192 !isCFStringType(Ty, S.Context) &&
1193 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001194 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001195 // FIXME: Should highlight the actual expression that has the wrong type.
1196 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001197 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001198 << IdxExpr->getSourceRange();
1199 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001200 }
1201
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001202 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001203}
1204
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001205enum FormatAttrKind {
1206 CFStringFormat,
1207 NSStringFormat,
1208 StrftimeFormat,
1209 SupportedFormat,
1210 InvalidFormat
1211};
1212
1213/// getFormatAttrKind - Map from format attribute names to supported format
1214/// types.
1215static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1216 // Check for formats that get handled specially.
1217 if (Format == "NSString")
1218 return NSStringFormat;
1219 if (Format == "CFString")
1220 return CFStringFormat;
1221 if (Format == "strftime")
1222 return StrftimeFormat;
1223
1224 // Otherwise, check for supported formats.
1225 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1226 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1227 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1228 Format == "zcmn_err")
1229 return SupportedFormat;
1230
1231 return InvalidFormat;
1232}
1233
Mike Stumpd3bb5572009-07-24 19:02:52 +00001234/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1235/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001236static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001237
Chris Lattner4a927cb2008-06-28 23:36:30 +00001238 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001240 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001241 return;
1242 }
1243
Chris Lattner4a927cb2008-06-28 23:36:30 +00001244 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001246 return;
1247 }
1248
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001249 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001251 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001252 return;
1253 }
1254
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001255 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001256 unsigned FirstIdx = 1;
1257
Daniel Dunbar07d07852009-10-18 21:17:35 +00001258 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001259
1260 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001261 if (Format.startswith("__") && Format.endswith("__"))
1262 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001263
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001264 // Check for supported formats.
1265 FormatAttrKind Kind = getFormatAttrKind(Format);
1266 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001267 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001268 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001269 return;
1270 }
1271
1272 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001273 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001274 llvm::APSInt Idx(32);
1275 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001276 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001277 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001278 return;
1279 }
1280
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001281 // FIXME: We should handle the implicit 'this' parameter in a more generic
1282 // way that can be used for other arguments.
1283 bool HasImplicitThisParam = false;
1284 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1285 if (MD->isInstance()) {
1286 HasImplicitThisParam = true;
1287 NumArgs++;
1288 }
1289 }
Mike Stump11289f42009-09-09 15:08:12 +00001290
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001291 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001293 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001294 return;
1295 }
1296
1297 // FIXME: Do we need to bounds check?
1298 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001299
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001300 if (HasImplicitThisParam) {
1301 if (ArgIdx == 0) {
1302 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1303 << "a string type" << IdxExpr->getSourceRange();
1304 return;
1305 }
1306 ArgIdx--;
1307 }
Mike Stump11289f42009-09-09 15:08:12 +00001308
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001309 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001310 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001311
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001312 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001313 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001314 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1315 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001316 return;
1317 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001318 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001319 // FIXME: do we need to check if the type is NSString*? What are the
1320 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001321 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001322 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001323 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1324 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001325 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001326 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001327 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001328 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001329 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001330 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1331 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001332 return;
1333 }
1334
1335 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001336 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001337 llvm::APSInt FirstArg(32);
1338 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001339 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001340 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001341 return;
1342 }
1343
1344 // check if the function is variadic if the 3rd argument non-zero
1345 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001346 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001347 ++NumArgs; // +1 for ...
1348 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001349 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001350 return;
1351 }
1352 }
1353
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001354 // strftime requires FirstArg to be 0 because it doesn't read from any
1355 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001356 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001357 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001358 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1359 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001360 return;
1361 }
1362 // if 0 it disables parameter checking (to use with e.g. va_list)
1363 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001364 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001365 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001366 return;
1367 }
1368
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001369 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1370 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001371}
1372
Chris Lattnera663a0a2008-06-29 00:28:59 +00001373static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1374 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001375 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001376 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001378 return;
1379 }
1380
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001381 // Try to find the underlying union declaration.
1382 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001383 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001384 if (TD && TD->getUnderlyingType()->isUnionType())
1385 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1386 else
1387 RD = dyn_cast<RecordDecl>(d);
1388
1389 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001390 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001391 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001392 return;
1393 }
1394
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001395 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001396 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001397 diag::warn_transparent_union_attribute_not_definition);
1398 return;
1399 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001400
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001401 RecordDecl::field_iterator Field = RD->field_begin(),
1402 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001403 if (Field == FieldEnd) {
1404 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1405 return;
1406 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001407
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001408 FieldDecl *FirstField = *Field;
1409 QualType FirstType = FirstField->getType();
1410 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001411 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001412 diag::warn_transparent_union_attribute_floating);
1413 return;
1414 }
1415
1416 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1417 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1418 for (; Field != FieldEnd; ++Field) {
1419 QualType FieldType = Field->getType();
1420 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1421 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1422 // Warn if we drop the attribute.
1423 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001424 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001425 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001426 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001427 diag::warn_transparent_union_attribute_field_size_align)
1428 << isSize << Field->getDeclName() << FieldBits;
1429 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001430 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001431 diag::note_transparent_union_first_field_size_align)
1432 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001433 return;
1434 }
1435 }
1436
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001437 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001438}
1439
Chris Lattnera663a0a2008-06-29 00:28:59 +00001440static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001441 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001442 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001444 return;
1445 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001446 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1447 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001448
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001449 // Make sure that there is a string literal as the annotation's single
1450 // argument.
1451 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001452 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001453 return;
1454 }
Benjamin Kramer5f089122009-11-30 17:08:26 +00001455 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001456}
1457
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001458static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001459 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001460 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001462 return;
1463 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001464
1465 //FIXME: The C++0x version of this attribute has more limited applicabilty
1466 // than GNU's, and should error out when it is used to specify a
1467 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001468
1469 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001470 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001471 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001472 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001473 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001474 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001475 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001476 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001477
Chris Lattner4627b742008-06-28 23:50:44 +00001478 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1479 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001480 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1482 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001483 return;
1484 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001485 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001486 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001487 << alignmentExpr->getSourceRange();
1488 return;
1489 }
1490
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001491 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001492}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001493
Mike Stumpd3bb5572009-07-24 19:02:52 +00001494/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1495/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001496///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001497/// Despite what would be logical, the mode attribute is a decl attribute, not a
1498/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1499/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001500static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001501 // This attribute isn't documented, but glibc uses it. It changes
1502 // the width of an int or unsigned int to the specified size.
1503
1504 // Check that there aren't any arguments
1505 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001506 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001507 return;
1508 }
1509
1510 IdentifierInfo *Name = Attr.getParameterName();
1511 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001512 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001513 return;
1514 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001515
Daniel Dunbar07d07852009-10-18 21:17:35 +00001516 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001517
1518 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001519 if (Str.startswith("__") && Str.endswith("__"))
1520 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001521
1522 unsigned DestWidth = 0;
1523 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001524 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001525 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001526 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001527 switch (Str[0]) {
1528 case 'Q': DestWidth = 8; break;
1529 case 'H': DestWidth = 16; break;
1530 case 'S': DestWidth = 32; break;
1531 case 'D': DestWidth = 64; break;
1532 case 'X': DestWidth = 96; break;
1533 case 'T': DestWidth = 128; break;
1534 }
1535 if (Str[1] == 'F') {
1536 IntegerMode = false;
1537 } else if (Str[1] == 'C') {
1538 IntegerMode = false;
1539 ComplexMode = true;
1540 } else if (Str[1] != 'I') {
1541 DestWidth = 0;
1542 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001543 break;
1544 case 4:
1545 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1546 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001547 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001548 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001549 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001550 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001551 break;
1552 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001553 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001554 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001555 break;
1556 }
1557
1558 QualType OldTy;
1559 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1560 OldTy = TD->getUnderlyingType();
1561 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1562 OldTy = VD->getType();
1563 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001564 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1565 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001566 return;
1567 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001568
John McCall9dd450b2009-09-21 23:43:11 +00001569 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001570 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1571 else if (IntegerMode) {
1572 if (!OldTy->isIntegralType())
1573 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1574 } else if (ComplexMode) {
1575 if (!OldTy->isComplexType())
1576 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1577 } else {
1578 if (!OldTy->isFloatingType())
1579 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1580 }
1581
Mike Stump87c57ac2009-05-16 07:39:55 +00001582 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1583 // and friends, at least with glibc.
1584 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1585 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001586 // FIXME: Make sure floating-point mappings are accurate
1587 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001588 QualType NewTy;
1589 switch (DestWidth) {
1590 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001591 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001592 return;
1593 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001594 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001595 return;
1596 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001597 if (!IntegerMode) {
1598 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1599 return;
1600 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001601 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001602 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001603 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001604 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001605 break;
1606 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001607 if (!IntegerMode) {
1608 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1609 return;
1610 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001611 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001612 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001613 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001614 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001615 break;
1616 case 32:
1617 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001618 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001619 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001620 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001621 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001622 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001623 break;
1624 case 64:
1625 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001626 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001627 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00001628 if (S.Context.Target.getLongWidth() == 64)
1629 NewTy = S.Context.LongTy;
1630 else
1631 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001632 else
Chandler Carruth72343702010-01-26 06:39:24 +00001633 if (S.Context.Target.getLongWidth() == 64)
1634 NewTy = S.Context.UnsignedLongTy;
1635 else
1636 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001637 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001638 case 96:
1639 NewTy = S.Context.LongDoubleTy;
1640 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001641 case 128:
1642 if (!IntegerMode) {
1643 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1644 return;
1645 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00001646 if (OldTy->isSignedIntegerType())
1647 NewTy = S.Context.Int128Ty;
1648 else
1649 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001650 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001651 }
1652
Eli Friedman4735374e2009-03-03 06:41:03 +00001653 if (ComplexMode) {
1654 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001655 }
1656
1657 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001658 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1659 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001660 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001661 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001662 cast<ValueDecl>(D)->setType(NewTy);
1663}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001664
Mike Stump3722f582009-08-26 22:31:08 +00001665static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001666 // check the attribute arguments.
1667 if (Attr.getNumArgs() > 0) {
1668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1669 return;
1670 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001671
Anders Carlsson88097122009-02-19 19:16:48 +00001672 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001673 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001674 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001675 return;
1676 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001677
Mike Stump3722f582009-08-26 22:31:08 +00001678 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001679}
1680
Mike Stump3722f582009-08-26 22:31:08 +00001681static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001682 // check the attribute arguments.
1683 if (Attr.getNumArgs() != 0) {
1684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1685 return;
1686 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001687
Chris Lattner4225e232009-04-14 17:02:11 +00001688 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001690 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001691 return;
1692 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001693
Mike Stump3722f582009-08-26 22:31:08 +00001694 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001695}
1696
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001697static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001698 // check the attribute arguments.
1699 if (Attr.getNumArgs() != 0) {
1700 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1701 return;
1702 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001703
Chris Lattner4225e232009-04-14 17:02:11 +00001704 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1705 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001707 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001708 return;
1709 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001710
Douglas Gregor35b57532009-10-27 21:01:01 +00001711 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001712 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001713 return;
1714 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001715
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001716 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001717}
1718
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001719static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1720 // check the attribute arguments.
1721 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001723 return;
1724 }
Eli Friedman7044b762009-03-27 21:06:47 +00001725
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001726 if (!isFunctionOrMethod(d)) {
1727 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001728 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001729 return;
1730 }
Eli Friedman7044b762009-03-27 21:06:47 +00001731
1732 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1733 llvm::APSInt NumParams(32);
1734 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1736 << "regparm" << NumParamsExpr->getSourceRange();
1737 return;
1738 }
1739
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001740 if (S.Context.Target.getRegParmMax() == 0) {
1741 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001742 << NumParamsExpr->getSourceRange();
1743 return;
1744 }
1745
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001746 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001747 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1748 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001749 return;
1750 }
1751
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001752 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001753}
1754
Alexis Hunt96d5c762009-11-21 08:43:09 +00001755static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1756 // check the attribute arguments.
1757 if (Attr.getNumArgs() != 0) {
1758 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1759 return;
1760 }
1761
1762 if (!isa<CXXRecordDecl>(d)
1763 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1764 S.Diag(Attr.getLoc(),
1765 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1766 : diag::warn_attribute_wrong_decl_type)
1767 << Attr.getName() << 7 /*virtual method or class*/;
1768 return;
1769 }
Alexis Hunt54a02542009-11-25 04:20:27 +00001770
1771 // FIXME: Conform to C++0x redeclaration rules.
1772
1773 if (d->getAttr<FinalAttr>()) {
1774 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1775 return;
1776 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001777
1778 d->addAttr(::new (S.Context) FinalAttr());
1779}
1780
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001781//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00001782// C++0x member checking attributes
1783//===----------------------------------------------------------------------===//
1784
1785static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1786 if (Attr.getNumArgs() != 0) {
1787 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1788 return;
1789 }
1790
1791 if (!isa<CXXRecordDecl>(d)) {
1792 S.Diag(Attr.getLoc(),
1793 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1794 : diag::warn_attribute_wrong_decl_type)
1795 << Attr.getName() << 9 /*class*/;
1796 return;
1797 }
1798
1799 if (d->getAttr<BaseCheckAttr>()) {
1800 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1801 return;
1802 }
1803
1804 d->addAttr(::new (S.Context) BaseCheckAttr());
1805}
1806
1807static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1808 if (Attr.getNumArgs() != 0) {
1809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1810 return;
1811 }
1812
1813 if (!isa<RecordDecl>(d->getDeclContext())) {
1814 // FIXME: It's not the type that's the problem
1815 S.Diag(Attr.getLoc(),
1816 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1817 : diag::warn_attribute_wrong_decl_type)
1818 << Attr.getName() << 11 /*member*/;
1819 return;
1820 }
1821
1822 // FIXME: Conform to C++0x redeclaration rules.
1823
1824 if (d->getAttr<HidingAttr>()) {
1825 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1826 return;
1827 }
1828
1829 d->addAttr(::new (S.Context) HidingAttr());
1830}
1831
1832static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1833 if (Attr.getNumArgs() != 0) {
1834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1835 return;
1836 }
1837
1838 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1839 // FIXME: It's not the type that's the problem
1840 S.Diag(Attr.getLoc(),
1841 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1842 : diag::warn_attribute_wrong_decl_type)
1843 << Attr.getName() << 10 /*virtual method*/;
1844 return;
1845 }
1846
1847 // FIXME: Conform to C++0x redeclaration rules.
1848
1849 if (d->getAttr<OverrideAttr>()) {
1850 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1851 return;
1852 }
1853
1854 d->addAttr(::new (S.Context) OverrideAttr());
1855}
1856
1857//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001858// Checker-specific attribute handlers.
1859//===----------------------------------------------------------------------===//
1860
1861static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1862 Sema &S) {
1863
Ted Kremenek3b204e42009-05-13 21:07:32 +00001864 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001865
Ted Kremenek3b204e42009-05-13 21:07:32 +00001866 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1867 RetTy = MD->getResultType();
1868 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1869 RetTy = FD->getResultType();
1870 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001871 SourceLocation L = Attr.getLoc();
1872 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1873 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001874 return;
1875 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001876
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001877 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001878 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001879 SourceLocation L = Attr.getLoc();
1880 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1881 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001882 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001883 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001884
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001885 switch (Attr.getKind()) {
1886 default:
1887 assert(0 && "invalid ownership attribute");
1888 return;
1889 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001890 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001891 return;
1892 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001893 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001894 return;
1895 };
1896}
1897
1898//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001899// Top Level Sema Entry Points
1900//===----------------------------------------------------------------------===//
1901
Sebastian Redlfc24b632008-12-21 19:24:58 +00001902/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001903/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001904/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1905/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001906static void ProcessDeclAttribute(Scope *scope, Decl *D,
1907 const AttributeList &Attr, Sema &S) {
Eli Friedman53339e02009-06-08 23:27:34 +00001908 if (Attr.isDeclspecAttribute())
1909 // FIXME: Try to deal with __declspec attributes!
1910 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001911 switch (Attr.getKind()) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001912 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001913 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001914 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00001915 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001916 // Ignore these, these are type attributes, handled by
1917 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001918 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001919 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1920 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001921 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001922 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001923 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001924 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001925 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1926 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001927 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001928 HandleDependencyAttr (D, Attr, S); break;
1929 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1930 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1931 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1932 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1933 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1934 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001935 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001936 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001937 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001938 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1939 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1940 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1941 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1942 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1943 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1944 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1945 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1946 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1947 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1948 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1949 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001950
1951 // Checker-specific.
1952 case AttributeList::AT_ns_returns_retained:
1953 case AttributeList::AT_cf_returns_retained:
1954 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1955
Nate Begemanf2758702009-06-26 06:32:41 +00001956 case AttributeList::AT_reqd_wg_size:
1957 HandleReqdWorkGroupSize(D, Attr, S); break;
1958
Alexis Hunt54a02542009-11-25 04:20:27 +00001959 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1960 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1961 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1962 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1963 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1964 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001965 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00001966 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1967 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001968 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1969 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001970 case AttributeList::AT_transparent_union:
1971 HandleTransparentUnionAttr(D, Attr, S);
1972 break;
Chris Lattner677a3582009-02-14 08:09:34 +00001973 case AttributeList::AT_objc_exception:
1974 HandleObjCExceptionAttr(D, Attr, S);
1975 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001976 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001977 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1978 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1979 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1980 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1981 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1982 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1983 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1984 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1985 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001986 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00001987 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00001988 // Just ignore
1989 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001990 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001991 // Ask target about the attribute.
1992 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1993 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1994 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001995 break;
1996 }
1997}
1998
1999/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2000/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00002001void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002002 while (AttrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +00002003 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002004 AttrList = AttrList->getNext();
2005 }
2006}
2007
Ryan Flynn7d470f32009-07-30 03:15:39 +00002008/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2009/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002010NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002011 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002012 NamedDecl *NewD = 0;
2013 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2014 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2015 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002016 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002017 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2018 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2019 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002020 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002021 VD->getStorageClass());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002022 }
2023 return NewD;
2024}
2025
2026/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2027/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002028void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002029 if (W.getUsed()) return; // only do this once
2030 W.setUsed(true);
2031 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2032 IdentifierInfo *NDId = ND->getIdentifier();
2033 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
2034 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2035 NewD->addAttr(::new (Context) WeakAttr());
2036 WeakTopLevelDecl.push_back(NewD);
2037 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2038 // to insert Decl at TU scope, sorry.
2039 DeclContext *SavedContext = CurContext;
2040 CurContext = Context.getTranslationUnitDecl();
2041 PushOnScopeChains(NewD, S);
2042 CurContext = SavedContext;
2043 } else { // just add weak to existing
2044 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002045 }
2046}
2047
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002048/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2049/// it, apply them to D. This is a bit tricky because PD can have attributes
2050/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002051void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002052 // Handle #pragma weak
2053 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2054 if (ND->hasLinkage()) {
2055 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2056 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002057 // Identifier referenced by #pragma weak before it was declared
2058 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002059 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2060 }
2061 }
2062 }
2063
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002064 // Apply decl attributes from the DeclSpec if present.
2065 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002066 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002067
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002068 // Walk the declarator structure, applying decl attributes that were in a type
2069 // position to the decl itself. This handles cases like:
2070 // int *__attr__(x)** D;
2071 // when X is a decl attribute.
2072 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2073 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002074 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002075
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002076 // Finally, apply any attributes on the decl itself.
2077 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002078 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002079}
John McCall28a6aea2009-11-04 02:18:39 +00002080
2081/// PushParsingDeclaration - Enter a new "scope" of deprecation
2082/// warnings.
2083///
2084/// The state token we use is the start index of this scope
2085/// on the warning stack.
2086Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2087 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002088 return (ParsingDeclStackState) DelayedDiagnostics.size();
2089}
2090
2091void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2092 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2093 ParsingDeclDepth--;
2094
2095 if (DelayedDiagnostics.empty())
2096 return;
2097
2098 unsigned SavedIndex = (unsigned) S;
2099 assert(SavedIndex <= DelayedDiagnostics.size() &&
2100 "saved index is out of bounds");
2101
2102 // We only want to actually emit delayed diagnostics when we
2103 // successfully parsed a decl.
2104 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2105 if (D) {
2106 // We really do want to start with 0 here. We get one push for a
2107 // decl spec and another for each declarator; in a decl group like:
2108 // deprecated_typedef foo, *bar, baz();
2109 // only the declarator pops will be passed decls. This is correct;
2110 // we really do need to consider delayed diagnostics from the decl spec
2111 // for each of the different declarations.
2112 for (unsigned I = 0, E = DelayedDiagnostics.size(); I != E; ++I) {
2113 if (DelayedDiagnostics[I].Triggered)
2114 continue;
2115
2116 switch (DelayedDiagnostics[I].Kind) {
2117 case DelayedDiagnostic::Deprecation:
2118 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2119 break;
2120
2121 case DelayedDiagnostic::Access:
2122 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2123 break;
2124 }
2125 }
2126 }
2127
2128 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002129}
2130
2131static bool isDeclDeprecated(Decl *D) {
2132 do {
2133 if (D->hasAttr<DeprecatedAttr>())
2134 return true;
2135 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2136 return false;
2137}
2138
John McCall86121512010-01-27 03:50:35 +00002139void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2140 Decl *Ctx) {
2141 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002142 return;
2143
John McCall86121512010-01-27 03:50:35 +00002144 DD.Triggered = true;
2145 Diag(DD.Loc, diag::warn_deprecated)
2146 << DD.DeprecationData.Decl->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002147}
2148
2149void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2150 // Delay if we're currently parsing a declaration.
2151 if (ParsingDeclDepth) {
John McCall86121512010-01-27 03:50:35 +00002152 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall28a6aea2009-11-04 02:18:39 +00002153 return;
2154 }
2155
2156 // Otherwise, don't warn if our current context is deprecated.
2157 if (isDeclDeprecated(cast<Decl>(CurContext)))
2158 return;
2159
2160 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2161}