blob: bb47ae7ea803a79439e4aabfc9e80029b87ab46e [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 Kremenekefbddd22010-02-17 02:37:45 +0000228static void HandleIBAttr(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 Kremenekefbddd22010-02-17 02:37:45 +0000235 // The IBOutlet/IBAction attributes only apply to instance variables of
236 // Objective-C classes.
237 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
238 switch (Attr.getKind()) {
239 case AttributeList::AT_IBAction:
240 d->addAttr(::new (S.Context) IBActionAttr());
241 break;
242 case AttributeList::AT_IBOutlet:
243 d->addAttr(::new (S.Context) IBOutletAttr());
244 break;
245 default:
246 llvm_unreachable("Invalid IB attribute");
247 }
248 }
Ted Kremenek96329d42008-07-15 22:26:48 +0000249 else
Ted Kremenekefbddd22010-02-17 02:37:45 +0000250 S.Diag(Attr.getLoc(), diag::err_attribute_ib) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000251}
252
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000253static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000254 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
255 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000256 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000257 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000258 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000259 return;
260 }
Mike Stumpbf916502009-07-24 19:02:52 +0000261
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000262 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000263
264 // The nonnull attribute only applies to pointers.
265 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000266
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000267 for (AttributeList::arg_iterator I=Attr.arg_begin(),
268 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000269
270
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000271 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000272 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000273 llvm::APSInt ArgNum(32);
274 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
276 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000277 return;
278 }
Mike Stumpbf916502009-07-24 19:02:52 +0000279
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000280 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000281
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000282 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000283 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000284 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000285 return;
286 }
Mike Stumpbf916502009-07-24 19:02:52 +0000287
Ted Kremenek465172f2008-07-21 22:09:15 +0000288 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000289
290 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000291 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000292 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000293 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000294 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
295 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000296 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000297 }
Mike Stumpbf916502009-07-24 19:02:52 +0000298
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000299 NonNullArgs.push_back(x);
300 }
Mike Stumpbf916502009-07-24 19:02:52 +0000301
302 // If no arguments were specified to __attribute__((nonnull)) then all pointer
303 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000304 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000305 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
306 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000307 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000308 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000309 }
Mike Stumpbf916502009-07-24 19:02:52 +0000310
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000311 if (NonNullArgs.empty()) {
312 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
313 return;
314 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000315 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000316
317 unsigned* start = &NonNullArgs[0];
318 unsigned size = NonNullArgs.size();
319 std::sort(start, start + size);
Ted Kremenek59616112010-02-11 07:31:47 +0000320 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000321}
322
Chris Lattner803d0802008-06-29 00:43:07 +0000323static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000324 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000325 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000327 return;
328 }
Mike Stumpbf916502009-07-24 19:02:52 +0000329
Chris Lattner545dd342008-06-28 23:36:30 +0000330 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000331 Arg = Arg->IgnoreParenCasts();
332 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000333
Chris Lattner6b6b5372008-06-26 18:38:35 +0000334 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000335 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000336 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000337 return;
338 }
Mike Stumpbf916502009-07-24 19:02:52 +0000339
Chris Lattner6b6b5372008-06-26 18:38:35 +0000340 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000341
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000342 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000343}
344
Mike Stumpbf916502009-07-24 19:02:52 +0000345static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000346 Sema &S) {
347 // check the attribute arguments.
348 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000350 return;
351 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000352
Chris Lattnerc5197432009-04-14 17:02:11 +0000353 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000355 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000356 return;
357 }
Mike Stumpbf916502009-07-24 19:02:52 +0000358
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000359 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000360}
361
Ryan Flynn76168e22009-08-09 20:07:29 +0000362static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
363 // check the attribute arguments.
364 if (Attr.getNumArgs() != 0) {
365 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
366 return;
367 }
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000369 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000370 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000371 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
372 d->addAttr(::new (S.Context) MallocAttr());
373 return;
374 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000375 }
376
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000377 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000378}
379
Ted Kremenekb7252322009-04-10 00:01:14 +0000380static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000381 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000382 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000383 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000384 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000385 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000386 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000387
Mike Stump19c30c02009-04-29 19:03:13 +0000388 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
389 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000390 if (VD == 0 || (!VD->getType()->isBlockPointerType()
391 && !VD->getType()->isFunctionPointerType())) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000392 S.Diag(Attr.getLoc(),
393 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
394 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000395 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000396 return false;
397 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000398 }
Mike Stumpbf916502009-07-24 19:02:52 +0000399
Ted Kremenekb7252322009-04-10 00:01:14 +0000400 return true;
401}
402
403static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
John McCall04a67a62010-02-05 21:31:56 +0000404 // Don't apply as a decl attribute to ValueDecl.
405 // FIXME: probably ought to diagnose this.
406 if (isa<ValueDecl>(d))
407 return;
408
Mike Stumpbf916502009-07-24 19:02:52 +0000409 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000410 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000411}
412
413static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
414 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000415 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000416 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000417}
418
Sean Huntbbd37c62009-11-21 08:43:09 +0000419static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
420 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000422 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000423 return;
424 }
425 // FIXME: Actually store the attribute on the declaration
426}
427
Ted Kremenek73798892008-07-25 04:39:19 +0000428static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
429 // check the attribute arguments.
430 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000432 return;
433 }
Mike Stumpbf916502009-07-24 19:02:52 +0000434
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000435 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000436 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000437 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000438 return;
439 }
Mike Stumpbf916502009-07-24 19:02:52 +0000440
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000441 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000442}
443
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000444static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
445 // check the attribute arguments.
446 if (Attr.getNumArgs() != 0) {
447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
448 return;
449 }
Mike Stumpbf916502009-07-24 19:02:52 +0000450
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000451 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000452 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000453 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
454 return;
455 }
456 } else if (!isFunctionOrMethod(d)) {
457 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000458 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000459 return;
460 }
Mike Stumpbf916502009-07-24 19:02:52 +0000461
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000462 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000463}
464
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000465static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
466 // check the attribute arguments.
467 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000468 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
469 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000470 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000471 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000472
473 int priority = 65535; // FIXME: Do not hardcode such constants.
474 if (Attr.getNumArgs() > 0) {
475 Expr *E = static_cast<Expr *>(Attr.getArg(0));
476 llvm::APSInt Idx(32);
477 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000478 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000479 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000480 return;
481 }
482 priority = Idx.getZExtValue();
483 }
Mike Stumpbf916502009-07-24 19:02:52 +0000484
Chris Lattnerc5197432009-04-14 17:02:11 +0000485 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000486 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000487 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000488 return;
489 }
490
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000491 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000492}
493
494static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
495 // check the attribute arguments.
496 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000497 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
498 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000499 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000500 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000501
502 int priority = 65535; // FIXME: Do not hardcode such constants.
503 if (Attr.getNumArgs() > 0) {
504 Expr *E = static_cast<Expr *>(Attr.getArg(0));
505 llvm::APSInt Idx(32);
506 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000507 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000508 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000509 return;
510 }
511 priority = Idx.getZExtValue();
512 }
Mike Stumpbf916502009-07-24 19:02:52 +0000513
Anders Carlsson6782fc62008-08-22 22:10:48 +0000514 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000515 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000516 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000517 return;
518 }
519
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000520 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000521}
522
Chris Lattner803d0802008-06-29 00:43:07 +0000523static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000524 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000525 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000527 return;
528 }
Mike Stumpbf916502009-07-24 19:02:52 +0000529
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000530 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000531}
532
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000533static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
534 // check the attribute arguments.
535 if (Attr.getNumArgs() != 0) {
536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
537 return;
538 }
Mike Stumpbf916502009-07-24 19:02:52 +0000539
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000540 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000541}
542
Chris Lattner803d0802008-06-29 00:43:07 +0000543static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000544 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000545 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000547 return;
548 }
Mike Stumpbf916502009-07-24 19:02:52 +0000549
Chris Lattner545dd342008-06-28 23:36:30 +0000550 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000551 Arg = Arg->IgnoreParenCasts();
552 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000553
Chris Lattner6b6b5372008-06-26 18:38:35 +0000554 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000555 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000557 return;
558 }
Mike Stumpbf916502009-07-24 19:02:52 +0000559
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000560 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000561 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000562
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000563 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000564 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000565 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000566 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000567 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000568 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000569 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000570 type = VisibilityAttr::ProtectedVisibility;
571 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000572 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000573 return;
574 }
Mike Stumpbf916502009-07-24 19:02:52 +0000575
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000576 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000577}
578
Chris Lattner0db29ec2009-02-14 08:09:34 +0000579static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
580 Sema &S) {
581 if (Attr.getNumArgs() != 0) {
582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
583 return;
584 }
Mike Stumpbf916502009-07-24 19:02:52 +0000585
Chris Lattner0db29ec2009-02-14 08:09:34 +0000586 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
587 if (OCI == 0) {
588 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
589 return;
590 }
Mike Stumpbf916502009-07-24 19:02:52 +0000591
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000592 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000593}
594
595static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000596 if (Attr.getNumArgs() != 0) {
597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
598 return;
599 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000600 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000601 QualType T = TD->getUnderlyingType();
602 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000603 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000604 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
605 return;
606 }
607 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000608 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000609}
610
Mike Stumpbf916502009-07-24 19:02:52 +0000611static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000612HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
613 if (Attr.getNumArgs() != 0) {
614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
615 return;
616 }
617
618 if (!isa<FunctionDecl>(D)) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
620 return;
621 }
622
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000623 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000624}
625
Steve Naroff9eae5762008-09-18 16:44:58 +0000626static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000627 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000628 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000629 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000630 return;
631 }
Mike Stumpbf916502009-07-24 19:02:52 +0000632
Steve Naroff9eae5762008-09-18 16:44:58 +0000633 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000635 return;
636 }
Mike Stumpbf916502009-07-24 19:02:52 +0000637
Steve Naroff9eae5762008-09-18 16:44:58 +0000638 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000639 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000640 type = BlocksAttr::ByRef;
641 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000642 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000643 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000644 return;
645 }
Mike Stumpbf916502009-07-24 19:02:52 +0000646
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000647 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000648}
649
Anders Carlsson77091822008-10-05 18:05:59 +0000650static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
651 // check the attribute arguments.
652 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
654 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000655 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000656 }
657
Anders Carlsson77091822008-10-05 18:05:59 +0000658 int sentinel = 0;
659 if (Attr.getNumArgs() > 0) {
660 Expr *E = static_cast<Expr *>(Attr.getArg(0));
661 llvm::APSInt Idx(32);
662 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000663 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000664 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000665 return;
666 }
667 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000668
Anders Carlsson77091822008-10-05 18:05:59 +0000669 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000670 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
671 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000672 return;
673 }
674 }
675
676 int nullPos = 0;
677 if (Attr.getNumArgs() > 1) {
678 Expr *E = static_cast<Expr *>(Attr.getArg(1));
679 llvm::APSInt Idx(32);
680 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000681 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000682 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000683 return;
684 }
685 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000686
Anders Carlsson77091822008-10-05 18:05:59 +0000687 if (nullPos > 1 || nullPos < 0) {
688 // FIXME: This error message could be improved, it would be nice
689 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
691 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000692 return;
693 }
694 }
695
696 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000697 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000698 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000699
Chris Lattner897cd902009-03-17 23:03:47 +0000700 if (isa<FunctionNoProtoType>(FT)) {
701 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
702 return;
703 }
Mike Stumpbf916502009-07-24 19:02:52 +0000704
Chris Lattner897cd902009-03-17 23:03:47 +0000705 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000706 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000707 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000708 }
Anders Carlsson77091822008-10-05 18:05:59 +0000709 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
710 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000711 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000712 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000713 }
714 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000715 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
716 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000717 ;
718 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000719 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000720 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000721 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000722 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000723 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000724 int m = Ty->isFunctionPointerType() ? 0 : 1;
725 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000726 return;
727 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000728 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000730 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000731 return;
732 }
Anders Carlsson77091822008-10-05 18:05:59 +0000733 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000735 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000736 return;
737 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000738 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000739}
740
Chris Lattner026dc962009-02-14 07:37:35 +0000741static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
742 // check the attribute arguments.
743 if (Attr.getNumArgs() != 0) {
744 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
745 return;
746 }
747
Nuno Lopesd20254f2009-12-20 23:11:08 +0000748 if (!isFunctionOrMethod(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000750 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000751 return;
752 }
Mike Stumpbf916502009-07-24 19:02:52 +0000753
Nuno Lopesf8577982009-12-22 23:59:52 +0000754 if (getFunctionType(D)->getResultType()->isVoidType()) {
755 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
756 << Attr.getName();
757 return;
758 }
759
Nuno Lopesd20254f2009-12-20 23:11:08 +0000760 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000761}
762
763static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000764 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000765 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000767 return;
768 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000769
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000770 /* weak only applies to non-static declarations */
771 bool isStatic = false;
772 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
773 isStatic = VD->getStorageClass() == VarDecl::Static;
774 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
775 isStatic = FD->getStorageClass() == FunctionDecl::Static;
776 }
777 if (isStatic) {
778 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
779 dyn_cast<NamedDecl>(D)->getNameAsString();
780 return;
781 }
782
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000783 // TODO: could also be applied to methods?
784 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
785 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000786 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000787 return;
788 }
Mike Stumpbf916502009-07-24 19:02:52 +0000789
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000790 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000791}
792
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000793static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
794 // check the attribute arguments.
795 if (Attr.getNumArgs() != 0) {
796 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
797 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000798 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000799
800 // weak_import only applies to variable & function declarations.
801 bool isDef = false;
802 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
803 isDef = (!VD->hasExternalStorage() || VD->getInit());
804 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000805 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000806 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
807 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000808 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000809 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000811 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000812 return;
813 }
814
815 // Merge should handle any subsequent violations.
816 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000817 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000818 diag::warn_attribute_weak_import_invalid_on_definition)
819 << "weak_import" << 2 /*variable and function*/;
820 return;
821 }
822
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000823 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000824}
825
Nate Begeman6f3d8382009-06-26 06:32:41 +0000826static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
827 Sema &S) {
828 // Attribute has 3 arguments.
829 if (Attr.getNumArgs() != 3) {
830 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
831 return;
832 }
833
834 unsigned WGSize[3];
835 for (unsigned i = 0; i < 3; ++i) {
836 Expr *E = static_cast<Expr *>(Attr.getArg(i));
837 llvm::APSInt ArgNum(32);
838 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
839 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
840 << "reqd_work_group_size" << E->getSourceRange();
841 return;
842 }
843 WGSize[i] = (unsigned) ArgNum.getZExtValue();
844 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000845 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000846 WGSize[2]));
847}
848
Chris Lattner026dc962009-02-14 07:37:35 +0000849static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000850 // Attribute has no arguments.
851 if (Attr.getNumArgs() != 1) {
852 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
853 return;
854 }
855
856 // Make sure that there is a string literal as the sections's single
857 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000858 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
859 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000860 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000861 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000862 return;
863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattner797c3c42009-08-10 19:03:04 +0000865 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000866 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000867 if (!Error.empty()) {
868 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
869 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000870 return;
871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000873 // This attribute cannot be applied to local variables.
874 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
875 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
876 return;
877 }
878
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000879 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000880}
881
Chris Lattner6b6b5372008-06-26 18:38:35 +0000882
Chris Lattner803d0802008-06-29 00:43:07 +0000883static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000884 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000885 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000887 return;
888 }
Mike Stumpbf916502009-07-24 19:02:52 +0000889
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000890 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000891}
892
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000893static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
894 // check the attribute arguments.
895 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000897 return;
898 }
Mike Stumpbf916502009-07-24 19:02:52 +0000899
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000900 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000901}
902
903static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
904 // check the attribute arguments.
905 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000907 return;
908 }
Mike Stumpbf916502009-07-24 19:02:52 +0000909
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000910 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000911}
912
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000913static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000914 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000915 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
916 return;
917 }
Mike Stumpbf916502009-07-24 19:02:52 +0000918
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000919 if (Attr.getNumArgs() != 0) {
920 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
921 return;
922 }
Mike Stumpbf916502009-07-24 19:02:52 +0000923
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000924 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +0000925
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000926 if (!VD || !VD->hasLocalStorage()) {
927 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
928 return;
929 }
Mike Stumpbf916502009-07-24 19:02:52 +0000930
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000931 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +0000932 NamedDecl *CleanupDecl
933 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
934 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000935 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000936 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000937 Attr.getParameterName();
938 return;
939 }
Mike Stumpbf916502009-07-24 19:02:52 +0000940
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000941 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
942 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000943 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000944 Attr.getParameterName();
945 return;
946 }
947
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000948 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000949 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000950 Attr.getParameterName();
951 return;
952 }
Mike Stumpbf916502009-07-24 19:02:52 +0000953
Anders Carlsson89941c12009-02-07 23:16:50 +0000954 // We're currently more strict than GCC about what function types we accept.
955 // If this ever proves to be a problem it should be easy to fix.
956 QualType Ty = S.Context.getPointerType(VD->getType());
957 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +0000958 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +0000959 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +0000960 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
961 Attr.getParameterName() << ParamTy << Ty;
962 return;
963 }
Mike Stumpbf916502009-07-24 19:02:52 +0000964
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000965 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000966}
967
Mike Stumpbf916502009-07-24 19:02:52 +0000968/// Handle __attribute__((format_arg((idx)))) attribute based on
969/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
970static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000971 if (Attr.getNumArgs() != 1) {
972 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
973 return;
974 }
975 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
976 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
977 << Attr.getName() << 0 /*function*/;
978 return;
979 }
Mike Stumpbf916502009-07-24 19:02:52 +0000980 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
981 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000982 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
983 unsigned FirstIdx = 1;
984 // checks for the 2nd argument
985 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
986 llvm::APSInt Idx(32);
987 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
988 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
989 << "format" << 2 << IdxExpr->getSourceRange();
990 return;
991 }
Mike Stumpbf916502009-07-24 19:02:52 +0000992
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000993 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
994 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
995 << "format" << 2 << IdxExpr->getSourceRange();
996 return;
997 }
Mike Stumpbf916502009-07-24 19:02:52 +0000998
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000999 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001000
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001001 // make sure the format string is really a string
1002 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001003
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001004 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1005 if (not_nsstring_type &&
1006 !isCFStringType(Ty, S.Context) &&
1007 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001008 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001009 // FIXME: Should highlight the actual expression that has the wrong type.
1010 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001011 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001012 << IdxExpr->getSourceRange();
1013 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001014 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001015 Ty = getFunctionOrMethodResultType(d);
1016 if (!isNSStringType(Ty, S.Context) &&
1017 !isCFStringType(Ty, S.Context) &&
1018 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001019 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001020 // FIXME: Should highlight the actual expression that has the wrong type.
1021 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001022 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001023 << IdxExpr->getSourceRange();
1024 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001025 }
1026
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001027 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001028}
1029
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001030enum FormatAttrKind {
1031 CFStringFormat,
1032 NSStringFormat,
1033 StrftimeFormat,
1034 SupportedFormat,
1035 InvalidFormat
1036};
1037
1038/// getFormatAttrKind - Map from format attribute names to supported format
1039/// types.
1040static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1041 // Check for formats that get handled specially.
1042 if (Format == "NSString")
1043 return NSStringFormat;
1044 if (Format == "CFString")
1045 return CFStringFormat;
1046 if (Format == "strftime")
1047 return StrftimeFormat;
1048
1049 // Otherwise, check for supported formats.
1050 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1051 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1052 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1053 Format == "zcmn_err")
1054 return SupportedFormat;
1055
1056 return InvalidFormat;
1057}
1058
Mike Stumpbf916502009-07-24 19:02:52 +00001059/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1060/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001061static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001062
Chris Lattner545dd342008-06-28 23:36:30 +00001063 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001065 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001066 return;
1067 }
1068
Chris Lattner545dd342008-06-28 23:36:30 +00001069 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001071 return;
1072 }
1073
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001074 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001076 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001077 return;
1078 }
1079
Daniel Dunbar35682492008-09-26 04:12:28 +00001080 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001081 unsigned FirstIdx = 1;
1082
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001083 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001084
1085 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001086 if (Format.startswith("__") && Format.endswith("__"))
1087 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001088
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001089 // Check for supported formats.
1090 FormatAttrKind Kind = getFormatAttrKind(Format);
1091 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001092 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001093 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001094 return;
1095 }
1096
1097 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001098 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001099 llvm::APSInt Idx(32);
1100 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001102 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001103 return;
1104 }
1105
Anders Carlsson4fb77202009-08-25 14:12:34 +00001106 // FIXME: We should handle the implicit 'this' parameter in a more generic
1107 // way that can be used for other arguments.
1108 bool HasImplicitThisParam = false;
1109 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1110 if (MD->isInstance()) {
1111 HasImplicitThisParam = true;
1112 NumArgs++;
1113 }
1114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Chris Lattner6b6b5372008-06-26 18:38:35 +00001116 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001118 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001119 return;
1120 }
1121
1122 // FIXME: Do we need to bounds check?
1123 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001124
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001125 if (HasImplicitThisParam) {
1126 if (ArgIdx == 0) {
1127 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1128 << "a string type" << IdxExpr->getSourceRange();
1129 return;
1130 }
1131 ArgIdx--;
1132 }
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Chris Lattner6b6b5372008-06-26 18:38:35 +00001134 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001135 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001136
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001137 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001138 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001139 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1140 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001141 return;
1142 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001143 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001144 // FIXME: do we need to check if the type is NSString*? What are the
1145 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001146 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001147 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001148 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1149 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001150 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001151 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001152 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001153 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001154 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001155 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1156 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001157 return;
1158 }
1159
1160 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001161 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001162 llvm::APSInt FirstArg(32);
1163 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001164 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001165 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166 return;
1167 }
1168
1169 // check if the function is variadic if the 3rd argument non-zero
1170 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001171 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001172 ++NumArgs; // +1 for ...
1173 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001174 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001175 return;
1176 }
1177 }
1178
Chris Lattner3c73c412008-11-19 08:23:25 +00001179 // strftime requires FirstArg to be 0 because it doesn't read from any
1180 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001181 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001182 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001183 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1184 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001185 return;
1186 }
1187 // if 0 it disables parameter checking (to use with e.g. va_list)
1188 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001190 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001191 return;
1192 }
1193
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001194 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001195 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001196}
1197
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001198static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1199 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001200 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001201 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001203 return;
1204 }
1205
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001206 // Try to find the underlying union declaration.
1207 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001208 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001209 if (TD && TD->getUnderlyingType()->isUnionType())
1210 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1211 else
1212 RD = dyn_cast<RecordDecl>(d);
1213
1214 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001215 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001216 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001217 return;
1218 }
1219
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001220 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001221 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001222 diag::warn_transparent_union_attribute_not_definition);
1223 return;
1224 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001225
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001226 RecordDecl::field_iterator Field = RD->field_begin(),
1227 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001228 if (Field == FieldEnd) {
1229 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1230 return;
1231 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001232
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001233 FieldDecl *FirstField = *Field;
1234 QualType FirstType = FirstField->getType();
1235 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001236 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001237 diag::warn_transparent_union_attribute_floating);
1238 return;
1239 }
1240
1241 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1242 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1243 for (; Field != FieldEnd; ++Field) {
1244 QualType FieldType = Field->getType();
1245 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1246 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1247 // Warn if we drop the attribute.
1248 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001249 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001250 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001251 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001252 diag::warn_transparent_union_attribute_field_size_align)
1253 << isSize << Field->getDeclName() << FieldBits;
1254 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001255 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001256 diag::note_transparent_union_first_field_size_align)
1257 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001258 return;
1259 }
1260 }
1261
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001262 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263}
1264
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001265static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001266 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001267 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001269 return;
1270 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001271 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1272 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001273
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274 // Make sure that there is a string literal as the annotation's single
1275 // argument.
1276 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001277 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001278 return;
1279 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001280 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001281}
1282
Chris Lattner803d0802008-06-29 00:43:07 +00001283static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001284 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001285 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001287 return;
1288 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001289
1290 //FIXME: The C++0x version of this attribute has more limited applicabilty
1291 // than GNU's, and should error out when it is used to specify a
1292 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001293
1294 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001295 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001296 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001297 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001298 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001299 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001300 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001301 }
Mike Stumpbf916502009-07-24 19:02:52 +00001302
Chris Lattner49e2d342008-06-28 23:50:44 +00001303 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1304 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001305 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001306 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1307 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001308 return;
1309 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001310 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001312 << alignmentExpr->getSourceRange();
1313 return;
1314 }
1315
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001316 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001317}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001318
Mike Stumpbf916502009-07-24 19:02:52 +00001319/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1320/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001321///
Mike Stumpbf916502009-07-24 19:02:52 +00001322/// Despite what would be logical, the mode attribute is a decl attribute, not a
1323/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1324/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001325static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001326 // This attribute isn't documented, but glibc uses it. It changes
1327 // the width of an int or unsigned int to the specified size.
1328
1329 // Check that there aren't any arguments
1330 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001331 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001332 return;
1333 }
1334
1335 IdentifierInfo *Name = Attr.getParameterName();
1336 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001337 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001338 return;
1339 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001340
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001341 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001342
1343 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001344 if (Str.startswith("__") && Str.endswith("__"))
1345 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001346
1347 unsigned DestWidth = 0;
1348 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001349 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001350 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001351 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001352 switch (Str[0]) {
1353 case 'Q': DestWidth = 8; break;
1354 case 'H': DestWidth = 16; break;
1355 case 'S': DestWidth = 32; break;
1356 case 'D': DestWidth = 64; break;
1357 case 'X': DestWidth = 96; break;
1358 case 'T': DestWidth = 128; break;
1359 }
1360 if (Str[1] == 'F') {
1361 IntegerMode = false;
1362 } else if (Str[1] == 'C') {
1363 IntegerMode = false;
1364 ComplexMode = true;
1365 } else if (Str[1] != 'I') {
1366 DestWidth = 0;
1367 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001368 break;
1369 case 4:
1370 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1371 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001372 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001373 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001374 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001375 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001376 break;
1377 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001378 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001379 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001380 break;
1381 }
1382
1383 QualType OldTy;
1384 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1385 OldTy = TD->getUnderlyingType();
1386 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1387 OldTy = VD->getType();
1388 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001389 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1390 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001391 return;
1392 }
Eli Friedman73397492009-03-03 06:41:03 +00001393
John McCall183700f2009-09-21 23:43:11 +00001394 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001395 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1396 else if (IntegerMode) {
1397 if (!OldTy->isIntegralType())
1398 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1399 } else if (ComplexMode) {
1400 if (!OldTy->isComplexType())
1401 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1402 } else {
1403 if (!OldTy->isFloatingType())
1404 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1405 }
1406
Mike Stump390b4cc2009-05-16 07:39:55 +00001407 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1408 // and friends, at least with glibc.
1409 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1410 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001411 // FIXME: Make sure floating-point mappings are accurate
1412 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001413 QualType NewTy;
1414 switch (DestWidth) {
1415 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001416 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001417 return;
1418 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001419 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001420 return;
1421 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001422 if (!IntegerMode) {
1423 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1424 return;
1425 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001426 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001427 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001428 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001429 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001430 break;
1431 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001432 if (!IntegerMode) {
1433 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1434 return;
1435 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001436 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001437 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001438 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001439 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001440 break;
1441 case 32:
1442 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001443 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001444 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001445 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001446 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001447 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001448 break;
1449 case 64:
1450 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001451 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001452 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001453 if (S.Context.Target.getLongWidth() == 64)
1454 NewTy = S.Context.LongTy;
1455 else
1456 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001457 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001458 if (S.Context.Target.getLongWidth() == 64)
1459 NewTy = S.Context.UnsignedLongTy;
1460 else
1461 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001462 break;
Eli Friedman73397492009-03-03 06:41:03 +00001463 case 96:
1464 NewTy = S.Context.LongDoubleTy;
1465 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001466 case 128:
1467 if (!IntegerMode) {
1468 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1469 return;
1470 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001471 if (OldTy->isSignedIntegerType())
1472 NewTy = S.Context.Int128Ty;
1473 else
1474 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001475 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001476 }
1477
Eli Friedman73397492009-03-03 06:41:03 +00001478 if (ComplexMode) {
1479 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001480 }
1481
1482 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001483 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1484 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001485 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001486 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001487 cast<ValueDecl>(D)->setType(NewTy);
1488}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001489
Mike Stump1feade82009-08-26 22:31:08 +00001490static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001491 // check the attribute arguments.
1492 if (Attr.getNumArgs() > 0) {
1493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1494 return;
1495 }
Anders Carlssone896d982009-02-13 08:11:52 +00001496
Anders Carlsson5bab7882009-02-19 19:16:48 +00001497 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001499 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001500 return;
1501 }
Mike Stumpbf916502009-07-24 19:02:52 +00001502
Mike Stump1feade82009-08-26 22:31:08 +00001503 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001504}
1505
Mike Stump1feade82009-08-26 22:31:08 +00001506static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001507 // check the attribute arguments.
1508 if (Attr.getNumArgs() != 0) {
1509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1510 return;
1511 }
Mike Stumpbf916502009-07-24 19:02:52 +00001512
Chris Lattnerc5197432009-04-14 17:02:11 +00001513 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001514 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001515 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001516 return;
1517 }
Mike Stumpbf916502009-07-24 19:02:52 +00001518
Mike Stump1feade82009-08-26 22:31:08 +00001519 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001520}
1521
Chris Lattnercf2a7212009-04-20 19:12:28 +00001522static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001523 // check the attribute arguments.
1524 if (Attr.getNumArgs() != 0) {
1525 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1526 return;
1527 }
Mike Stumpbf916502009-07-24 19:02:52 +00001528
Chris Lattnerc5197432009-04-14 17:02:11 +00001529 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1530 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001532 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001533 return;
1534 }
Mike Stumpbf916502009-07-24 19:02:52 +00001535
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001536 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001537 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001538 return;
1539 }
Mike Stumpbf916502009-07-24 19:02:52 +00001540
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001541 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001542}
1543
Fariborz Jahanianee760332009-03-27 18:38:55 +00001544static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1545 // check the attribute arguments.
1546 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001547 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001548 return;
1549 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001550
Fariborz Jahanianee760332009-03-27 18:38:55 +00001551 if (!isFunctionOrMethod(d)) {
1552 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001553 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001554 return;
1555 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001556
1557 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1558 llvm::APSInt NumParams(32);
1559 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1560 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1561 << "regparm" << NumParamsExpr->getSourceRange();
1562 return;
1563 }
1564
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001565 if (S.Context.Target.getRegParmMax() == 0) {
1566 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001567 << NumParamsExpr->getSourceRange();
1568 return;
1569 }
1570
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001571 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001572 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1573 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001574 return;
1575 }
1576
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001577 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001578}
1579
Sean Huntbbd37c62009-11-21 08:43:09 +00001580static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1581 // check the attribute arguments.
1582 if (Attr.getNumArgs() != 0) {
1583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1584 return;
1585 }
1586
1587 if (!isa<CXXRecordDecl>(d)
1588 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1589 S.Diag(Attr.getLoc(),
1590 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1591 : diag::warn_attribute_wrong_decl_type)
1592 << Attr.getName() << 7 /*virtual method or class*/;
1593 return;
1594 }
Sean Hunt7725e672009-11-25 04:20:27 +00001595
1596 // FIXME: Conform to C++0x redeclaration rules.
1597
1598 if (d->getAttr<FinalAttr>()) {
1599 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1600 return;
1601 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001602
1603 d->addAttr(::new (S.Context) FinalAttr());
1604}
1605
Chris Lattner0744e5f2008-06-29 00:23:49 +00001606//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001607// C++0x member checking attributes
1608//===----------------------------------------------------------------------===//
1609
1610static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1611 if (Attr.getNumArgs() != 0) {
1612 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1613 return;
1614 }
1615
1616 if (!isa<CXXRecordDecl>(d)) {
1617 S.Diag(Attr.getLoc(),
1618 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1619 : diag::warn_attribute_wrong_decl_type)
1620 << Attr.getName() << 9 /*class*/;
1621 return;
1622 }
1623
1624 if (d->getAttr<BaseCheckAttr>()) {
1625 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1626 return;
1627 }
1628
1629 d->addAttr(::new (S.Context) BaseCheckAttr());
1630}
1631
1632static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1633 if (Attr.getNumArgs() != 0) {
1634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1635 return;
1636 }
1637
1638 if (!isa<RecordDecl>(d->getDeclContext())) {
1639 // FIXME: It's not the type that's the problem
1640 S.Diag(Attr.getLoc(),
1641 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1642 : diag::warn_attribute_wrong_decl_type)
1643 << Attr.getName() << 11 /*member*/;
1644 return;
1645 }
1646
1647 // FIXME: Conform to C++0x redeclaration rules.
1648
1649 if (d->getAttr<HidingAttr>()) {
1650 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1651 return;
1652 }
1653
1654 d->addAttr(::new (S.Context) HidingAttr());
1655}
1656
1657static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1658 if (Attr.getNumArgs() != 0) {
1659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1660 return;
1661 }
1662
1663 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1664 // FIXME: It's not the type that's the problem
1665 S.Diag(Attr.getLoc(),
1666 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1667 : diag::warn_attribute_wrong_decl_type)
1668 << Attr.getName() << 10 /*virtual method*/;
1669 return;
1670 }
1671
1672 // FIXME: Conform to C++0x redeclaration rules.
1673
1674 if (d->getAttr<OverrideAttr>()) {
1675 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1676 return;
1677 }
1678
1679 d->addAttr(::new (S.Context) OverrideAttr());
1680}
1681
1682//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001683// Checker-specific attribute handlers.
1684//===----------------------------------------------------------------------===//
1685
1686static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1687 Sema &S) {
1688
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001689 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001690
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001691 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1692 RetTy = MD->getResultType();
1693 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1694 RetTy = FD->getResultType();
1695 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001696 SourceLocation L = Attr.getLoc();
1697 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1698 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001699 return;
1700 }
Mike Stumpbf916502009-07-24 19:02:52 +00001701
Ted Kremenek6217b802009-07-29 21:53:49 +00001702 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001703 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001704 SourceLocation L = Attr.getLoc();
1705 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1706 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001707 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001708 }
Mike Stumpbf916502009-07-24 19:02:52 +00001709
Ted Kremenekb71368d2009-05-09 02:44:38 +00001710 switch (Attr.getKind()) {
1711 default:
1712 assert(0 && "invalid ownership attribute");
1713 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00001714 case AttributeList::AT_cf_returns_not_retained:
1715 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1716 return;
1717 case AttributeList::AT_ns_returns_not_retained:
1718 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1719 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001720 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001721 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001722 return;
1723 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001724 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001725 return;
1726 };
1727}
1728
Charles Davisf0122fe2010-02-16 18:27:26 +00001729static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1730 return Attr.getKind() == AttributeList::AT_dllimport ||
1731 Attr.getKind() == AttributeList::AT_dllexport;
1732}
1733
Ted Kremenekb71368d2009-05-09 02:44:38 +00001734//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001735// Top Level Sema Entry Points
1736//===----------------------------------------------------------------------===//
1737
Sebastian Redla89d82c2008-12-21 19:24:58 +00001738/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001739/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001740/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1741/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001742static void ProcessDeclAttribute(Scope *scope, Decl *D,
1743 const AttributeList &Attr, Sema &S) {
Charles Davisf0122fe2010-02-16 18:27:26 +00001744 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1745 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00001746 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001747 switch (Attr.getKind()) {
Ted Kremenekefbddd22010-02-17 02:37:45 +00001748 case AttributeList::AT_IBAction:
1749 case AttributeList::AT_IBOutlet: HandleIBAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001750 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001751 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001752 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001753 // Ignore these, these are type attributes, handled by
1754 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001755 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001756 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1757 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001758 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001759 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001760 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001761 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001762 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1763 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001764 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001765 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001766 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1767 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1768 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001769 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001770 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001771 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001772 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1773 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1774 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1775 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1776 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1777 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1778 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1779 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1780 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1781 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1782 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001783
1784 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00001785 case AttributeList::AT_ns_returns_not_retained:
1786 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001787 case AttributeList::AT_ns_returns_retained:
1788 case AttributeList::AT_cf_returns_retained:
1789 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1790
Nate Begeman6f3d8382009-06-26 06:32:41 +00001791 case AttributeList::AT_reqd_wg_size:
1792 HandleReqdWorkGroupSize(D, Attr, S); break;
1793
Sean Hunt7725e672009-11-25 04:20:27 +00001794 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1795 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001796 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1797 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1798 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001799 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001800 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1801 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001802 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1803 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001804 case AttributeList::AT_transparent_union:
1805 HandleTransparentUnionAttr(D, Attr, S);
1806 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001807 case AttributeList::AT_objc_exception:
1808 HandleObjCExceptionAttr(D, Attr, S);
1809 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001810 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001811 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1812 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1813 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1814 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1815 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1816 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1817 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1818 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1819 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001820 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001821 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001822 // Just ignore
1823 break;
John McCall04a67a62010-02-05 21:31:56 +00001824 case AttributeList::AT_stdcall:
1825 case AttributeList::AT_cdecl:
1826 case AttributeList::AT_fastcall:
1827 // These are all treated as type attributes.
1828 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001829 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001830 // Ask target about the attribute.
1831 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1832 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1833 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001834 break;
1835 }
1836}
1837
1838/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1839/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001840void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001841 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001842 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001843 AttrList = AttrList->getNext();
1844 }
1845}
1846
Ryan Flynne25ff832009-07-30 03:15:39 +00001847/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1848/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001849NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001850 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001851 NamedDecl *NewD = 0;
1852 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1853 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1854 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001855 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001856 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1857 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1858 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001859 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001860 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001861 }
1862 return NewD;
1863}
1864
1865/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1866/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001867void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001868 if (W.getUsed()) return; // only do this once
1869 W.setUsed(true);
1870 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1871 IdentifierInfo *NDId = ND->getIdentifier();
1872 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001873 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001874 NewD->addAttr(::new (Context) WeakAttr());
1875 WeakTopLevelDecl.push_back(NewD);
1876 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1877 // to insert Decl at TU scope, sorry.
1878 DeclContext *SavedContext = CurContext;
1879 CurContext = Context.getTranslationUnitDecl();
1880 PushOnScopeChains(NewD, S);
1881 CurContext = SavedContext;
1882 } else { // just add weak to existing
1883 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001884 }
1885}
1886
Chris Lattner0744e5f2008-06-29 00:23:49 +00001887/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1888/// it, apply them to D. This is a bit tricky because PD can have attributes
1889/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001890void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001891 // Handle #pragma weak
1892 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1893 if (ND->hasLinkage()) {
1894 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1895 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001896 // Identifier referenced by #pragma weak before it was declared
1897 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001898 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1899 }
1900 }
1901 }
1902
Chris Lattner0744e5f2008-06-29 00:23:49 +00001903 // Apply decl attributes from the DeclSpec if present.
1904 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001905 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001906
Chris Lattner0744e5f2008-06-29 00:23:49 +00001907 // Walk the declarator structure, applying decl attributes that were in a type
1908 // position to the decl itself. This handles cases like:
1909 // int *__attr__(x)** D;
1910 // when X is a decl attribute.
1911 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1912 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001913 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001914
Chris Lattner0744e5f2008-06-29 00:23:49 +00001915 // Finally, apply any attributes on the decl itself.
1916 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001917 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001918}
John McCall54abf7d2009-11-04 02:18:39 +00001919
1920/// PushParsingDeclaration - Enter a new "scope" of deprecation
1921/// warnings.
1922///
1923/// The state token we use is the start index of this scope
1924/// on the warning stack.
1925Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
1926 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00001927 return (ParsingDeclStackState) DelayedDiagnostics.size();
1928}
1929
1930void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
1931 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
1932 ParsingDeclDepth--;
1933
1934 if (DelayedDiagnostics.empty())
1935 return;
1936
1937 unsigned SavedIndex = (unsigned) S;
1938 assert(SavedIndex <= DelayedDiagnostics.size() &&
1939 "saved index is out of bounds");
1940
1941 // We only want to actually emit delayed diagnostics when we
1942 // successfully parsed a decl.
1943 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
1944 if (D) {
1945 // We really do want to start with 0 here. We get one push for a
1946 // decl spec and another for each declarator; in a decl group like:
1947 // deprecated_typedef foo, *bar, baz();
1948 // only the declarator pops will be passed decls. This is correct;
1949 // we really do need to consider delayed diagnostics from the decl spec
1950 // for each of the different declarations.
1951 for (unsigned I = 0, E = DelayedDiagnostics.size(); I != E; ++I) {
1952 if (DelayedDiagnostics[I].Triggered)
1953 continue;
1954
1955 switch (DelayedDiagnostics[I].Kind) {
1956 case DelayedDiagnostic::Deprecation:
1957 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
1958 break;
1959
1960 case DelayedDiagnostic::Access:
1961 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
1962 break;
1963 }
1964 }
1965 }
1966
1967 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00001968}
1969
1970static bool isDeclDeprecated(Decl *D) {
1971 do {
1972 if (D->hasAttr<DeprecatedAttr>())
1973 return true;
1974 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
1975 return false;
1976}
1977
John McCall2f514482010-01-27 03:50:35 +00001978void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
1979 Decl *Ctx) {
1980 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00001981 return;
1982
John McCall2f514482010-01-27 03:50:35 +00001983 DD.Triggered = true;
1984 Diag(DD.Loc, diag::warn_deprecated)
1985 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00001986}
1987
1988void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
1989 // Delay if we're currently parsing a declaration.
1990 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00001991 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00001992 return;
1993 }
1994
1995 // Otherwise, don't warn if our current context is deprecated.
1996 if (isDeclDeprecated(cast<Decl>(CurContext)))
1997 return;
1998
1999 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2000}