blob: ef82d53b5c7841c8b1e36aa15078ea57e34c6d91 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremeneka18d7d82009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000039
Chris Lattner6b6b5372008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000044
John McCall183700f2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000046}
47
Daniel Dunbar35682492008-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 Lopesd20254f2009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-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 Dunbard3f2c102008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000062}
63
Fariborz Jahanian620d89c2009-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 Kremeneka18d7d82009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-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 Jahaniand66f22d2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076}
77
Daniel Dunbard3f2c102008-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 Jahanian620d89c2009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000084 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-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 Kremeneka18d7d82009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000106
Chris Lattner89951a82009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000108}
109
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-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 Kremeneka18d7d82009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
121 return BD->IsVariadic();
122 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner6b6b5372008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000131
John McCall183700f2009-09-21 23:43:11 +0000132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000133 if (!ClsT)
134 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000135
Chris Lattner6b6b5372008-06-26 18:38:35 +0000136 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000137
Chris Lattner6b6b5372008-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 Dunbar085e8f72008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Daniel Dunbar085e8f72008-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 Lattnere5c5ee12008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar3068ae02008-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 Stumpbf916502009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000172 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 }
Mike Stumpbf916502009-07-24 19:02:52 +0000174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-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 McCallf7a1a742009-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 Gregor9cdda0c2009-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 Lattner6b6b5372008-06-26 18:38:35 +0000192 }
Douglas Gregor9cdda0c2009-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 McCallba6a9bd2009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204}
205
Chris Lattner803d0802008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-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 Lattner803d0802008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 else
Anders Carlssona860e752009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226}
227
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpbf916502009-07-24 19:02:52 +0000234
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
238 d->addAttr(::new (S.Context) IBActionAttr());
239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000255 d->addAttr(::new (S.Context) IBOutletAttr());
256 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000257 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000260}
261
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000262static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000263 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
264 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000265 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000267 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000268 return;
269 }
Mike Stumpbf916502009-07-24 19:02:52 +0000270
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000271 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000272
273 // The nonnull attribute only applies to pointers.
274 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000275
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000276 for (AttributeList::arg_iterator I=Attr.arg_begin(),
277 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000278
279
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000280 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000281 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000282 llvm::APSInt ArgNum(32);
283 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
285 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000286 return;
287 }
Mike Stumpbf916502009-07-24 19:02:52 +0000288
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000289 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000290
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000291 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000293 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000294 return;
295 }
Mike Stumpbf916502009-07-24 19:02:52 +0000296
Ted Kremenek465172f2008-07-21 22:09:15 +0000297 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000298
299 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000300 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000301 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000302 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000303 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
304 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000305 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000306 }
Mike Stumpbf916502009-07-24 19:02:52 +0000307
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000308 NonNullArgs.push_back(x);
309 }
Mike Stumpbf916502009-07-24 19:02:52 +0000310
311 // If no arguments were specified to __attribute__((nonnull)) then all pointer
312 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000313 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000314 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
315 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000316 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000317 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000318 }
Mike Stumpbf916502009-07-24 19:02:52 +0000319
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000320 if (NonNullArgs.empty()) {
321 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
322 return;
323 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000324 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000325
326 unsigned* start = &NonNullArgs[0];
327 unsigned size = NonNullArgs.size();
328 std::sort(start, start + size);
Ted Kremenek59616112010-02-11 07:31:47 +0000329 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000330}
331
Chris Lattner803d0802008-06-29 00:43:07 +0000332static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000333 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000334 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000335 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000336 return;
337 }
Mike Stumpbf916502009-07-24 19:02:52 +0000338
Chris Lattner545dd342008-06-28 23:36:30 +0000339 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000340 Arg = Arg->IgnoreParenCasts();
341 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000342
Chris Lattner6b6b5372008-06-26 18:38:35 +0000343 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000345 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000346 return;
347 }
Mike Stumpbf916502009-07-24 19:02:52 +0000348
Chris Lattner6b6b5372008-06-26 18:38:35 +0000349 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000350
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000351 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000352}
353
Mike Stumpbf916502009-07-24 19:02:52 +0000354static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000355 Sema &S) {
356 // check the attribute arguments.
357 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000358 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000359 return;
360 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000361
Chris Lattnerc5197432009-04-14 17:02:11 +0000362 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000363 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000364 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000365 return;
366 }
Mike Stumpbf916502009-07-24 19:02:52 +0000367
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000368 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000369}
370
Ryan Flynn76168e22009-08-09 20:07:29 +0000371static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
372 // check the attribute arguments.
373 if (Attr.getNumArgs() != 0) {
374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
375 return;
376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000378 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000379 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000380 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
381 d->addAttr(::new (S.Context) MallocAttr());
382 return;
383 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000384 }
385
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000386 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000387}
388
Ted Kremenekb7252322009-04-10 00:01:14 +0000389static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000390 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000391 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000392 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000393 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000394 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000395 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000396
Mike Stump19c30c02009-04-29 19:03:13 +0000397 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
398 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000399 if (VD == 0 || (!VD->getType()->isBlockPointerType()
400 && !VD->getType()->isFunctionPointerType())) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000401 S.Diag(Attr.getLoc(),
402 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
403 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000404 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000405 return false;
406 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000407 }
Mike Stumpbf916502009-07-24 19:02:52 +0000408
Ted Kremenekb7252322009-04-10 00:01:14 +0000409 return true;
410}
411
412static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
John McCall04a67a62010-02-05 21:31:56 +0000413 // Don't apply as a decl attribute to ValueDecl.
414 // FIXME: probably ought to diagnose this.
415 if (isa<ValueDecl>(d))
416 return;
417
Mike Stumpbf916502009-07-24 19:02:52 +0000418 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000419 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000420}
421
422static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
423 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000424 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000425 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000426}
427
Sean Huntbbd37c62009-11-21 08:43:09 +0000428static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
429 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000431 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000432 return;
433 }
434 // FIXME: Actually store the attribute on the declaration
435}
436
Ted Kremenek73798892008-07-25 04:39:19 +0000437static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
438 // check the attribute arguments.
439 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000440 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000441 return;
442 }
Mike Stumpbf916502009-07-24 19:02:52 +0000443
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000444 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000445 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000446 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000447 return;
448 }
Mike Stumpbf916502009-07-24 19:02:52 +0000449
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000450 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000451}
452
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000453static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
454 // check the attribute arguments.
455 if (Attr.getNumArgs() != 0) {
456 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
457 return;
458 }
Mike Stumpbf916502009-07-24 19:02:52 +0000459
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000460 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000461 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000462 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
463 return;
464 }
465 } else if (!isFunctionOrMethod(d)) {
466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000467 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000468 return;
469 }
Mike Stumpbf916502009-07-24 19:02:52 +0000470
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000471 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000472}
473
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000474static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
475 // check the attribute arguments.
476 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000477 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
478 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000479 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000480 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000481
482 int priority = 65535; // FIXME: Do not hardcode such constants.
483 if (Attr.getNumArgs() > 0) {
484 Expr *E = static_cast<Expr *>(Attr.getArg(0));
485 llvm::APSInt Idx(32);
486 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000487 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000488 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000489 return;
490 }
491 priority = Idx.getZExtValue();
492 }
Mike Stumpbf916502009-07-24 19:02:52 +0000493
Chris Lattnerc5197432009-04-14 17:02:11 +0000494 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000495 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000496 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000497 return;
498 }
499
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000500 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000501}
502
503static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
504 // check the attribute arguments.
505 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000506 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
507 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000508 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000509 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000510
511 int priority = 65535; // FIXME: Do not hardcode such constants.
512 if (Attr.getNumArgs() > 0) {
513 Expr *E = static_cast<Expr *>(Attr.getArg(0));
514 llvm::APSInt Idx(32);
515 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000517 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000518 return;
519 }
520 priority = Idx.getZExtValue();
521 }
Mike Stumpbf916502009-07-24 19:02:52 +0000522
Anders Carlsson6782fc62008-08-22 22:10:48 +0000523 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000524 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000525 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000526 return;
527 }
528
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000529 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000530}
531
Chris Lattner803d0802008-06-29 00:43:07 +0000532static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000533 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000534 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000535 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000536 return;
537 }
Mike Stumpbf916502009-07-24 19:02:52 +0000538
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000539 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000540}
541
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000542static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
543 // check the attribute arguments.
544 if (Attr.getNumArgs() != 0) {
545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
546 return;
547 }
Mike Stumpbf916502009-07-24 19:02:52 +0000548
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000549 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000550}
551
Chris Lattner803d0802008-06-29 00:43:07 +0000552static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000553 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000554 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000555 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000556 return;
557 }
Mike Stumpbf916502009-07-24 19:02:52 +0000558
Chris Lattner545dd342008-06-28 23:36:30 +0000559 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000560 Arg = Arg->IgnoreParenCasts();
561 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000562
Chris Lattner6b6b5372008-06-26 18:38:35 +0000563 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000564 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000565 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000566 return;
567 }
Mike Stumpbf916502009-07-24 19:02:52 +0000568
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000569 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000570 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000571
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000572 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000573 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000574 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000575 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000576 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000577 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000578 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000579 type = VisibilityAttr::ProtectedVisibility;
580 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000581 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000582 return;
583 }
Mike Stumpbf916502009-07-24 19:02:52 +0000584
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000585 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000586}
587
Chris Lattner0db29ec2009-02-14 08:09:34 +0000588static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
589 Sema &S) {
590 if (Attr.getNumArgs() != 0) {
591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
592 return;
593 }
Mike Stumpbf916502009-07-24 19:02:52 +0000594
Chris Lattner0db29ec2009-02-14 08:09:34 +0000595 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
596 if (OCI == 0) {
597 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
598 return;
599 }
Mike Stumpbf916502009-07-24 19:02:52 +0000600
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000601 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000602}
603
604static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000605 if (Attr.getNumArgs() != 0) {
606 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
607 return;
608 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000609 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000610 QualType T = TD->getUnderlyingType();
611 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000612 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000613 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
614 return;
615 }
616 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000617 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000618}
619
Mike Stumpbf916502009-07-24 19:02:52 +0000620static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000621HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
622 if (Attr.getNumArgs() != 0) {
623 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
624 return;
625 }
626
627 if (!isa<FunctionDecl>(D)) {
628 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
629 return;
630 }
631
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000632 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000633}
634
Steve Naroff9eae5762008-09-18 16:44:58 +0000635static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000636 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000637 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000638 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000639 return;
640 }
Mike Stumpbf916502009-07-24 19:02:52 +0000641
Steve Naroff9eae5762008-09-18 16:44:58 +0000642 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000644 return;
645 }
Mike Stumpbf916502009-07-24 19:02:52 +0000646
Steve Naroff9eae5762008-09-18 16:44:58 +0000647 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000648 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000649 type = BlocksAttr::ByRef;
650 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000651 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000652 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000653 return;
654 }
Mike Stumpbf916502009-07-24 19:02:52 +0000655
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000656 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000657}
658
Anders Carlsson77091822008-10-05 18:05:59 +0000659static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
660 // check the attribute arguments.
661 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000662 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
663 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000664 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000665 }
666
Anders Carlsson77091822008-10-05 18:05:59 +0000667 int sentinel = 0;
668 if (Attr.getNumArgs() > 0) {
669 Expr *E = static_cast<Expr *>(Attr.getArg(0));
670 llvm::APSInt Idx(32);
671 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000672 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000673 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000674 return;
675 }
676 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000677
Anders Carlsson77091822008-10-05 18:05:59 +0000678 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000679 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
680 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000681 return;
682 }
683 }
684
685 int nullPos = 0;
686 if (Attr.getNumArgs() > 1) {
687 Expr *E = static_cast<Expr *>(Attr.getArg(1));
688 llvm::APSInt Idx(32);
689 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000691 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000692 return;
693 }
694 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000695
Anders Carlsson77091822008-10-05 18:05:59 +0000696 if (nullPos > 1 || nullPos < 0) {
697 // FIXME: This error message could be improved, it would be nice
698 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000699 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
700 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000701 return;
702 }
703 }
704
705 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000706 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000707 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000708
Chris Lattner897cd902009-03-17 23:03:47 +0000709 if (isa<FunctionNoProtoType>(FT)) {
710 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
711 return;
712 }
Mike Stumpbf916502009-07-24 19:02:52 +0000713
Chris Lattner897cd902009-03-17 23:03:47 +0000714 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000715 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000716 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000717 }
Anders Carlsson77091822008-10-05 18:05:59 +0000718 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
719 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000720 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000721 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000722 }
723 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000724 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
725 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000726 ;
727 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000728 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000729 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000730 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000731 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000732 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000733 int m = Ty->isFunctionPointerType() ? 0 : 1;
734 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000735 return;
736 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000737 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000739 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000740 return;
741 }
Anders Carlsson77091822008-10-05 18:05:59 +0000742 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000743 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000744 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000745 return;
746 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000747 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000748}
749
Chris Lattner026dc962009-02-14 07:37:35 +0000750static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
751 // check the attribute arguments.
752 if (Attr.getNumArgs() != 0) {
753 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
754 return;
755 }
756
Nuno Lopesd20254f2009-12-20 23:11:08 +0000757 if (!isFunctionOrMethod(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000759 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000760 return;
761 }
Mike Stumpbf916502009-07-24 19:02:52 +0000762
Nuno Lopesf8577982009-12-22 23:59:52 +0000763 if (getFunctionType(D)->getResultType()->isVoidType()) {
764 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
765 << Attr.getName();
766 return;
767 }
768
Nuno Lopesd20254f2009-12-20 23:11:08 +0000769 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000770}
771
772static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000773 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000774 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000775 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000776 return;
777 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000778
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000779 /* weak only applies to non-static declarations */
780 bool isStatic = false;
781 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
782 isStatic = VD->getStorageClass() == VarDecl::Static;
783 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
784 isStatic = FD->getStorageClass() == FunctionDecl::Static;
785 }
786 if (isStatic) {
787 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
788 dyn_cast<NamedDecl>(D)->getNameAsString();
789 return;
790 }
791
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000792 // TODO: could also be applied to methods?
793 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
794 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000795 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000796 return;
797 }
Mike Stumpbf916502009-07-24 19:02:52 +0000798
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000799 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000800}
801
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000802static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
803 // check the attribute arguments.
804 if (Attr.getNumArgs() != 0) {
805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
806 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000807 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000808
809 // weak_import only applies to variable & function declarations.
810 bool isDef = false;
811 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
812 isDef = (!VD->hasExternalStorage() || VD->getInit());
813 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000814 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000815 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
816 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000817 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000818 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000820 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000821 return;
822 }
823
824 // Merge should handle any subsequent violations.
825 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000826 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000827 diag::warn_attribute_weak_import_invalid_on_definition)
828 << "weak_import" << 2 /*variable and function*/;
829 return;
830 }
831
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000832 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000833}
834
Nate Begeman6f3d8382009-06-26 06:32:41 +0000835static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
836 Sema &S) {
837 // Attribute has 3 arguments.
838 if (Attr.getNumArgs() != 3) {
839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
840 return;
841 }
842
843 unsigned WGSize[3];
844 for (unsigned i = 0; i < 3; ++i) {
845 Expr *E = static_cast<Expr *>(Attr.getArg(i));
846 llvm::APSInt ArgNum(32);
847 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
848 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
849 << "reqd_work_group_size" << E->getSourceRange();
850 return;
851 }
852 WGSize[i] = (unsigned) ArgNum.getZExtValue();
853 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000854 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000855 WGSize[2]));
856}
857
Chris Lattner026dc962009-02-14 07:37:35 +0000858static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000859 // Attribute has no arguments.
860 if (Attr.getNumArgs() != 1) {
861 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
862 return;
863 }
864
865 // Make sure that there is a string literal as the sections's single
866 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000867 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
868 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000869 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000870 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000871 return;
872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chris Lattner797c3c42009-08-10 19:03:04 +0000874 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000875 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000876 if (!Error.empty()) {
877 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
878 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000879 return;
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000882 // This attribute cannot be applied to local variables.
883 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
884 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
885 return;
886 }
887
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000888 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000889}
890
Chris Lattner6b6b5372008-06-26 18:38:35 +0000891
Chris Lattner803d0802008-06-29 00:43:07 +0000892static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000893 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000894 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000896 return;
897 }
Mike Stumpbf916502009-07-24 19:02:52 +0000898
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000899 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000900}
901
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000902static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
903 // check the attribute arguments.
904 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000905 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000906 return;
907 }
Mike Stumpbf916502009-07-24 19:02:52 +0000908
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000909 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000910}
911
912static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
913 // check the attribute arguments.
914 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000915 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000916 return;
917 }
Mike Stumpbf916502009-07-24 19:02:52 +0000918
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000919 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000920}
921
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000922static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000923 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000924 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
925 return;
926 }
Mike Stumpbf916502009-07-24 19:02:52 +0000927
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000928 if (Attr.getNumArgs() != 0) {
929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
930 return;
931 }
Mike Stumpbf916502009-07-24 19:02:52 +0000932
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000933 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +0000934
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000935 if (!VD || !VD->hasLocalStorage()) {
936 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
937 return;
938 }
Mike Stumpbf916502009-07-24 19:02:52 +0000939
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000940 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +0000941 NamedDecl *CleanupDecl
942 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
943 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000944 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000945 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000946 Attr.getParameterName();
947 return;
948 }
Mike Stumpbf916502009-07-24 19:02:52 +0000949
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000950 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
951 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000952 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000953 Attr.getParameterName();
954 return;
955 }
956
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000957 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000958 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000959 Attr.getParameterName();
960 return;
961 }
Mike Stumpbf916502009-07-24 19:02:52 +0000962
Anders Carlsson89941c12009-02-07 23:16:50 +0000963 // We're currently more strict than GCC about what function types we accept.
964 // If this ever proves to be a problem it should be easy to fix.
965 QualType Ty = S.Context.getPointerType(VD->getType());
966 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +0000967 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +0000968 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +0000969 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
970 Attr.getParameterName() << ParamTy << Ty;
971 return;
972 }
Mike Stumpbf916502009-07-24 19:02:52 +0000973
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000974 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000975}
976
Mike Stumpbf916502009-07-24 19:02:52 +0000977/// Handle __attribute__((format_arg((idx)))) attribute based on
978/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
979static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000980 if (Attr.getNumArgs() != 1) {
981 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
982 return;
983 }
984 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
985 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
986 << Attr.getName() << 0 /*function*/;
987 return;
988 }
Mike Stumpbf916502009-07-24 19:02:52 +0000989 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
990 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000991 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
992 unsigned FirstIdx = 1;
993 // checks for the 2nd argument
994 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
995 llvm::APSInt Idx(32);
996 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
997 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
998 << "format" << 2 << IdxExpr->getSourceRange();
999 return;
1000 }
Mike Stumpbf916502009-07-24 19:02:52 +00001001
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001002 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1003 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1004 << "format" << 2 << IdxExpr->getSourceRange();
1005 return;
1006 }
Mike Stumpbf916502009-07-24 19:02:52 +00001007
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001008 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001009
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001010 // make sure the format string is really a string
1011 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001012
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001013 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1014 if (not_nsstring_type &&
1015 !isCFStringType(Ty, S.Context) &&
1016 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001017 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001018 // FIXME: Should highlight the actual expression that has the wrong type.
1019 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001020 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001021 << IdxExpr->getSourceRange();
1022 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001023 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001024 Ty = getFunctionOrMethodResultType(d);
1025 if (!isNSStringType(Ty, S.Context) &&
1026 !isCFStringType(Ty, S.Context) &&
1027 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001028 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001029 // FIXME: Should highlight the actual expression that has the wrong type.
1030 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001031 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001032 << IdxExpr->getSourceRange();
1033 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001034 }
1035
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001036 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001037}
1038
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001039enum FormatAttrKind {
1040 CFStringFormat,
1041 NSStringFormat,
1042 StrftimeFormat,
1043 SupportedFormat,
1044 InvalidFormat
1045};
1046
1047/// getFormatAttrKind - Map from format attribute names to supported format
1048/// types.
1049static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1050 // Check for formats that get handled specially.
1051 if (Format == "NSString")
1052 return NSStringFormat;
1053 if (Format == "CFString")
1054 return CFStringFormat;
1055 if (Format == "strftime")
1056 return StrftimeFormat;
1057
1058 // Otherwise, check for supported formats.
1059 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1060 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1061 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1062 Format == "zcmn_err")
1063 return SupportedFormat;
1064
1065 return InvalidFormat;
1066}
1067
Mike Stumpbf916502009-07-24 19:02:52 +00001068/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1069/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001070static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001071
Chris Lattner545dd342008-06-28 23:36:30 +00001072 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001073 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001074 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001075 return;
1076 }
1077
Chris Lattner545dd342008-06-28 23:36:30 +00001078 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001079 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001080 return;
1081 }
1082
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001083 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001084 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001085 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001086 return;
1087 }
1088
Daniel Dunbar35682492008-09-26 04:12:28 +00001089 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001090 unsigned FirstIdx = 1;
1091
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001092 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001093
1094 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001095 if (Format.startswith("__") && Format.endswith("__"))
1096 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001097
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001098 // Check for supported formats.
1099 FormatAttrKind Kind = getFormatAttrKind(Format);
1100 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001101 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001102 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001103 return;
1104 }
1105
1106 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001107 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001108 llvm::APSInt Idx(32);
1109 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001110 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001111 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001112 return;
1113 }
1114
Anders Carlsson4fb77202009-08-25 14:12:34 +00001115 // FIXME: We should handle the implicit 'this' parameter in a more generic
1116 // way that can be used for other arguments.
1117 bool HasImplicitThisParam = false;
1118 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1119 if (MD->isInstance()) {
1120 HasImplicitThisParam = true;
1121 NumArgs++;
1122 }
1123 }
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Chris Lattner6b6b5372008-06-26 18:38:35 +00001125 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001126 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001127 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001128 return;
1129 }
1130
1131 // FIXME: Do we need to bounds check?
1132 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001133
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001134 if (HasImplicitThisParam) {
1135 if (ArgIdx == 0) {
1136 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1137 << "a string type" << IdxExpr->getSourceRange();
1138 return;
1139 }
1140 ArgIdx--;
1141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattner6b6b5372008-06-26 18:38:35 +00001143 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001144 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001145
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001146 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001147 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001148 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1149 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001150 return;
1151 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001152 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001153 // FIXME: do we need to check if the type is NSString*? What are the
1154 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001155 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001156 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001157 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1158 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001159 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001160 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001161 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001162 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001163 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001164 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1165 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166 return;
1167 }
1168
1169 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001170 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001171 llvm::APSInt FirstArg(32);
1172 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001173 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001174 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001175 return;
1176 }
1177
1178 // check if the function is variadic if the 3rd argument non-zero
1179 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001180 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001181 ++NumArgs; // +1 for ...
1182 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001183 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001184 return;
1185 }
1186 }
1187
Chris Lattner3c73c412008-11-19 08:23:25 +00001188 // strftime requires FirstArg to be 0 because it doesn't read from any
1189 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001190 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001191 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001192 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1193 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001194 return;
1195 }
1196 // if 0 it disables parameter checking (to use with e.g. va_list)
1197 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001198 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001199 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001200 return;
1201 }
1202
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001203 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001204 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001205}
1206
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001207static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1208 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001209 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001210 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001211 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001212 return;
1213 }
1214
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001215 // Try to find the underlying union declaration.
1216 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001217 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001218 if (TD && TD->getUnderlyingType()->isUnionType())
1219 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1220 else
1221 RD = dyn_cast<RecordDecl>(d);
1222
1223 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001224 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001225 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001226 return;
1227 }
1228
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001229 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001230 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001231 diag::warn_transparent_union_attribute_not_definition);
1232 return;
1233 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001234
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001235 RecordDecl::field_iterator Field = RD->field_begin(),
1236 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001237 if (Field == FieldEnd) {
1238 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1239 return;
1240 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001241
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001242 FieldDecl *FirstField = *Field;
1243 QualType FirstType = FirstField->getType();
1244 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001245 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001246 diag::warn_transparent_union_attribute_floating);
1247 return;
1248 }
1249
1250 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1251 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1252 for (; Field != FieldEnd; ++Field) {
1253 QualType FieldType = Field->getType();
1254 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1255 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1256 // Warn if we drop the attribute.
1257 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001258 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001259 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001260 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001261 diag::warn_transparent_union_attribute_field_size_align)
1262 << isSize << Field->getDeclName() << FieldBits;
1263 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001264 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001265 diag::note_transparent_union_first_field_size_align)
1266 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001267 return;
1268 }
1269 }
1270
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001271 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001272}
1273
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001274static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001275 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001276 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001278 return;
1279 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001280 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1281 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001282
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283 // Make sure that there is a string literal as the annotation's single
1284 // argument.
1285 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001286 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001287 return;
1288 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001289 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001290}
1291
Chris Lattner803d0802008-06-29 00:43:07 +00001292static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001293 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001294 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001295 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001296 return;
1297 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001298
1299 //FIXME: The C++0x version of this attribute has more limited applicabilty
1300 // than GNU's, and should error out when it is used to specify a
1301 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001302
1303 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001304 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001305 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001306 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001307 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001308 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001309 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001310 }
Mike Stumpbf916502009-07-24 19:02:52 +00001311
Chris Lattner49e2d342008-06-28 23:50:44 +00001312 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1313 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001314 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001315 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1316 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001317 return;
1318 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001319 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001320 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001321 << alignmentExpr->getSourceRange();
1322 return;
1323 }
1324
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001325 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001326}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001327
Mike Stumpbf916502009-07-24 19:02:52 +00001328/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1329/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001330///
Mike Stumpbf916502009-07-24 19:02:52 +00001331/// Despite what would be logical, the mode attribute is a decl attribute, not a
1332/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1333/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001334static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001335 // This attribute isn't documented, but glibc uses it. It changes
1336 // the width of an int or unsigned int to the specified size.
1337
1338 // Check that there aren't any arguments
1339 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001341 return;
1342 }
1343
1344 IdentifierInfo *Name = Attr.getParameterName();
1345 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001346 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001347 return;
1348 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001349
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001350 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001351
1352 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001353 if (Str.startswith("__") && Str.endswith("__"))
1354 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001355
1356 unsigned DestWidth = 0;
1357 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001358 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001359 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001360 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001361 switch (Str[0]) {
1362 case 'Q': DestWidth = 8; break;
1363 case 'H': DestWidth = 16; break;
1364 case 'S': DestWidth = 32; break;
1365 case 'D': DestWidth = 64; break;
1366 case 'X': DestWidth = 96; break;
1367 case 'T': DestWidth = 128; break;
1368 }
1369 if (Str[1] == 'F') {
1370 IntegerMode = false;
1371 } else if (Str[1] == 'C') {
1372 IntegerMode = false;
1373 ComplexMode = true;
1374 } else if (Str[1] != 'I') {
1375 DestWidth = 0;
1376 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001377 break;
1378 case 4:
1379 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1380 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001381 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001382 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001383 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001384 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001385 break;
1386 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001387 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001388 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001389 break;
1390 }
1391
1392 QualType OldTy;
1393 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1394 OldTy = TD->getUnderlyingType();
1395 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1396 OldTy = VD->getType();
1397 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001398 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1399 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001400 return;
1401 }
Eli Friedman73397492009-03-03 06:41:03 +00001402
John McCall183700f2009-09-21 23:43:11 +00001403 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001404 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1405 else if (IntegerMode) {
1406 if (!OldTy->isIntegralType())
1407 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1408 } else if (ComplexMode) {
1409 if (!OldTy->isComplexType())
1410 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1411 } else {
1412 if (!OldTy->isFloatingType())
1413 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1414 }
1415
Mike Stump390b4cc2009-05-16 07:39:55 +00001416 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1417 // and friends, at least with glibc.
1418 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1419 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001420 // FIXME: Make sure floating-point mappings are accurate
1421 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001422 QualType NewTy;
1423 switch (DestWidth) {
1424 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001425 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001426 return;
1427 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001428 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001429 return;
1430 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001431 if (!IntegerMode) {
1432 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1433 return;
1434 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001435 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001436 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001437 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001438 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001439 break;
1440 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001441 if (!IntegerMode) {
1442 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1443 return;
1444 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001445 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001446 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001447 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001448 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001449 break;
1450 case 32:
1451 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001452 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001453 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001454 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001455 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001456 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001457 break;
1458 case 64:
1459 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001460 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001461 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001462 if (S.Context.Target.getLongWidth() == 64)
1463 NewTy = S.Context.LongTy;
1464 else
1465 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001466 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001467 if (S.Context.Target.getLongWidth() == 64)
1468 NewTy = S.Context.UnsignedLongTy;
1469 else
1470 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001471 break;
Eli Friedman73397492009-03-03 06:41:03 +00001472 case 96:
1473 NewTy = S.Context.LongDoubleTy;
1474 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001475 case 128:
1476 if (!IntegerMode) {
1477 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1478 return;
1479 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001480 if (OldTy->isSignedIntegerType())
1481 NewTy = S.Context.Int128Ty;
1482 else
1483 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001484 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001485 }
1486
Eli Friedman73397492009-03-03 06:41:03 +00001487 if (ComplexMode) {
1488 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001489 }
1490
1491 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001492 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1493 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001494 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001495 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001496 cast<ValueDecl>(D)->setType(NewTy);
1497}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001498
Mike Stump1feade82009-08-26 22:31:08 +00001499static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001500 // check the attribute arguments.
1501 if (Attr.getNumArgs() > 0) {
1502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1503 return;
1504 }
Anders Carlssone896d982009-02-13 08:11:52 +00001505
Anders Carlsson5bab7882009-02-19 19:16:48 +00001506 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001507 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001508 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001509 return;
1510 }
Mike Stumpbf916502009-07-24 19:02:52 +00001511
Mike Stump1feade82009-08-26 22:31:08 +00001512 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001513}
1514
Mike Stump1feade82009-08-26 22:31:08 +00001515static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001516 // check the attribute arguments.
1517 if (Attr.getNumArgs() != 0) {
1518 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1519 return;
1520 }
Mike Stumpbf916502009-07-24 19:02:52 +00001521
Chris Lattnerc5197432009-04-14 17:02:11 +00001522 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001523 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001524 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001525 return;
1526 }
Mike Stumpbf916502009-07-24 19:02:52 +00001527
Mike Stump1feade82009-08-26 22:31:08 +00001528 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001529}
1530
Chris Lattnercf2a7212009-04-20 19:12:28 +00001531static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001532 // check the attribute arguments.
1533 if (Attr.getNumArgs() != 0) {
1534 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1535 return;
1536 }
Mike Stumpbf916502009-07-24 19:02:52 +00001537
Chris Lattnerc5197432009-04-14 17:02:11 +00001538 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1539 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001540 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001541 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001542 return;
1543 }
Mike Stumpbf916502009-07-24 19:02:52 +00001544
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001545 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001546 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001547 return;
1548 }
Mike Stumpbf916502009-07-24 19:02:52 +00001549
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001550 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001551}
1552
Fariborz Jahanianee760332009-03-27 18:38:55 +00001553static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1554 // check the attribute arguments.
1555 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001556 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001557 return;
1558 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001559
Fariborz Jahanianee760332009-03-27 18:38:55 +00001560 if (!isFunctionOrMethod(d)) {
1561 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001562 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001563 return;
1564 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001565
1566 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1567 llvm::APSInt NumParams(32);
1568 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1570 << "regparm" << NumParamsExpr->getSourceRange();
1571 return;
1572 }
1573
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001574 if (S.Context.Target.getRegParmMax() == 0) {
1575 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001576 << NumParamsExpr->getSourceRange();
1577 return;
1578 }
1579
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001580 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001581 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1582 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001583 return;
1584 }
1585
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001586 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001587}
1588
Sean Huntbbd37c62009-11-21 08:43:09 +00001589static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1590 // check the attribute arguments.
1591 if (Attr.getNumArgs() != 0) {
1592 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1593 return;
1594 }
1595
1596 if (!isa<CXXRecordDecl>(d)
1597 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1598 S.Diag(Attr.getLoc(),
1599 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1600 : diag::warn_attribute_wrong_decl_type)
1601 << Attr.getName() << 7 /*virtual method or class*/;
1602 return;
1603 }
Sean Hunt7725e672009-11-25 04:20:27 +00001604
1605 // FIXME: Conform to C++0x redeclaration rules.
1606
1607 if (d->getAttr<FinalAttr>()) {
1608 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1609 return;
1610 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001611
1612 d->addAttr(::new (S.Context) FinalAttr());
1613}
1614
Chris Lattner0744e5f2008-06-29 00:23:49 +00001615//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001616// C++0x member checking attributes
1617//===----------------------------------------------------------------------===//
1618
1619static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1620 if (Attr.getNumArgs() != 0) {
1621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1622 return;
1623 }
1624
1625 if (!isa<CXXRecordDecl>(d)) {
1626 S.Diag(Attr.getLoc(),
1627 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1628 : diag::warn_attribute_wrong_decl_type)
1629 << Attr.getName() << 9 /*class*/;
1630 return;
1631 }
1632
1633 if (d->getAttr<BaseCheckAttr>()) {
1634 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1635 return;
1636 }
1637
1638 d->addAttr(::new (S.Context) BaseCheckAttr());
1639}
1640
1641static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1642 if (Attr.getNumArgs() != 0) {
1643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1644 return;
1645 }
1646
1647 if (!isa<RecordDecl>(d->getDeclContext())) {
1648 // FIXME: It's not the type that's the problem
1649 S.Diag(Attr.getLoc(),
1650 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1651 : diag::warn_attribute_wrong_decl_type)
1652 << Attr.getName() << 11 /*member*/;
1653 return;
1654 }
1655
1656 // FIXME: Conform to C++0x redeclaration rules.
1657
1658 if (d->getAttr<HidingAttr>()) {
1659 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1660 return;
1661 }
1662
1663 d->addAttr(::new (S.Context) HidingAttr());
1664}
1665
1666static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1667 if (Attr.getNumArgs() != 0) {
1668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1669 return;
1670 }
1671
1672 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1673 // FIXME: It's not the type that's the problem
1674 S.Diag(Attr.getLoc(),
1675 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1676 : diag::warn_attribute_wrong_decl_type)
1677 << Attr.getName() << 10 /*virtual method*/;
1678 return;
1679 }
1680
1681 // FIXME: Conform to C++0x redeclaration rules.
1682
1683 if (d->getAttr<OverrideAttr>()) {
1684 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1685 return;
1686 }
1687
1688 d->addAttr(::new (S.Context) OverrideAttr());
1689}
1690
1691//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001692// Checker-specific attribute handlers.
1693//===----------------------------------------------------------------------===//
1694
1695static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1696 Sema &S) {
1697
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001698 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001699
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001700 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1701 RetTy = MD->getResultType();
1702 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1703 RetTy = FD->getResultType();
1704 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001705 SourceLocation L = Attr.getLoc();
1706 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1707 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001708 return;
1709 }
Mike Stumpbf916502009-07-24 19:02:52 +00001710
Ted Kremenek6217b802009-07-29 21:53:49 +00001711 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001712 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001713 SourceLocation L = Attr.getLoc();
1714 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1715 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001716 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001717 }
Mike Stumpbf916502009-07-24 19:02:52 +00001718
Ted Kremenekb71368d2009-05-09 02:44:38 +00001719 switch (Attr.getKind()) {
1720 default:
1721 assert(0 && "invalid ownership attribute");
1722 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00001723 case AttributeList::AT_cf_returns_not_retained:
1724 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1725 return;
1726 case AttributeList::AT_ns_returns_not_retained:
1727 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1728 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001729 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001730 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001731 return;
1732 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001733 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001734 return;
1735 };
1736}
1737
Charles Davisf0122fe2010-02-16 18:27:26 +00001738static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1739 return Attr.getKind() == AttributeList::AT_dllimport ||
1740 Attr.getKind() == AttributeList::AT_dllexport;
1741}
1742
Ted Kremenekb71368d2009-05-09 02:44:38 +00001743//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001744// Top Level Sema Entry Points
1745//===----------------------------------------------------------------------===//
1746
Sebastian Redla89d82c2008-12-21 19:24:58 +00001747/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001748/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001749/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1750/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001751static void ProcessDeclAttribute(Scope *scope, Decl *D,
1752 const AttributeList &Attr, Sema &S) {
Charles Davisf0122fe2010-02-16 18:27:26 +00001753 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1754 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00001755 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001756 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001757 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
1758 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001759 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001760 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001761 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001762 // Ignore these, these are type attributes, handled by
1763 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001764 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001765 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1766 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001767 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001768 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001769 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001770 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001771 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1772 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001773 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001774 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001775 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1776 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1777 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001778 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001779 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001780 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001781 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1782 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1783 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1784 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1785 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1786 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1787 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1788 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1789 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1790 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1791 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001792
1793 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00001794 case AttributeList::AT_ns_returns_not_retained:
1795 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001796 case AttributeList::AT_ns_returns_retained:
1797 case AttributeList::AT_cf_returns_retained:
1798 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1799
Nate Begeman6f3d8382009-06-26 06:32:41 +00001800 case AttributeList::AT_reqd_wg_size:
1801 HandleReqdWorkGroupSize(D, Attr, S); break;
1802
Sean Hunt7725e672009-11-25 04:20:27 +00001803 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1804 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001805 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1806 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1807 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001808 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001809 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1810 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001811 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1812 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001813 case AttributeList::AT_transparent_union:
1814 HandleTransparentUnionAttr(D, Attr, S);
1815 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001816 case AttributeList::AT_objc_exception:
1817 HandleObjCExceptionAttr(D, Attr, S);
1818 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001819 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001820 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1821 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1822 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1823 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1824 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1825 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1826 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1827 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1828 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001829 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001830 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001831 // Just ignore
1832 break;
John McCall04a67a62010-02-05 21:31:56 +00001833 case AttributeList::AT_stdcall:
1834 case AttributeList::AT_cdecl:
1835 case AttributeList::AT_fastcall:
1836 // These are all treated as type attributes.
1837 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001838 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001839 // Ask target about the attribute.
1840 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1841 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1842 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001843 break;
1844 }
1845}
1846
1847/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1848/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001849void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001850 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001851 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001852 AttrList = AttrList->getNext();
1853 }
1854}
1855
Ryan Flynne25ff832009-07-30 03:15:39 +00001856/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1857/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001858NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001859 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001860 NamedDecl *NewD = 0;
1861 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1862 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1863 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001864 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001865 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1866 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1867 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001868 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001869 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001870 }
1871 return NewD;
1872}
1873
1874/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1875/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001876void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001877 if (W.getUsed()) return; // only do this once
1878 W.setUsed(true);
1879 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1880 IdentifierInfo *NDId = ND->getIdentifier();
1881 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001882 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001883 NewD->addAttr(::new (Context) WeakAttr());
1884 WeakTopLevelDecl.push_back(NewD);
1885 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1886 // to insert Decl at TU scope, sorry.
1887 DeclContext *SavedContext = CurContext;
1888 CurContext = Context.getTranslationUnitDecl();
1889 PushOnScopeChains(NewD, S);
1890 CurContext = SavedContext;
1891 } else { // just add weak to existing
1892 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001893 }
1894}
1895
Chris Lattner0744e5f2008-06-29 00:23:49 +00001896/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1897/// it, apply them to D. This is a bit tricky because PD can have attributes
1898/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001899void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001900 // Handle #pragma weak
1901 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1902 if (ND->hasLinkage()) {
1903 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1904 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001905 // Identifier referenced by #pragma weak before it was declared
1906 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001907 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1908 }
1909 }
1910 }
1911
Chris Lattner0744e5f2008-06-29 00:23:49 +00001912 // Apply decl attributes from the DeclSpec if present.
1913 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001914 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001915
Chris Lattner0744e5f2008-06-29 00:23:49 +00001916 // Walk the declarator structure, applying decl attributes that were in a type
1917 // position to the decl itself. This handles cases like:
1918 // int *__attr__(x)** D;
1919 // when X is a decl attribute.
1920 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1921 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001922 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001923
Chris Lattner0744e5f2008-06-29 00:23:49 +00001924 // Finally, apply any attributes on the decl itself.
1925 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001926 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001927}
John McCall54abf7d2009-11-04 02:18:39 +00001928
1929/// PushParsingDeclaration - Enter a new "scope" of deprecation
1930/// warnings.
1931///
1932/// The state token we use is the start index of this scope
1933/// on the warning stack.
1934Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
1935 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00001936 return (ParsingDeclStackState) DelayedDiagnostics.size();
1937}
1938
1939void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
1940 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
1941 ParsingDeclDepth--;
1942
1943 if (DelayedDiagnostics.empty())
1944 return;
1945
1946 unsigned SavedIndex = (unsigned) S;
1947 assert(SavedIndex <= DelayedDiagnostics.size() &&
1948 "saved index is out of bounds");
1949
1950 // We only want to actually emit delayed diagnostics when we
1951 // successfully parsed a decl.
1952 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
1953 if (D) {
1954 // We really do want to start with 0 here. We get one push for a
1955 // decl spec and another for each declarator; in a decl group like:
1956 // deprecated_typedef foo, *bar, baz();
1957 // only the declarator pops will be passed decls. This is correct;
1958 // we really do need to consider delayed diagnostics from the decl spec
1959 // for each of the different declarations.
1960 for (unsigned I = 0, E = DelayedDiagnostics.size(); I != E; ++I) {
1961 if (DelayedDiagnostics[I].Triggered)
1962 continue;
1963
1964 switch (DelayedDiagnostics[I].Kind) {
1965 case DelayedDiagnostic::Deprecation:
1966 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
1967 break;
1968
1969 case DelayedDiagnostic::Access:
1970 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
1971 break;
1972 }
1973 }
1974 }
1975
1976 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00001977}
1978
1979static bool isDeclDeprecated(Decl *D) {
1980 do {
1981 if (D->hasAttr<DeprecatedAttr>())
1982 return true;
1983 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
1984 return false;
1985}
1986
John McCall2f514482010-01-27 03:50:35 +00001987void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
1988 Decl *Ctx) {
1989 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00001990 return;
1991
John McCall2f514482010-01-27 03:50:35 +00001992 DD.Triggered = true;
1993 Diag(DD.Loc, diag::warn_deprecated)
1994 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00001995}
1996
1997void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
1998 // Delay if we're currently parsing a declaration.
1999 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002000 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002001 return;
2002 }
2003
2004 // Otherwise, don't warn if our current context is deprecated.
2005 if (isDeclDeprecated(cast<Decl>(CurContext)))
2006 return;
2007
2008 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2009}