blob: a81ad768b8c31426b6e25d12e3a9cc55e08a5616 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremeneka18d7d82009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000039
Chris Lattner6b6b5372008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000044
John McCall183700f2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000046}
47
Daniel Dunbar35682492008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopesd20254f2009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000062}
63
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076}
77
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000084 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000106
Chris Lattner89951a82009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000108}
109
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
121 return BD->IsVariadic();
122 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner6b6b5372008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000131
John McCall183700f2009-09-21 23:43:11 +0000132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000133 if (!ClsT)
134 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000135
Chris Lattner6b6b5372008-06-26 18:38:35 +0000136 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000137
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
153 if (RD->getTagKind() != TagDecl::TK_struct)
154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpbf916502009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000172 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 }
Mike Stumpbf916502009-07-24 19:02:52 +0000174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204}
205
Chris Lattner803d0802008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 else
Anders Carlssona860e752009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226}
227
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpbf916502009-07-24 19:02:52 +0000234
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
238 d->addAttr(::new (S.Context) IBActionAttr());
239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000255 d->addAttr(::new (S.Context) IBOutletAttr());
256 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000257 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000260}
261
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000262static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000263 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
264 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000265 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000267 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000268 return;
269 }
Mike Stumpbf916502009-07-24 19:02:52 +0000270
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000271 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000272
273 // The nonnull attribute only applies to pointers.
274 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000275
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000276 for (AttributeList::arg_iterator I=Attr.arg_begin(),
277 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000278
279
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000280 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000281 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000282 llvm::APSInt ArgNum(32);
283 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
285 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000286 return;
287 }
Mike Stumpbf916502009-07-24 19:02:52 +0000288
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000289 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000290
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000291 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000293 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000294 return;
295 }
Mike Stumpbf916502009-07-24 19:02:52 +0000296
Ted Kremenek465172f2008-07-21 22:09:15 +0000297 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000298
299 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000300 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000301 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000302 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000303 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
304 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000305 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000306 }
Mike Stumpbf916502009-07-24 19:02:52 +0000307
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000308 NonNullArgs.push_back(x);
309 }
Mike Stumpbf916502009-07-24 19:02:52 +0000310
311 // If no arguments were specified to __attribute__((nonnull)) then all pointer
312 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000313 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000314 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
315 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000316 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000317 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000318 }
Mike Stumpbf916502009-07-24 19:02:52 +0000319
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000320 if (NonNullArgs.empty()) {
321 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
322 return;
323 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000324 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000325
326 unsigned* start = &NonNullArgs[0];
327 unsigned size = NonNullArgs.size();
328 std::sort(start, start + size);
Ted Kremenek59616112010-02-11 07:31:47 +0000329 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000330}
331
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000332static bool isStaticVarOrStaticFunciton(Decl *D) {
333 if (VarDecl *VD = dyn_cast<VarDecl>(D))
334 return VD->getStorageClass() == VarDecl::Static;
335 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
336 return FD->getStorageClass() == FunctionDecl::Static;
337 return false;
338}
339
340static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
341 // Check the attribute arguments.
342 if (Attr.getNumArgs() > 1) {
343 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
344 return;
345 }
346
347 // gcc rejects
348 // class c {
349 // static int a __attribute__((weakref ("v2")));
350 // static int b() __attribute__((weakref ("f3")));
351 // };
352 // and ignores the attributes of
353 // void f(void) {
354 // static int a __attribute__((weakref ("v2")));
355 // }
356 // we reject them
357 if (const DeclContext *Ctx = d->getDeclContext()) {
358 Ctx = Ctx->getLookupContext();
359 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
360 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
361 dyn_cast<NamedDecl>(d)->getNameAsString();
362 return;
363 }
364 }
365
366 // The GCC manual says
367 //
368 // At present, a declaration to which `weakref' is attached can only
369 // be `static'.
370 //
371 // It also says
372 //
373 // Without a TARGET,
374 // given as an argument to `weakref' or to `alias', `weakref' is
375 // equivalent to `weak'.
376 //
377 // gcc 4.4.1 will accept
378 // int a7 __attribute__((weakref));
379 // as
380 // int a7 __attribute__((weak));
381 // This looks like a bug in gcc. We reject that for now. We should revisit
382 // it if this behaviour is actually used.
383
384 if (!isStaticVarOrStaticFunciton(d)) {
385 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
386 dyn_cast<NamedDecl>(d)->getNameAsString();
387 return;
388 }
389
390 // GCC rejects
391 // static ((alias ("y"), weakref)).
392 // Should we? How to check that weakref is before or after alias?
393
394 if (Attr.getNumArgs() == 1) {
395 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
396 Arg = Arg->IgnoreParenCasts();
397 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
398
399 if (Str == 0 || Str->isWide()) {
400 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
401 << "weakref" << 1;
402 return;
403 }
404 // GCC will accept anything as the argument of weakref. Should we
405 // check for an existing decl?
406 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
407 }
408
409 d->addAttr(::new (S.Context) WeakRefAttr());
410}
411
Chris Lattner803d0802008-06-29 00:43:07 +0000412static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000413 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000414 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000416 return;
417 }
Mike Stumpbf916502009-07-24 19:02:52 +0000418
Chris Lattner545dd342008-06-28 23:36:30 +0000419 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000420 Arg = Arg->IgnoreParenCasts();
421 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000422
Chris Lattner6b6b5372008-06-26 18:38:35 +0000423 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000424 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000425 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000426 return;
427 }
Mike Stumpbf916502009-07-24 19:02:52 +0000428
Chris Lattner6b6b5372008-06-26 18:38:35 +0000429 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000430
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000431 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000432}
433
Mike Stumpbf916502009-07-24 19:02:52 +0000434static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000435 Sema &S) {
436 // check the attribute arguments.
437 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000439 return;
440 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000441
Chris Lattnerc5197432009-04-14 17:02:11 +0000442 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000444 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000445 return;
446 }
Mike Stumpbf916502009-07-24 19:02:52 +0000447
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000448 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000449}
450
Ryan Flynn76168e22009-08-09 20:07:29 +0000451static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
452 // check the attribute arguments.
453 if (Attr.getNumArgs() != 0) {
454 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
455 return;
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000459 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000460 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
461 d->addAttr(::new (S.Context) MallocAttr());
462 return;
463 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000464 }
465
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000466 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000467}
468
Ted Kremenekb7252322009-04-10 00:01:14 +0000469static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000470 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000471 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000472 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000473 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000474 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000475 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000476
Mike Stump19c30c02009-04-29 19:03:13 +0000477 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
478 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000479 if (VD == 0 || (!VD->getType()->isBlockPointerType()
480 && !VD->getType()->isFunctionPointerType())) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000481 S.Diag(Attr.getLoc(),
482 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
483 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000484 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000485 return false;
486 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000487 }
Mike Stumpbf916502009-07-24 19:02:52 +0000488
Ted Kremenekb7252322009-04-10 00:01:14 +0000489 return true;
490}
491
492static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
John McCall04a67a62010-02-05 21:31:56 +0000493 // Don't apply as a decl attribute to ValueDecl.
494 // FIXME: probably ought to diagnose this.
495 if (isa<ValueDecl>(d))
496 return;
497
Mike Stumpbf916502009-07-24 19:02:52 +0000498 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000499 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000500}
501
502static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
503 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000504 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000505 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000506}
507
Sean Huntbbd37c62009-11-21 08:43:09 +0000508static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
509 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000511 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000512 return;
513 }
514 // FIXME: Actually store the attribute on the declaration
515}
516
Ted Kremenek73798892008-07-25 04:39:19 +0000517static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
518 // check the attribute arguments.
519 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000520 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000521 return;
522 }
Mike Stumpbf916502009-07-24 19:02:52 +0000523
Ted Kremenek2ec93a82010-02-25 03:26:51 +0000524 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000525 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000526 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000527 return;
528 }
Mike Stumpbf916502009-07-24 19:02:52 +0000529
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000530 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000531}
532
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000533static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
534 // check the attribute arguments.
535 if (Attr.getNumArgs() != 0) {
536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
537 return;
538 }
Mike Stumpbf916502009-07-24 19:02:52 +0000539
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000540 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000541 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000542 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
543 return;
544 }
545 } else if (!isFunctionOrMethod(d)) {
546 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000547 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000548 return;
549 }
Mike Stumpbf916502009-07-24 19:02:52 +0000550
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000551 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000552}
553
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000554static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
555 // check the attribute arguments.
556 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000557 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
558 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000559 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000560 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000561
562 int priority = 65535; // FIXME: Do not hardcode such constants.
563 if (Attr.getNumArgs() > 0) {
564 Expr *E = static_cast<Expr *>(Attr.getArg(0));
565 llvm::APSInt Idx(32);
566 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000567 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000568 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000569 return;
570 }
571 priority = Idx.getZExtValue();
572 }
Mike Stumpbf916502009-07-24 19:02:52 +0000573
Chris Lattnerc5197432009-04-14 17:02:11 +0000574 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000575 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000576 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000577 return;
578 }
579
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000580 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000581}
582
583static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
584 // check the attribute arguments.
585 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
587 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000588 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000589 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000590
591 int priority = 65535; // FIXME: Do not hardcode such constants.
592 if (Attr.getNumArgs() > 0) {
593 Expr *E = static_cast<Expr *>(Attr.getArg(0));
594 llvm::APSInt Idx(32);
595 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000597 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000598 return;
599 }
600 priority = Idx.getZExtValue();
601 }
Mike Stumpbf916502009-07-24 19:02:52 +0000602
Anders Carlsson6782fc62008-08-22 22:10:48 +0000603 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000604 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000605 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000606 return;
607 }
608
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000609 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000610}
611
Chris Lattner803d0802008-06-29 00:43:07 +0000612static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000613 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000614 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000616 return;
617 }
Mike Stumpbf916502009-07-24 19:02:52 +0000618
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000619 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000620}
621
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000622static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
623 // check the attribute arguments.
624 if (Attr.getNumArgs() != 0) {
625 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
626 return;
627 }
Mike Stumpbf916502009-07-24 19:02:52 +0000628
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000629 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000630}
631
Chris Lattner803d0802008-06-29 00:43:07 +0000632static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000633 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000634 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000636 return;
637 }
Mike Stumpbf916502009-07-24 19:02:52 +0000638
Chris Lattner545dd342008-06-28 23:36:30 +0000639 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000640 Arg = Arg->IgnoreParenCasts();
641 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000642
Chris Lattner6b6b5372008-06-26 18:38:35 +0000643 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000644 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000645 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000646 return;
647 }
Mike Stumpbf916502009-07-24 19:02:52 +0000648
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000649 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000650 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000651
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000652 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000653 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000654 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000655 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000656 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000657 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000658 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000659 type = VisibilityAttr::ProtectedVisibility;
660 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000661 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000662 return;
663 }
Mike Stumpbf916502009-07-24 19:02:52 +0000664
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000665 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000666}
667
Chris Lattner0db29ec2009-02-14 08:09:34 +0000668static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
669 Sema &S) {
670 if (Attr.getNumArgs() != 0) {
671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
672 return;
673 }
Mike Stumpbf916502009-07-24 19:02:52 +0000674
Chris Lattner0db29ec2009-02-14 08:09:34 +0000675 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
676 if (OCI == 0) {
677 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
678 return;
679 }
Mike Stumpbf916502009-07-24 19:02:52 +0000680
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000681 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000682}
683
684static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000685 if (Attr.getNumArgs() != 0) {
686 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
687 return;
688 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000689 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000690 QualType T = TD->getUnderlyingType();
691 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000692 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000693 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
694 return;
695 }
696 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000697 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000698}
699
Mike Stumpbf916502009-07-24 19:02:52 +0000700static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000701HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
702 if (Attr.getNumArgs() != 0) {
703 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
704 return;
705 }
706
707 if (!isa<FunctionDecl>(D)) {
708 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
709 return;
710 }
711
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000712 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000713}
714
Steve Naroff9eae5762008-09-18 16:44:58 +0000715static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000716 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000717 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000718 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000719 return;
720 }
Mike Stumpbf916502009-07-24 19:02:52 +0000721
Steve Naroff9eae5762008-09-18 16:44:58 +0000722 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000723 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000724 return;
725 }
Mike Stumpbf916502009-07-24 19:02:52 +0000726
Steve Naroff9eae5762008-09-18 16:44:58 +0000727 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000728 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000729 type = BlocksAttr::ByRef;
730 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000731 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000732 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000733 return;
734 }
Mike Stumpbf916502009-07-24 19:02:52 +0000735
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000736 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000737}
738
Anders Carlsson77091822008-10-05 18:05:59 +0000739static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
740 // check the attribute arguments.
741 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000742 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
743 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000744 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000745 }
746
Anders Carlsson77091822008-10-05 18:05:59 +0000747 int sentinel = 0;
748 if (Attr.getNumArgs() > 0) {
749 Expr *E = static_cast<Expr *>(Attr.getArg(0));
750 llvm::APSInt Idx(32);
751 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000753 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000754 return;
755 }
756 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000757
Anders Carlsson77091822008-10-05 18:05:59 +0000758 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000759 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
760 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000761 return;
762 }
763 }
764
765 int nullPos = 0;
766 if (Attr.getNumArgs() > 1) {
767 Expr *E = static_cast<Expr *>(Attr.getArg(1));
768 llvm::APSInt Idx(32);
769 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000771 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000772 return;
773 }
774 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000775
Anders Carlsson77091822008-10-05 18:05:59 +0000776 if (nullPos > 1 || nullPos < 0) {
777 // FIXME: This error message could be improved, it would be nice
778 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000779 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
780 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000781 return;
782 }
783 }
784
785 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000786 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000787 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000788
Chris Lattner897cd902009-03-17 23:03:47 +0000789 if (isa<FunctionNoProtoType>(FT)) {
790 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
791 return;
792 }
Mike Stumpbf916502009-07-24 19:02:52 +0000793
Chris Lattner897cd902009-03-17 23:03:47 +0000794 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000795 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000796 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000797 }
Anders Carlsson77091822008-10-05 18:05:59 +0000798 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
799 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000800 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000801 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000802 }
803 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000804 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
805 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000806 ;
807 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000808 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000809 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000810 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000811 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000812 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000813 int m = Ty->isFunctionPointerType() ? 0 : 1;
814 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000815 return;
816 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000817 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000818 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000819 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000820 return;
821 }
Anders Carlsson77091822008-10-05 18:05:59 +0000822 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000823 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000824 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000825 return;
826 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000827 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000828}
829
Chris Lattner026dc962009-02-14 07:37:35 +0000830static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
831 // check the attribute arguments.
832 if (Attr.getNumArgs() != 0) {
833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
834 return;
835 }
836
Ted Kremenek48108fd2010-02-21 05:15:32 +0000837 if (!isFunction(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000838 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000839 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000840 return;
841 }
Mike Stumpbf916502009-07-24 19:02:52 +0000842
Nuno Lopesf8577982009-12-22 23:59:52 +0000843 if (getFunctionType(D)->getResultType()->isVoidType()) {
844 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
845 << Attr.getName();
846 return;
847 }
848
Nuno Lopesd20254f2009-12-20 23:11:08 +0000849 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000850}
851
852static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000853 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000854 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000855 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000856 return;
857 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000858
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000859 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000860 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000861 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
862 dyn_cast<NamedDecl>(D)->getNameAsString();
863 return;
864 }
865
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000866 // TODO: could also be applied to methods?
867 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
868 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000869 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000870 return;
871 }
Mike Stumpbf916502009-07-24 19:02:52 +0000872
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000873 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000874}
875
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000876static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
877 // check the attribute arguments.
878 if (Attr.getNumArgs() != 0) {
879 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
880 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000881 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000882
883 // weak_import only applies to variable & function declarations.
884 bool isDef = false;
885 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
886 isDef = (!VD->hasExternalStorage() || VD->getInit());
887 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000888 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000889 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
890 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000891 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000892 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000894 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000895 return;
896 }
897
898 // Merge should handle any subsequent violations.
899 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000900 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000901 diag::warn_attribute_weak_import_invalid_on_definition)
902 << "weak_import" << 2 /*variable and function*/;
903 return;
904 }
905
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000906 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000907}
908
Nate Begeman6f3d8382009-06-26 06:32:41 +0000909static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
910 Sema &S) {
911 // Attribute has 3 arguments.
912 if (Attr.getNumArgs() != 3) {
913 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
914 return;
915 }
916
917 unsigned WGSize[3];
918 for (unsigned i = 0; i < 3; ++i) {
919 Expr *E = static_cast<Expr *>(Attr.getArg(i));
920 llvm::APSInt ArgNum(32);
921 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
922 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
923 << "reqd_work_group_size" << E->getSourceRange();
924 return;
925 }
926 WGSize[i] = (unsigned) ArgNum.getZExtValue();
927 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000928 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000929 WGSize[2]));
930}
931
Chris Lattner026dc962009-02-14 07:37:35 +0000932static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000933 // Attribute has no arguments.
934 if (Attr.getNumArgs() != 1) {
935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
936 return;
937 }
938
939 // Make sure that there is a string literal as the sections's single
940 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000941 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
942 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000943 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000944 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000945 return;
946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattner797c3c42009-08-10 19:03:04 +0000948 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000949 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000950 if (!Error.empty()) {
951 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
952 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000953 return;
954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000956 // This attribute cannot be applied to local variables.
957 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
958 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
959 return;
960 }
961
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000962 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000963}
964
Chris Lattner6b6b5372008-06-26 18:38:35 +0000965
Chris Lattner803d0802008-06-29 00:43:07 +0000966static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000967 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000968 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000969 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000970 return;
971 }
Mike Stumpbf916502009-07-24 19:02:52 +0000972
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000973 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000974}
975
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000976static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
977 // check the attribute arguments.
978 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000980 return;
981 }
Mike Stumpbf916502009-07-24 19:02:52 +0000982
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000983 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000984}
985
986static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
987 // check the attribute arguments.
988 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000989 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000990 return;
991 }
Mike Stumpbf916502009-07-24 19:02:52 +0000992
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000993 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000994}
995
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000996static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000997 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000998 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
999 return;
1000 }
Mike Stumpbf916502009-07-24 19:02:52 +00001001
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001002 if (Attr.getNumArgs() != 0) {
1003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1004 return;
1005 }
Mike Stumpbf916502009-07-24 19:02:52 +00001006
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001007 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001008
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001009 if (!VD || !VD->hasLocalStorage()) {
1010 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1011 return;
1012 }
Mike Stumpbf916502009-07-24 19:02:52 +00001013
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001014 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001015 NamedDecl *CleanupDecl
1016 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1017 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001018 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001019 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001020 Attr.getParameterName();
1021 return;
1022 }
Mike Stumpbf916502009-07-24 19:02:52 +00001023
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001024 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1025 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001026 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001027 Attr.getParameterName();
1028 return;
1029 }
1030
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001031 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001032 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001033 Attr.getParameterName();
1034 return;
1035 }
Mike Stumpbf916502009-07-24 19:02:52 +00001036
Anders Carlsson89941c12009-02-07 23:16:50 +00001037 // We're currently more strict than GCC about what function types we accept.
1038 // If this ever proves to be a problem it should be easy to fix.
1039 QualType Ty = S.Context.getPointerType(VD->getType());
1040 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001041 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001042 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001043 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1044 Attr.getParameterName() << ParamTy << Ty;
1045 return;
1046 }
Mike Stumpbf916502009-07-24 19:02:52 +00001047
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001048 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001049}
1050
Mike Stumpbf916502009-07-24 19:02:52 +00001051/// Handle __attribute__((format_arg((idx)))) attribute based on
1052/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1053static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001054 if (Attr.getNumArgs() != 1) {
1055 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1056 return;
1057 }
1058 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1059 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1060 << Attr.getName() << 0 /*function*/;
1061 return;
1062 }
Mike Stumpbf916502009-07-24 19:02:52 +00001063 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1064 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001065 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1066 unsigned FirstIdx = 1;
1067 // checks for the 2nd argument
1068 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1069 llvm::APSInt Idx(32);
1070 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1071 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1072 << "format" << 2 << IdxExpr->getSourceRange();
1073 return;
1074 }
Mike Stumpbf916502009-07-24 19:02:52 +00001075
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001076 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1078 << "format" << 2 << IdxExpr->getSourceRange();
1079 return;
1080 }
Mike Stumpbf916502009-07-24 19:02:52 +00001081
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001082 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001083
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001084 // make sure the format string is really a string
1085 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001086
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001087 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1088 if (not_nsstring_type &&
1089 !isCFStringType(Ty, S.Context) &&
1090 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001091 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001092 // FIXME: Should highlight the actual expression that has the wrong type.
1093 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001094 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001095 << IdxExpr->getSourceRange();
1096 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001097 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001098 Ty = getFunctionOrMethodResultType(d);
1099 if (!isNSStringType(Ty, S.Context) &&
1100 !isCFStringType(Ty, S.Context) &&
1101 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001102 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001103 // FIXME: Should highlight the actual expression that has the wrong type.
1104 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001105 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001106 << IdxExpr->getSourceRange();
1107 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001108 }
1109
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001110 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001111}
1112
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001113enum FormatAttrKind {
1114 CFStringFormat,
1115 NSStringFormat,
1116 StrftimeFormat,
1117 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001118 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001119 InvalidFormat
1120};
1121
1122/// getFormatAttrKind - Map from format attribute names to supported format
1123/// types.
1124static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1125 // Check for formats that get handled specially.
1126 if (Format == "NSString")
1127 return NSStringFormat;
1128 if (Format == "CFString")
1129 return CFStringFormat;
1130 if (Format == "strftime")
1131 return StrftimeFormat;
1132
1133 // Otherwise, check for supported formats.
1134 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1135 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1136 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1137 Format == "zcmn_err")
1138 return SupportedFormat;
1139
Duncan Sandsbc525952010-03-23 14:44:19 +00001140 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1141 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001142 return IgnoredFormat;
1143
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001144 return InvalidFormat;
1145}
1146
Mike Stumpbf916502009-07-24 19:02:52 +00001147/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1148/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001149static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001150
Chris Lattner545dd342008-06-28 23:36:30 +00001151 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001153 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001154 return;
1155 }
1156
Chris Lattner545dd342008-06-28 23:36:30 +00001157 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001158 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001159 return;
1160 }
1161
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001162 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001163 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001164 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001165 return;
1166 }
1167
Daniel Dunbar35682492008-09-26 04:12:28 +00001168 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001169 unsigned FirstIdx = 1;
1170
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001171 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001172
1173 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001174 if (Format.startswith("__") && Format.endswith("__"))
1175 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001176
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001177 // Check for supported formats.
1178 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001179
1180 if (Kind == IgnoredFormat)
1181 return;
1182
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001183 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001184 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001185 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001186 return;
1187 }
1188
1189 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001190 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001191 llvm::APSInt Idx(32);
1192 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001193 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001194 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001195 return;
1196 }
1197
Anders Carlsson4fb77202009-08-25 14:12:34 +00001198 // FIXME: We should handle the implicit 'this' parameter in a more generic
1199 // way that can be used for other arguments.
1200 bool HasImplicitThisParam = false;
1201 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1202 if (MD->isInstance()) {
1203 HasImplicitThisParam = true;
1204 NumArgs++;
1205 }
1206 }
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Chris Lattner6b6b5372008-06-26 18:38:35 +00001208 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001209 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001210 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001211 return;
1212 }
1213
1214 // FIXME: Do we need to bounds check?
1215 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001216
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001217 if (HasImplicitThisParam) {
1218 if (ArgIdx == 0) {
1219 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1220 << "a string type" << IdxExpr->getSourceRange();
1221 return;
1222 }
1223 ArgIdx--;
1224 }
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Chris Lattner6b6b5372008-06-26 18:38:35 +00001226 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001227 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001229 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001230 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001231 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1232 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001233 return;
1234 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001235 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001236 // FIXME: do we need to check if the type is NSString*? What are the
1237 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001238 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001239 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001240 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1241 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001242 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001243 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001244 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001245 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001246 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001247 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1248 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001249 return;
1250 }
1251
1252 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001253 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001254 llvm::APSInt FirstArg(32);
1255 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001256 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001257 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001258 return;
1259 }
1260
1261 // check if the function is variadic if the 3rd argument non-zero
1262 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001263 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001264 ++NumArgs; // +1 for ...
1265 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001266 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001267 return;
1268 }
1269 }
1270
Chris Lattner3c73c412008-11-19 08:23:25 +00001271 // strftime requires FirstArg to be 0 because it doesn't read from any
1272 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001273 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001275 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1276 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001277 return;
1278 }
1279 // if 0 it disables parameter checking (to use with e.g. va_list)
1280 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001282 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283 return;
1284 }
1285
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001286 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001287 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001288}
1289
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001290static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1291 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001292 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001293 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001294 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001295 return;
1296 }
1297
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001298 // Try to find the underlying union declaration.
1299 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001300 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001301 if (TD && TD->getUnderlyingType()->isUnionType())
1302 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1303 else
1304 RD = dyn_cast<RecordDecl>(d);
1305
1306 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001307 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001308 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001309 return;
1310 }
1311
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001312 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001313 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001314 diag::warn_transparent_union_attribute_not_definition);
1315 return;
1316 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001317
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001318 RecordDecl::field_iterator Field = RD->field_begin(),
1319 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001320 if (Field == FieldEnd) {
1321 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1322 return;
1323 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001324
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001325 FieldDecl *FirstField = *Field;
1326 QualType FirstType = FirstField->getType();
1327 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001328 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001329 diag::warn_transparent_union_attribute_floating);
1330 return;
1331 }
1332
1333 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1334 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1335 for (; Field != FieldEnd; ++Field) {
1336 QualType FieldType = Field->getType();
1337 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1338 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1339 // Warn if we drop the attribute.
1340 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001341 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001342 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001343 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001344 diag::warn_transparent_union_attribute_field_size_align)
1345 << isSize << Field->getDeclName() << FieldBits;
1346 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001347 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001348 diag::note_transparent_union_first_field_size_align)
1349 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001350 return;
1351 }
1352 }
1353
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001354 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001355}
1356
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001357static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001358 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001359 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001361 return;
1362 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001363 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1364 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001365
Chris Lattner6b6b5372008-06-26 18:38:35 +00001366 // Make sure that there is a string literal as the annotation's single
1367 // argument.
1368 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001369 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001370 return;
1371 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001372 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001373}
1374
Chris Lattner803d0802008-06-29 00:43:07 +00001375static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001376 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001377 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001379 return;
1380 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001381
1382 //FIXME: The C++0x version of this attribute has more limited applicabilty
1383 // than GNU's, and should error out when it is used to specify a
1384 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001385
1386 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001387 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001388 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001389 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001390 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001391 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001392 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001393 }
Mike Stumpbf916502009-07-24 19:02:52 +00001394
Chris Lattner49e2d342008-06-28 23:50:44 +00001395 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1396 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001397 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001398 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1399 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001400 return;
1401 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001402 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001403 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001404 << alignmentExpr->getSourceRange();
1405 return;
1406 }
1407
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001408 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001409}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001410
Mike Stumpbf916502009-07-24 19:02:52 +00001411/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1412/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001413///
Mike Stumpbf916502009-07-24 19:02:52 +00001414/// Despite what would be logical, the mode attribute is a decl attribute, not a
1415/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1416/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001417static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001418 // This attribute isn't documented, but glibc uses it. It changes
1419 // the width of an int or unsigned int to the specified size.
1420
1421 // Check that there aren't any arguments
1422 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001424 return;
1425 }
1426
1427 IdentifierInfo *Name = Attr.getParameterName();
1428 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001430 return;
1431 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001432
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001433 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001434
1435 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001436 if (Str.startswith("__") && Str.endswith("__"))
1437 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001438
1439 unsigned DestWidth = 0;
1440 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001441 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001442 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001443 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001444 switch (Str[0]) {
1445 case 'Q': DestWidth = 8; break;
1446 case 'H': DestWidth = 16; break;
1447 case 'S': DestWidth = 32; break;
1448 case 'D': DestWidth = 64; break;
1449 case 'X': DestWidth = 96; break;
1450 case 'T': DestWidth = 128; break;
1451 }
1452 if (Str[1] == 'F') {
1453 IntegerMode = false;
1454 } else if (Str[1] == 'C') {
1455 IntegerMode = false;
1456 ComplexMode = true;
1457 } else if (Str[1] != 'I') {
1458 DestWidth = 0;
1459 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001460 break;
1461 case 4:
1462 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1463 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001464 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001465 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001466 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001467 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001468 break;
1469 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001470 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001471 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001472 break;
1473 }
1474
1475 QualType OldTy;
1476 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1477 OldTy = TD->getUnderlyingType();
1478 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1479 OldTy = VD->getType();
1480 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001481 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1482 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001483 return;
1484 }
Eli Friedman73397492009-03-03 06:41:03 +00001485
John McCall183700f2009-09-21 23:43:11 +00001486 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001487 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1488 else if (IntegerMode) {
1489 if (!OldTy->isIntegralType())
1490 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1491 } else if (ComplexMode) {
1492 if (!OldTy->isComplexType())
1493 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1494 } else {
1495 if (!OldTy->isFloatingType())
1496 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1497 }
1498
Mike Stump390b4cc2009-05-16 07:39:55 +00001499 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1500 // and friends, at least with glibc.
1501 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1502 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001503 // FIXME: Make sure floating-point mappings are accurate
1504 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001505 QualType NewTy;
1506 switch (DestWidth) {
1507 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001508 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001509 return;
1510 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001511 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001512 return;
1513 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001514 if (!IntegerMode) {
1515 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1516 return;
1517 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001518 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001519 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001520 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001521 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001522 break;
1523 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001524 if (!IntegerMode) {
1525 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1526 return;
1527 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001528 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001529 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001530 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001531 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001532 break;
1533 case 32:
1534 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001535 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001536 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001537 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001538 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001539 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001540 break;
1541 case 64:
1542 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001543 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001544 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001545 if (S.Context.Target.getLongWidth() == 64)
1546 NewTy = S.Context.LongTy;
1547 else
1548 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001549 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001550 if (S.Context.Target.getLongWidth() == 64)
1551 NewTy = S.Context.UnsignedLongTy;
1552 else
1553 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001554 break;
Eli Friedman73397492009-03-03 06:41:03 +00001555 case 96:
1556 NewTy = S.Context.LongDoubleTy;
1557 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001558 case 128:
1559 if (!IntegerMode) {
1560 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1561 return;
1562 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001563 if (OldTy->isSignedIntegerType())
1564 NewTy = S.Context.Int128Ty;
1565 else
1566 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001567 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001568 }
1569
Eli Friedman73397492009-03-03 06:41:03 +00001570 if (ComplexMode) {
1571 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001572 }
1573
1574 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001575 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1576 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001577 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001578 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001579 cast<ValueDecl>(D)->setType(NewTy);
1580}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001581
Mike Stump1feade82009-08-26 22:31:08 +00001582static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001583 // check the attribute arguments.
1584 if (Attr.getNumArgs() > 0) {
1585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1586 return;
1587 }
Anders Carlssone896d982009-02-13 08:11:52 +00001588
Anders Carlsson5bab7882009-02-19 19:16:48 +00001589 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001590 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001591 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001592 return;
1593 }
Mike Stumpbf916502009-07-24 19:02:52 +00001594
Mike Stump1feade82009-08-26 22:31:08 +00001595 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001596}
1597
Mike Stump1feade82009-08-26 22:31:08 +00001598static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001599 // check the attribute arguments.
1600 if (Attr.getNumArgs() != 0) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1602 return;
1603 }
Mike Stumpbf916502009-07-24 19:02:52 +00001604
Chris Lattnerc5197432009-04-14 17:02:11 +00001605 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001607 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001608 return;
1609 }
Mike Stumpbf916502009-07-24 19:02:52 +00001610
Mike Stump1feade82009-08-26 22:31:08 +00001611 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001612}
1613
Chris Lattnercf2a7212009-04-20 19:12:28 +00001614static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001615 // check the attribute arguments.
1616 if (Attr.getNumArgs() != 0) {
1617 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1618 return;
1619 }
Mike Stumpbf916502009-07-24 19:02:52 +00001620
Chris Lattnerc5197432009-04-14 17:02:11 +00001621 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1622 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001623 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001624 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001625 return;
1626 }
Mike Stumpbf916502009-07-24 19:02:52 +00001627
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001628 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001629 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001630 return;
1631 }
Mike Stumpbf916502009-07-24 19:02:52 +00001632
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001633 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001634}
1635
Fariborz Jahanianee760332009-03-27 18:38:55 +00001636static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1637 // check the attribute arguments.
1638 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001640 return;
1641 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001642
Fariborz Jahanianee760332009-03-27 18:38:55 +00001643 if (!isFunctionOrMethod(d)) {
1644 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001645 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001646 return;
1647 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001648
1649 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1650 llvm::APSInt NumParams(32);
1651 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1652 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1653 << "regparm" << NumParamsExpr->getSourceRange();
1654 return;
1655 }
1656
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001657 if (S.Context.Target.getRegParmMax() == 0) {
1658 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001659 << NumParamsExpr->getSourceRange();
1660 return;
1661 }
1662
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001663 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001664 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1665 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001666 return;
1667 }
1668
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001669 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001670}
1671
Sean Huntbbd37c62009-11-21 08:43:09 +00001672static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1673 // check the attribute arguments.
1674 if (Attr.getNumArgs() != 0) {
1675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1676 return;
1677 }
1678
1679 if (!isa<CXXRecordDecl>(d)
1680 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1681 S.Diag(Attr.getLoc(),
1682 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1683 : diag::warn_attribute_wrong_decl_type)
1684 << Attr.getName() << 7 /*virtual method or class*/;
1685 return;
1686 }
Sean Hunt7725e672009-11-25 04:20:27 +00001687
1688 // FIXME: Conform to C++0x redeclaration rules.
1689
1690 if (d->getAttr<FinalAttr>()) {
1691 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1692 return;
1693 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001694
1695 d->addAttr(::new (S.Context) FinalAttr());
1696}
1697
Chris Lattner0744e5f2008-06-29 00:23:49 +00001698//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001699// C++0x member checking attributes
1700//===----------------------------------------------------------------------===//
1701
1702static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1703 if (Attr.getNumArgs() != 0) {
1704 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1705 return;
1706 }
1707
1708 if (!isa<CXXRecordDecl>(d)) {
1709 S.Diag(Attr.getLoc(),
1710 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1711 : diag::warn_attribute_wrong_decl_type)
1712 << Attr.getName() << 9 /*class*/;
1713 return;
1714 }
1715
1716 if (d->getAttr<BaseCheckAttr>()) {
1717 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1718 return;
1719 }
1720
1721 d->addAttr(::new (S.Context) BaseCheckAttr());
1722}
1723
1724static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1725 if (Attr.getNumArgs() != 0) {
1726 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1727 return;
1728 }
1729
1730 if (!isa<RecordDecl>(d->getDeclContext())) {
1731 // FIXME: It's not the type that's the problem
1732 S.Diag(Attr.getLoc(),
1733 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1734 : diag::warn_attribute_wrong_decl_type)
1735 << Attr.getName() << 11 /*member*/;
1736 return;
1737 }
1738
1739 // FIXME: Conform to C++0x redeclaration rules.
1740
1741 if (d->getAttr<HidingAttr>()) {
1742 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1743 return;
1744 }
1745
1746 d->addAttr(::new (S.Context) HidingAttr());
1747}
1748
1749static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1750 if (Attr.getNumArgs() != 0) {
1751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1752 return;
1753 }
1754
1755 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1756 // FIXME: It's not the type that's the problem
1757 S.Diag(Attr.getLoc(),
1758 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1759 : diag::warn_attribute_wrong_decl_type)
1760 << Attr.getName() << 10 /*virtual method*/;
1761 return;
1762 }
1763
1764 // FIXME: Conform to C++0x redeclaration rules.
1765
1766 if (d->getAttr<OverrideAttr>()) {
1767 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1768 return;
1769 }
1770
1771 d->addAttr(::new (S.Context) OverrideAttr());
1772}
1773
1774//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001775// Checker-specific attribute handlers.
1776//===----------------------------------------------------------------------===//
1777
1778static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1779 Sema &S) {
1780
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001781 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001782
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001783 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1784 RetTy = MD->getResultType();
1785 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1786 RetTy = FD->getResultType();
1787 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001788 SourceLocation L = Attr.getLoc();
1789 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1790 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001791 return;
1792 }
Mike Stumpbf916502009-07-24 19:02:52 +00001793
Ted Kremenek6217b802009-07-29 21:53:49 +00001794 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001795 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001796 SourceLocation L = Attr.getLoc();
1797 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1798 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001799 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001800 }
Mike Stumpbf916502009-07-24 19:02:52 +00001801
Ted Kremenekb71368d2009-05-09 02:44:38 +00001802 switch (Attr.getKind()) {
1803 default:
1804 assert(0 && "invalid ownership attribute");
1805 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00001806 case AttributeList::AT_cf_returns_not_retained:
1807 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1808 return;
1809 case AttributeList::AT_ns_returns_not_retained:
1810 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1811 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001812 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001813 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001814 return;
1815 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001816 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001817 return;
1818 };
1819}
1820
Charles Davisf0122fe2010-02-16 18:27:26 +00001821static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1822 return Attr.getKind() == AttributeList::AT_dllimport ||
1823 Attr.getKind() == AttributeList::AT_dllexport;
1824}
1825
Ted Kremenekb71368d2009-05-09 02:44:38 +00001826//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001827// Top Level Sema Entry Points
1828//===----------------------------------------------------------------------===//
1829
Sebastian Redla89d82c2008-12-21 19:24:58 +00001830/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001831/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001832/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1833/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001834static void ProcessDeclAttribute(Scope *scope, Decl *D,
1835 const AttributeList &Attr, Sema &S) {
Charles Davisf0122fe2010-02-16 18:27:26 +00001836 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1837 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00001838 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001839 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001840 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
1841 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001842 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001843 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001844 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001845 // Ignore these, these are type attributes, handled by
1846 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001847 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001848 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1849 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001850 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001851 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001852 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001853 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001854 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1855 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001856 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001857 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001858 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1859 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1860 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001861 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001862 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001863 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001864 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1865 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1866 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1867 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1868 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1869 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1870 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1871 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1872 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1873 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1874 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001875
1876 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00001877 case AttributeList::AT_ns_returns_not_retained:
1878 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001879 case AttributeList::AT_ns_returns_retained:
1880 case AttributeList::AT_cf_returns_retained:
1881 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1882
Nate Begeman6f3d8382009-06-26 06:32:41 +00001883 case AttributeList::AT_reqd_wg_size:
1884 HandleReqdWorkGroupSize(D, Attr, S); break;
1885
Sean Hunt7725e672009-11-25 04:20:27 +00001886 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1887 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001888 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1889 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1890 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001891 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001892 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1893 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001894 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001895 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001896 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001897 case AttributeList::AT_transparent_union:
1898 HandleTransparentUnionAttr(D, Attr, S);
1899 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001900 case AttributeList::AT_objc_exception:
1901 HandleObjCExceptionAttr(D, Attr, S);
1902 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001903 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001904 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1905 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1906 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1907 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1908 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1909 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1910 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1911 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1912 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001913 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001914 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001915 // Just ignore
1916 break;
John McCall04a67a62010-02-05 21:31:56 +00001917 case AttributeList::AT_stdcall:
1918 case AttributeList::AT_cdecl:
1919 case AttributeList::AT_fastcall:
1920 // These are all treated as type attributes.
1921 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001922 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001923 // Ask target about the attribute.
1924 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1925 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1926 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001927 break;
1928 }
1929}
1930
1931/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1932/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001933void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001934 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
1935 ProcessDeclAttribute(S, D, *l, *this);
1936 }
1937
1938 // GCC accepts
1939 // static int a9 __attribute__((weakref));
1940 // but that looks really pointless. We reject it.
1941 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
1942 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
1943 dyn_cast<NamedDecl>(D)->getNameAsString();
1944 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001945 }
1946}
1947
Ryan Flynne25ff832009-07-30 03:15:39 +00001948/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1949/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001950NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001951 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001952 NamedDecl *NewD = 0;
1953 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1954 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1955 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001956 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00001957 if (FD->getQualifier()) {
1958 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
1959 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
1960 }
Ryan Flynne25ff832009-07-30 03:15:39 +00001961 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1962 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1963 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001964 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001965 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00001966 if (VD->getQualifier()) {
1967 VarDecl *NewVD = cast<VarDecl>(NewD);
1968 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
1969 }
Ryan Flynne25ff832009-07-30 03:15:39 +00001970 }
1971 return NewD;
1972}
1973
1974/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1975/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001976void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001977 if (W.getUsed()) return; // only do this once
1978 W.setUsed(true);
1979 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1980 IdentifierInfo *NDId = ND->getIdentifier();
1981 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001982 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001983 NewD->addAttr(::new (Context) WeakAttr());
1984 WeakTopLevelDecl.push_back(NewD);
1985 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1986 // to insert Decl at TU scope, sorry.
1987 DeclContext *SavedContext = CurContext;
1988 CurContext = Context.getTranslationUnitDecl();
1989 PushOnScopeChains(NewD, S);
1990 CurContext = SavedContext;
1991 } else { // just add weak to existing
1992 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001993 }
1994}
1995
Chris Lattner0744e5f2008-06-29 00:23:49 +00001996/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1997/// it, apply them to D. This is a bit tricky because PD can have attributes
1998/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001999void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002000 // Handle #pragma weak
2001 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2002 if (ND->hasLinkage()) {
2003 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2004 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002005 // Identifier referenced by #pragma weak before it was declared
2006 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002007 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2008 }
2009 }
2010 }
2011
Chris Lattner0744e5f2008-06-29 00:23:49 +00002012 // Apply decl attributes from the DeclSpec if present.
2013 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002014 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002015
Chris Lattner0744e5f2008-06-29 00:23:49 +00002016 // Walk the declarator structure, applying decl attributes that were in a type
2017 // position to the decl itself. This handles cases like:
2018 // int *__attr__(x)** D;
2019 // when X is a decl attribute.
2020 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2021 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002022 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002023
Chris Lattner0744e5f2008-06-29 00:23:49 +00002024 // Finally, apply any attributes on the decl itself.
2025 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002026 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002027}
John McCall54abf7d2009-11-04 02:18:39 +00002028
2029/// PushParsingDeclaration - Enter a new "scope" of deprecation
2030/// warnings.
2031///
2032/// The state token we use is the start index of this scope
2033/// on the warning stack.
2034Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2035 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002036 return (ParsingDeclStackState) DelayedDiagnostics.size();
2037}
2038
2039void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2040 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2041 ParsingDeclDepth--;
2042
2043 if (DelayedDiagnostics.empty())
2044 return;
2045
2046 unsigned SavedIndex = (unsigned) S;
2047 assert(SavedIndex <= DelayedDiagnostics.size() &&
2048 "saved index is out of bounds");
2049
John McCall58e6f342010-03-16 05:22:47 +00002050 unsigned E = DelayedDiagnostics.size();
2051
John McCall2f514482010-01-27 03:50:35 +00002052 // We only want to actually emit delayed diagnostics when we
2053 // successfully parsed a decl.
2054 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2055 if (D) {
2056 // We really do want to start with 0 here. We get one push for a
2057 // decl spec and another for each declarator; in a decl group like:
2058 // deprecated_typedef foo, *bar, baz();
2059 // only the declarator pops will be passed decls. This is correct;
2060 // we really do need to consider delayed diagnostics from the decl spec
2061 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002062 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002063 if (DelayedDiagnostics[I].Triggered)
2064 continue;
2065
2066 switch (DelayedDiagnostics[I].Kind) {
2067 case DelayedDiagnostic::Deprecation:
2068 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2069 break;
2070
2071 case DelayedDiagnostic::Access:
2072 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2073 break;
2074 }
2075 }
2076 }
2077
John McCall58e6f342010-03-16 05:22:47 +00002078 // Destroy all the delayed diagnostics we're about to pop off.
2079 for (unsigned I = SavedIndex; I != E; ++I)
2080 DelayedDiagnostics[I].destroy();
2081
John McCall2f514482010-01-27 03:50:35 +00002082 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002083}
2084
2085static bool isDeclDeprecated(Decl *D) {
2086 do {
2087 if (D->hasAttr<DeprecatedAttr>())
2088 return true;
2089 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2090 return false;
2091}
2092
John McCall2f514482010-01-27 03:50:35 +00002093void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2094 Decl *Ctx) {
2095 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002096 return;
2097
John McCall2f514482010-01-27 03:50:35 +00002098 DD.Triggered = true;
2099 Diag(DD.Loc, diag::warn_deprecated)
2100 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002101}
2102
2103void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2104 // Delay if we're currently parsing a declaration.
2105 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002106 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002107 return;
2108 }
2109
2110 // Otherwise, don't warn if our current context is deprecated.
2111 if (isDeclDeprecated(cast<Decl>(CurContext)))
2112 return;
2113
2114 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2115}