blob: e0943274dac29bbe2540b3008c8c5964f5021827 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattner58418ff2008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremenek527042b2009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000044
John McCall9dd450b2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000046}
47
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopes518e3702009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000062}
63
Fariborz Jahanian4447e172009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000076}
77
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000084 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099}
100
Ted Kremenek527042b2009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000106
Chris Lattnera4997152009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000108}
109
Ted Kremenek527042b2009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremenek527042b2009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
121 return BD->IsVariadic();
122 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000130 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000131
John McCall9dd450b2009-09-21 23:43:11 +0000132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000133 if (!ClsT)
134 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000135
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000136 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar980c6692008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
Daniel Dunbar980c6692008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
153 if (RD->getTagKind() != TagDecl::TK_struct)
154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattner58418ff2008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar032db472008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000172 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000173 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000200
Douglas Gregor758a8692009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000203 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000204}
205
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000212
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlsson68e0b682009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000222 else
Anders Carlsson68e0b682009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000224 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000226}
227
Ted Kremenek06be9682010-02-17 02:37:45 +0000228static void HandleIBAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000234
Ted Kremenek06be9682010-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 Kremenek8e3704d2008-07-15 22:26:48 +0000249 else
Ted Kremenek06be9682010-02-17 02:37:45 +0000250 S.Diag(Attr.getLoc(), diag::err_attribute_ib) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000251}
252
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000253static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-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 Dunbar70e3eba2008-10-19 02:04:16 +0000256 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000257 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000258 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000259 return;
260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000261
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000262 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000263
264 // The nonnull attribute only applies to pointers.
265 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000266
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000267 for (AttributeList::arg_iterator I=Attr.arg_begin(),
268 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000269
270
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000271 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000272 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000273 llvm::APSInt ArgNum(32);
274 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
276 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000277 return;
278 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000279
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000280 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000281
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000282 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000283 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000284 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000285 return;
286 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000287
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000288 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000289
290 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000291 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000292 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000293 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000294 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
295 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000296 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000297 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000298
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000299 NonNullArgs.push_back(x);
300 }
Mike Stumpd3bb5572009-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 Kremenekc4f6d902008-09-01 19:57:52 +0000304 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000305 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
306 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000307 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000308 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000309 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000310
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000311 if (NonNullArgs.empty()) {
312 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
313 return;
314 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000315 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000316
317 unsigned* start = &NonNullArgs[0];
318 unsigned size = NonNullArgs.size();
319 std::sort(start, start + size);
Ted Kremenek510ee252010-02-11 07:31:47 +0000320 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000321}
322
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000323static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000324 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000325 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000327 return;
328 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000329
Chris Lattner4a927cb2008-06-28 23:36:30 +0000330 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000331 Arg = Arg->IgnoreParenCasts();
332 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000333
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000334 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000335 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000336 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000337 return;
338 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000339
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000340 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000341
Ted Kremenek7f4945a2010-02-11 05:28:37 +0000342 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000343}
344
Mike Stumpd3bb5572009-07-24 19:02:52 +0000345static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000346 Sema &S) {
347 // check the attribute arguments.
348 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000350 return;
351 }
Anders Carlsson88097122009-02-19 19:16:48 +0000352
Chris Lattner4225e232009-04-14 17:02:11 +0000353 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000355 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000356 return;
357 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000358
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000359 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000360}
361
Ryan Flynn1f1fdc02009-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 Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenek08479ae2009-08-15 00:51:46 +0000369 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000370 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000371 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
372 d->addAttr(::new (S.Context) MallocAttr());
373 return;
374 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000375 }
376
Ted Kremenek08479ae2009-08-15 00:51:46 +0000377 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000378}
379
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000380static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000381 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000382 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000383 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000384 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000385 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000386 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000387
Mike Stump88788fe2009-04-29 19:03:13 +0000388 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
389 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stumpfeb19452009-12-15 03:11:10 +0000390 if (VD == 0 || (!VD->getType()->isBlockPointerType()
391 && !VD->getType()->isFunctionPointerType())) {
Alexis Hunt96d5c762009-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 Kremenek3b204e42009-05-13 21:07:32 +0000395 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000396 return false;
397 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000398 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000399
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000400 return true;
401}
402
403static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
John McCallab26cfa2010-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 Stumpd3bb5572009-07-24 19:02:52 +0000409 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000410 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000411}
412
413static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
414 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000415 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000416 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000417}
418
Alexis Hunt96d5c762009-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 McCallab26cfa2010-02-05 21:31:56 +0000422 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000423 return;
424 }
425 // FIXME: Actually store the attribute on the declaration
426}
427
Ted Kremenek39c59a82008-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 Lattner4bd8dd82008-11-19 08:23:25 +0000431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000432 return;
433 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000434
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000435 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000436 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000437 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000438 return;
439 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000440
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000441 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000442}
443
Daniel Dunbarfee07a02009-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 Stumpd3bb5572009-07-24 19:02:52 +0000450
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000451 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000452 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-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 Kremenek3b204e42009-05-13 21:07:32 +0000458 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000459 return;
460 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000461
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000462 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000463}
464
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000468 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
469 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000470 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000471 }
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000478 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000479 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000480 return;
481 }
482 priority = Idx.getZExtValue();
483 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000484
Chris Lattner4225e232009-04-14 17:02:11 +0000485 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000486 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000487 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000488 return;
489 }
490
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000491 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000497 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
498 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000499 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000500 }
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000507 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000508 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000509 return;
510 }
511 priority = Idx.getZExtValue();
512 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000513
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000514 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000515 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000516 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000517 return;
518 }
519
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000520 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000521}
522
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000523static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000524 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000525 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000527 return;
528 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000529
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000530 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000531}
532
Fariborz Jahanian1470e932008-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 Stumpd3bb5572009-07-24 19:02:52 +0000539
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000540 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000541}
542
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000543static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000544 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000545 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000547 return;
548 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000549
Chris Lattner4a927cb2008-06-28 23:36:30 +0000550 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000551 Arg = Arg->IgnoreParenCasts();
552 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000553
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000554 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000555 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000556 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000557 return;
558 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000559
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000560 llvm::StringRef TypeStr = Str->getString();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000561 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000562
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000563 if (TypeStr == "default")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000564 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000565 else if (TypeStr == "hidden")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000566 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000567 else if (TypeStr == "internal")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000568 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000569 else if (TypeStr == "protected")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000570 type = VisibilityAttr::ProtectedVisibility;
571 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000572 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000573 return;
574 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000575
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000576 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000577}
578
Chris Lattner677a3582009-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 Stumpd3bb5572009-07-24 19:02:52 +0000585
Chris Lattner677a3582009-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 Stumpd3bb5572009-07-24 19:02:52 +0000591
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000592 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000593}
594
595static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-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 Lattner677a3582009-02-14 08:09:34 +0000600 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000601 QualType T = TD->getUnderlyingType();
602 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000603 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000604 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
605 return;
606 }
607 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000608 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000609}
610
Mike Stumpd3bb5572009-07-24 19:02:52 +0000611static void
Douglas Gregor4e5cbdc2009-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 Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000623 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000624}
625
Steve Naroff3405a732008-09-18 16:44:58 +0000626static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000627 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000628 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000629 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000630 return;
631 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000632
Steve Naroff3405a732008-09-18 16:44:58 +0000633 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000635 return;
636 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000637
Steve Naroff3405a732008-09-18 16:44:58 +0000638 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000639 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000640 type = BlocksAttr::ByRef;
641 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000642 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000643 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000644 return;
645 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000646
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000647 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000648}
649
Anders Carlssonc181b012008-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 Lattner3b054132008-11-19 05:08:23 +0000653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
654 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000655 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000656 }
657
Anders Carlssonc181b012008-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 Lattner3b054132008-11-19 05:08:23 +0000663 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000664 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000665 return;
666 }
667 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000668
Anders Carlssonc181b012008-10-05 18:05:59 +0000669 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000670 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
671 << E->getSourceRange();
Anders Carlssonc181b012008-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 Lattner3b054132008-11-19 05:08:23 +0000681 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000682 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000683 return;
684 }
685 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000686
Anders Carlssonc181b012008-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 Lattner3b054132008-11-19 05:08:23 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
691 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000692 return;
693 }
694 }
695
696 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000697 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000698 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000699
Chris Lattner9363e312009-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 Stumpd3bb5572009-07-24 19:02:52 +0000704
Chris Lattner9363e312009-03-17 23:03:47 +0000705 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000706 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000707 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000708 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000709 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
710 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000711 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000712 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000713 }
714 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000715 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
716 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000717 ;
718 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000719 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000720 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000721 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000722 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000723 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-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 Jahanian6607b212009-05-14 20:53:39 +0000726 return;
727 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000728 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000730 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000731 return;
732 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000733 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000735 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000736 return;
737 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000738 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000739}
740
Chris Lattner237f2752009-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 Lopes518e3702009-12-20 23:11:08 +0000748 if (!isFunctionOrMethod(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +0000749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000750 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000751 return;
752 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000753
Nuno Lopes56abcbd2009-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 Lopes518e3702009-12-20 23:11:08 +0000760 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000761}
762
763static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000764 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000765 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000767 return;
768 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000769
Fariborz Jahanian41136ee2009-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 Dunbar5cb85eb2009-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 Kremenek3b204e42009-05-13 21:07:32 +0000786 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000787 return;
788 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000789
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000790 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000791}
792
Daniel Dunbar5cb85eb2009-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 Stumpd3bb5572009-07-24 19:02:52 +0000798 }
Daniel Dunbar5cb85eb2009-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 Kyrtzidisddcd1322009-06-30 02:35:26 +0000805 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000806 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
807 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000808 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000809 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000811 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000812 return;
813 }
814
815 // Merge should handle any subsequent violations.
816 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000817 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-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 Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000823 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000824}
825
Nate Begemanf2758702009-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 Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000845 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000846 WGSize[2]));
847}
848
Chris Lattner237f2752009-02-14 07:37:35 +0000849static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-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 Lattner30ba6742009-08-10 19:03:04 +0000858 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
859 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +0000860 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +0000861 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +0000862 return;
863 }
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattner30ba6742009-08-10 19:03:04 +0000865 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +0000866 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +0000867 if (!Error.empty()) {
868 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
869 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +0000870 return;
871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattner20aee9b2010-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 Kremenek7f4945a2010-02-11 05:28:37 +0000879 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +0000880}
881
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000882
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000883static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000884 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000885 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000887 return;
888 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000889
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000890 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000891}
892
Anders Carlssonb8316282008-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 Lattner4bd8dd82008-11-19 08:23:25 +0000896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +0000897 return;
898 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000899
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000900 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-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 Lattner4bd8dd82008-11-19 08:23:25 +0000906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +0000907 return;
908 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000909
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000910 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +0000911}
912
Anders Carlssond277d792009-01-31 01:16:18 +0000913static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000914 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +0000915 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
916 return;
917 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000918
Anders Carlssond277d792009-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 Stumpd3bb5572009-07-24 19:02:52 +0000923
Anders Carlssond277d792009-01-31 01:16:18 +0000924 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000925
Anders Carlssond277d792009-01-31 01:16:18 +0000926 if (!VD || !VD->hasLocalStorage()) {
927 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
928 return;
929 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000930
Anders Carlssond277d792009-01-31 01:16:18 +0000931 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +0000932 NamedDecl *CleanupDecl
933 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
934 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +0000935 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +0000936 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +0000937 Attr.getParameterName();
938 return;
939 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000940
Anders Carlssond277d792009-01-31 01:16:18 +0000941 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
942 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +0000943 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +0000944 Attr.getParameterName();
945 return;
946 }
947
Anders Carlssond277d792009-01-31 01:16:18 +0000948 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +0000949 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +0000950 Attr.getParameterName();
951 return;
952 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000953
Anders Carlsson723f55d2009-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 Friedmanbd0e6732009-04-26 01:30:08 +0000958 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000959 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +0000960 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
961 Attr.getParameterName() << ParamTy << Ty;
962 return;
963 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000964
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000965 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +0000966}
967
Mike Stumpd3bb5572009-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 Jahanianf1c25022009-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 Stumpd3bb5572009-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 Jahanianf1c25022009-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 Stumpd3bb5572009-07-24 19:02:52 +0000992
Fariborz Jahanianf1c25022009-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 Stumpd3bb5572009-07-24 19:02:52 +0000998
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000999 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001000
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001001 // make sure the format string is really a string
1002 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001003
Fariborz Jahanianf1c25022009-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 Kremenekc23c7e62009-07-29 21:53:49 +00001008 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-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 Stumpd3bb5572009-07-24 19:02:52 +00001011 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001012 << IdxExpr->getSourceRange();
1013 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001014 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001015 Ty = getFunctionOrMethodResultType(d);
1016 if (!isNSStringType(Ty, S.Context) &&
1017 !isCFStringType(Ty, S.Context) &&
1018 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001019 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-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 Stumpd3bb5572009-07-24 19:02:52 +00001022 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001023 << IdxExpr->getSourceRange();
1024 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001025 }
1026
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001027 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001028}
1029
Daniel Dunbarccbd9a42009-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 Stumpd3bb5572009-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 Lattnerb632a6e2008-06-29 00:43:07 +00001061static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001062
Chris Lattner4a927cb2008-06-28 23:36:30 +00001063 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001065 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001066 return;
1067 }
1068
Chris Lattner4a927cb2008-06-28 23:36:30 +00001069 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001071 return;
1072 }
1073
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001074 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001076 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001077 return;
1078 }
1079
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001080 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001081 unsigned FirstIdx = 1;
1082
Daniel Dunbar07d07852009-10-18 21:17:35 +00001083 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001084
1085 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001086 if (Format.startswith("__") && Format.endswith("__"))
1087 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001088
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001089 // Check for supported formats.
1090 FormatAttrKind Kind = getFormatAttrKind(Format);
1091 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001092 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001093 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001094 return;
1095 }
1096
1097 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001098 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001099 llvm::APSInt Idx(32);
1100 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001102 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001103 return;
1104 }
1105
Anders Carlssonbe96bc92009-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 Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001116 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001118 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001119 return;
1120 }
1121
1122 // FIXME: Do we need to bounds check?
1123 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001124
Sebastian Redl6eedcc12009-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 Stump11289f42009-09-09 15:08:12 +00001133
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001134 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001135 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001136
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001137 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001138 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001139 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1140 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001141 return;
1142 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001143 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001144 // FIXME: do we need to check if the type is NSString*? What are the
1145 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001146 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001147 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001148 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1149 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001150 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001151 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001152 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001153 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001154 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001155 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1156 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001157 return;
1158 }
1159
1160 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001161 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001162 llvm::APSInt FirstArg(32);
1163 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001164 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001165 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-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 Dunbarc136e0c2008-09-26 04:12:28 +00001171 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001172 ++NumArgs; // +1 for ...
1173 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001174 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001175 return;
1176 }
1177 }
1178
Chris Lattner4bd8dd82008-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 Dunbarccbd9a42009-10-18 02:09:17 +00001181 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001182 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001183 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1184 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-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 Lattner3b054132008-11-19 05:08:23 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001190 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001191 return;
1192 }
1193
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001194 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001195 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001196}
1197
Chris Lattnera663a0a2008-06-29 00:28:59 +00001198static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1199 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001200 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001201 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001203 return;
1204 }
1205
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001206 // Try to find the underlying union declaration.
1207 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001208 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-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 Lattner3b054132008-11-19 05:08:23 +00001215 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001216 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001217 return;
1218 }
1219
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001220 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001221 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001222 diag::warn_transparent_union_attribute_not_definition);
1223 return;
1224 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001225
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001226 RecordDecl::field_iterator Field = RD->field_begin(),
1227 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-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 Friedman7c9ba6a2008-09-02 05:19:23 +00001232
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001233 FieldDecl *FirstField = *Field;
1234 QualType FirstType = FirstField->getType();
1235 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001236 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-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 Stumpd3bb5572009-07-24 19:02:52 +00001249 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001250 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001251 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-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 Stumpd3bb5572009-07-24 19:02:52 +00001255 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001256 diag::note_transparent_union_first_field_size_align)
1257 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001258 return;
1259 }
1260 }
1261
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001262 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001263}
1264
Chris Lattnera663a0a2008-06-29 00:28:59 +00001265static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001266 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001267 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001269 return;
1270 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001271 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1272 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001273
Chris Lattner2c6fcf52008-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 Lattner30ba6742009-08-10 19:03:04 +00001277 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001278 return;
1279 }
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001280 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001281}
1282
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001283static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001284 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001285 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001287 return;
1288 }
Alexis Hunt96d5c762009-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 Lattner2c6fcf52008-06-26 18:38:35 +00001293
1294 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001295 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001296 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001297 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001298 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001299 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001300 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001301 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001302
Chris Lattner4627b742008-06-28 23:50:44 +00001303 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1304 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001305 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001306 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1307 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001308 return;
1309 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001310 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001312 << alignmentExpr->getSourceRange();
1313 return;
1314 }
1315
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001316 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001317}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001318
Mike Stumpd3bb5572009-07-24 19:02:52 +00001319/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1320/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001321///
Mike Stumpd3bb5572009-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 Lattnera663a0a2008-06-29 00:28:59 +00001325static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-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 Lattner4bd8dd82008-11-19 08:23:25 +00001331 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001332 return;
1333 }
1334
1335 IdentifierInfo *Name = Attr.getParameterName();
1336 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001337 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001338 return;
1339 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001340
Daniel Dunbar07d07852009-10-18 21:17:35 +00001341 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001342
1343 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001344 if (Str.startswith("__") && Str.endswith("__"))
1345 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001346
1347 unsigned DestWidth = 0;
1348 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001349 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001350 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001351 case 2:
Eli Friedman4735374e2009-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 Lattneracbc2d22008-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 Dunbarafff4342009-10-18 02:09:24 +00001372 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001373 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001374 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001375 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001376 break;
1377 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001378 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001379 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-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 Lattner3b054132008-11-19 05:08:23 +00001389 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1390 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001391 return;
1392 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001393
John McCall9dd450b2009-09-21 23:43:11 +00001394 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-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 Stump87c57ac2009-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 Friedman1efaaea2009-02-13 02:31:07 +00001411 // FIXME: Make sure floating-point mappings are accurate
1412 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001413 QualType NewTy;
1414 switch (DestWidth) {
1415 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001416 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001417 return;
1418 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001419 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001420 return;
1421 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001422 if (!IntegerMode) {
1423 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1424 return;
1425 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001426 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001427 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001428 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001429 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001430 break;
1431 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001432 if (!IntegerMode) {
1433 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1434 return;
1435 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001436 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001437 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001438 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001439 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001440 break;
1441 case 32:
1442 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001443 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001444 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001445 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001446 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001447 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001448 break;
1449 case 64:
1450 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001451 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001452 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-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 Lattneracbc2d22008-06-27 22:18:37 +00001457 else
Chandler Carruth72343702010-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 Lattneracbc2d22008-06-27 22:18:37 +00001462 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001463 case 96:
1464 NewTy = S.Context.LongDoubleTy;
1465 break;
Eli Friedman1efaaea2009-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 Carlsson88ea2452009-12-29 07:07:36 +00001471 if (OldTy->isSignedIntegerType())
1472 NewTy = S.Context.Int128Ty;
1473 else
1474 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001475 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001476 }
1477
Eli Friedman4735374e2009-03-03 06:41:03 +00001478 if (ComplexMode) {
1479 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001480 }
1481
1482 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001483 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1484 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001485 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001486 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001487 cast<ValueDecl>(D)->setType(NewTy);
1488}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001489
Mike Stump3722f582009-08-26 22:31:08 +00001490static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-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 Carlsson63784f42009-02-13 08:11:52 +00001496
Anders Carlsson88097122009-02-19 19:16:48 +00001497 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001499 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001500 return;
1501 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001502
Mike Stump3722f582009-08-26 22:31:08 +00001503 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001504}
1505
Mike Stump3722f582009-08-26 22:31:08 +00001506static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-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 Stumpd3bb5572009-07-24 19:02:52 +00001512
Chris Lattner4225e232009-04-14 17:02:11 +00001513 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001514 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001515 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001516 return;
1517 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001518
Mike Stump3722f582009-08-26 22:31:08 +00001519 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001520}
1521
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001522static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-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 Stumpd3bb5572009-07-24 19:02:52 +00001528
Chris Lattner4225e232009-04-14 17:02:11 +00001529 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1530 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001532 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001533 return;
1534 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001535
Douglas Gregor35b57532009-10-27 21:01:01 +00001536 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001537 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001538 return;
1539 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001540
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001541 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001542}
1543
Fariborz Jahaniana2d609e2009-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 Friedman7044b762009-03-27 21:06:47 +00001547 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001548 return;
1549 }
Eli Friedman7044b762009-03-27 21:06:47 +00001550
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001551 if (!isFunctionOrMethod(d)) {
1552 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001553 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001554 return;
1555 }
Eli Friedman7044b762009-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 Korobeynikov6953ef22009-04-03 23:38:25 +00001565 if (S.Context.Target.getRegParmMax() == 0) {
1566 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001567 << NumParamsExpr->getSourceRange();
1568 return;
1569 }
1570
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001571 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001572 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1573 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001574 return;
1575 }
1576
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001577 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001578}
1579
Alexis Hunt96d5c762009-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 }
Alexis Hunt54a02542009-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 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001602
1603 d->addAttr(::new (S.Context) FinalAttr());
1604}
1605
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001606//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-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 Kremenek9ecdfaf2009-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 Kremenek3b204e42009-05-13 21:07:32 +00001689 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001690
Ted Kremenek3b204e42009-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 Kremenekeebcf572009-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 Kremenek9ecdfaf2009-05-09 02:44:38 +00001699 return;
1700 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001701
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001702 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001703 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-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 Stumpd3bb5572009-07-24 19:02:52 +00001707 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001708 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001709
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001710 switch (Attr.getKind()) {
1711 default:
1712 assert(0 && "invalid ownership attribute");
1713 return;
1714 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001715 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001716 return;
1717 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001718 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001719 return;
1720 };
1721}
1722
Charles Davis163855f2010-02-16 18:27:26 +00001723static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1724 return Attr.getKind() == AttributeList::AT_dllimport ||
1725 Attr.getKind() == AttributeList::AT_dllexport;
1726}
1727
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001728//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001729// Top Level Sema Entry Points
1730//===----------------------------------------------------------------------===//
1731
Sebastian Redlfc24b632008-12-21 19:24:58 +00001732/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001733/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001734/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1735/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001736static void ProcessDeclAttribute(Scope *scope, Decl *D,
1737 const AttributeList &Attr, Sema &S) {
Charles Davis163855f2010-02-16 18:27:26 +00001738 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1739 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00001740 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001741 switch (Attr.getKind()) {
Ted Kremenek06be9682010-02-17 02:37:45 +00001742 case AttributeList::AT_IBAction:
1743 case AttributeList::AT_IBOutlet: HandleIBAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001744 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001745 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00001746 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001747 // Ignore these, these are type attributes, handled by
1748 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001749 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001750 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1751 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001752 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001753 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001754 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001755 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001756 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1757 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001758 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001759 HandleDependencyAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001760 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1761 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1762 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001763 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001764 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001765 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001766 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1767 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1768 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1769 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1770 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1771 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1772 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1773 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1774 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1775 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1776 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001777
1778 // Checker-specific.
1779 case AttributeList::AT_ns_returns_retained:
1780 case AttributeList::AT_cf_returns_retained:
1781 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1782
Nate Begemanf2758702009-06-26 06:32:41 +00001783 case AttributeList::AT_reqd_wg_size:
1784 HandleReqdWorkGroupSize(D, Attr, S); break;
1785
Alexis Hunt54a02542009-11-25 04:20:27 +00001786 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1787 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001788 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1789 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1790 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001791 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00001792 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1793 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001794 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1795 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001796 case AttributeList::AT_transparent_union:
1797 HandleTransparentUnionAttr(D, Attr, S);
1798 break;
Chris Lattner677a3582009-02-14 08:09:34 +00001799 case AttributeList::AT_objc_exception:
1800 HandleObjCExceptionAttr(D, Attr, S);
1801 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001802 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001803 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1804 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1805 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1806 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1807 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1808 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1809 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1810 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1811 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001812 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00001813 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00001814 // Just ignore
1815 break;
John McCallab26cfa2010-02-05 21:31:56 +00001816 case AttributeList::AT_stdcall:
1817 case AttributeList::AT_cdecl:
1818 case AttributeList::AT_fastcall:
1819 // These are all treated as type attributes.
1820 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001821 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001822 // Ask target about the attribute.
1823 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1824 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1825 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001826 break;
1827 }
1828}
1829
1830/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1831/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00001832void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001833 while (AttrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +00001834 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001835 AttrList = AttrList->getNext();
1836 }
1837}
1838
Ryan Flynn7d470f32009-07-30 03:15:39 +00001839/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1840/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00001841NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001842 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00001843 NamedDecl *NewD = 0;
1844 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1845 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1846 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00001847 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001848 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1849 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1850 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00001851 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001852 VD->getStorageClass());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001853 }
1854 return NewD;
1855}
1856
1857/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1858/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00001859void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00001860 if (W.getUsed()) return; // only do this once
1861 W.setUsed(true);
1862 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1863 IdentifierInfo *NDId = ND->getIdentifier();
1864 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001865 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnere6eab982009-09-08 18:10:11 +00001866 NewD->addAttr(::new (Context) WeakAttr());
1867 WeakTopLevelDecl.push_back(NewD);
1868 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1869 // to insert Decl at TU scope, sorry.
1870 DeclContext *SavedContext = CurContext;
1871 CurContext = Context.getTranslationUnitDecl();
1872 PushOnScopeChains(NewD, S);
1873 CurContext = SavedContext;
1874 } else { // just add weak to existing
1875 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001876 }
1877}
1878
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001879/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1880/// it, apply them to D. This is a bit tricky because PD can have attributes
1881/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00001882void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00001883 // Handle #pragma weak
1884 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1885 if (ND->hasLinkage()) {
1886 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1887 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001888 // Identifier referenced by #pragma weak before it was declared
1889 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00001890 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1891 }
1892 }
1893 }
1894
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001895 // Apply decl attributes from the DeclSpec if present.
1896 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00001897 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001898
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001899 // Walk the declarator structure, applying decl attributes that were in a type
1900 // position to the decl itself. This handles cases like:
1901 // int *__attr__(x)** D;
1902 // when X is a decl attribute.
1903 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1904 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00001905 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001906
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001907 // Finally, apply any attributes on the decl itself.
1908 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00001909 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001910}
John McCall28a6aea2009-11-04 02:18:39 +00001911
1912/// PushParsingDeclaration - Enter a new "scope" of deprecation
1913/// warnings.
1914///
1915/// The state token we use is the start index of this scope
1916/// on the warning stack.
1917Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
1918 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00001919 return (ParsingDeclStackState) DelayedDiagnostics.size();
1920}
1921
1922void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
1923 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
1924 ParsingDeclDepth--;
1925
1926 if (DelayedDiagnostics.empty())
1927 return;
1928
1929 unsigned SavedIndex = (unsigned) S;
1930 assert(SavedIndex <= DelayedDiagnostics.size() &&
1931 "saved index is out of bounds");
1932
1933 // We only want to actually emit delayed diagnostics when we
1934 // successfully parsed a decl.
1935 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
1936 if (D) {
1937 // We really do want to start with 0 here. We get one push for a
1938 // decl spec and another for each declarator; in a decl group like:
1939 // deprecated_typedef foo, *bar, baz();
1940 // only the declarator pops will be passed decls. This is correct;
1941 // we really do need to consider delayed diagnostics from the decl spec
1942 // for each of the different declarations.
1943 for (unsigned I = 0, E = DelayedDiagnostics.size(); I != E; ++I) {
1944 if (DelayedDiagnostics[I].Triggered)
1945 continue;
1946
1947 switch (DelayedDiagnostics[I].Kind) {
1948 case DelayedDiagnostic::Deprecation:
1949 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
1950 break;
1951
1952 case DelayedDiagnostic::Access:
1953 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
1954 break;
1955 }
1956 }
1957 }
1958
1959 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00001960}
1961
1962static bool isDeclDeprecated(Decl *D) {
1963 do {
1964 if (D->hasAttr<DeprecatedAttr>())
1965 return true;
1966 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
1967 return false;
1968}
1969
John McCall86121512010-01-27 03:50:35 +00001970void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
1971 Decl *Ctx) {
1972 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00001973 return;
1974
John McCall86121512010-01-27 03:50:35 +00001975 DD.Triggered = true;
1976 Diag(DD.Loc, diag::warn_deprecated)
1977 << DD.DeprecationData.Decl->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00001978}
1979
1980void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
1981 // Delay if we're currently parsing a declaration.
1982 if (ParsingDeclDepth) {
John McCall86121512010-01-27 03:50:35 +00001983 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall28a6aea2009-11-04 02:18:39 +00001984 return;
1985 }
1986
1987 // Otherwise, don't warn if our current context is deprecated.
1988 if (isDeclDeprecated(cast<Decl>(CurContext)))
1989 return;
1990
1991 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
1992}