blob: aceaafcf3a165b0554fc23ae3da8cc1b787cec2e [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) {
Ted Kremenek79f5e512010-03-26 22:57:10 +0000493 // NOTE: We don't add the attribute to a FunctionDecl because the noreturn
494 // trait will be part of the function's type.
495
John McCall04a67a62010-02-05 21:31:56 +0000496 // Don't apply as a decl attribute to ValueDecl.
497 // FIXME: probably ought to diagnose this.
498 if (isa<ValueDecl>(d))
499 return;
500
Mike Stumpbf916502009-07-24 19:02:52 +0000501 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000502 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000503}
504
505static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
506 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000507 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000508 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000509}
510
Sean Huntbbd37c62009-11-21 08:43:09 +0000511static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
512 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
513 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000514 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000515 return;
516 }
517 // FIXME: Actually store the attribute on the declaration
518}
519
Ted Kremenek73798892008-07-25 04:39:19 +0000520static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
521 // check the attribute arguments.
522 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000524 return;
525 }
Mike Stumpbf916502009-07-24 19:02:52 +0000526
John McCallaec58602010-03-31 02:47:45 +0000527 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
528 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000529 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000530 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000531 return;
532 }
Mike Stumpbf916502009-07-24 19:02:52 +0000533
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000534 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000535}
536
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000537static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
538 // check the attribute arguments.
539 if (Attr.getNumArgs() != 0) {
540 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
541 return;
542 }
Mike Stumpbf916502009-07-24 19:02:52 +0000543
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000544 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000545 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000546 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
547 return;
548 }
549 } else if (!isFunctionOrMethod(d)) {
550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000551 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000552 return;
553 }
Mike Stumpbf916502009-07-24 19:02:52 +0000554
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000555 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000556}
557
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000558static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
559 // check the attribute arguments.
560 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000561 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
562 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000563 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000564 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000565
566 int priority = 65535; // FIXME: Do not hardcode such constants.
567 if (Attr.getNumArgs() > 0) {
568 Expr *E = static_cast<Expr *>(Attr.getArg(0));
569 llvm::APSInt Idx(32);
570 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000571 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000572 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000573 return;
574 }
575 priority = Idx.getZExtValue();
576 }
Mike Stumpbf916502009-07-24 19:02:52 +0000577
Chris Lattnerc5197432009-04-14 17:02:11 +0000578 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000579 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000580 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000581 return;
582 }
583
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000584 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000585}
586
587static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
588 // check the attribute arguments.
589 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000590 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
591 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000592 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000593 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000594
595 int priority = 65535; // FIXME: Do not hardcode such constants.
596 if (Attr.getNumArgs() > 0) {
597 Expr *E = static_cast<Expr *>(Attr.getArg(0));
598 llvm::APSInt Idx(32);
599 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000600 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000601 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000602 return;
603 }
604 priority = Idx.getZExtValue();
605 }
Mike Stumpbf916502009-07-24 19:02:52 +0000606
Anders Carlsson6782fc62008-08-22 22:10:48 +0000607 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000609 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000610 return;
611 }
612
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000613 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000614}
615
Chris Lattner803d0802008-06-29 00:43:07 +0000616static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000617 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000618 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000620 return;
621 }
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000623 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000624}
625
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000626static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
627 // check the attribute arguments.
628 if (Attr.getNumArgs() != 0) {
629 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
630 return;
631 }
Mike Stumpbf916502009-07-24 19:02:52 +0000632
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000633 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000634}
635
Chris Lattner803d0802008-06-29 00:43:07 +0000636static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000637 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000638 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000640 return;
641 }
Mike Stumpbf916502009-07-24 19:02:52 +0000642
Chris Lattner545dd342008-06-28 23:36:30 +0000643 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000644 Arg = Arg->IgnoreParenCasts();
645 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000646
Chris Lattner6b6b5372008-06-26 18:38:35 +0000647 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000650 return;
651 }
Mike Stumpbf916502009-07-24 19:02:52 +0000652
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000653 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000654 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000655
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000656 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000657 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000658 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000659 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000660 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000661 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000662 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000663 type = VisibilityAttr::ProtectedVisibility;
664 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000665 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000666 return;
667 }
Mike Stumpbf916502009-07-24 19:02:52 +0000668
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000669 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000670}
671
Chris Lattner0db29ec2009-02-14 08:09:34 +0000672static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
673 Sema &S) {
674 if (Attr.getNumArgs() != 0) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
676 return;
677 }
Mike Stumpbf916502009-07-24 19:02:52 +0000678
Chris Lattner0db29ec2009-02-14 08:09:34 +0000679 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
680 if (OCI == 0) {
681 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
682 return;
683 }
Mike Stumpbf916502009-07-24 19:02:52 +0000684
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000685 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000686}
687
688static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000689 if (Attr.getNumArgs() != 0) {
690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
691 return;
692 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000693 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000694 QualType T = TD->getUnderlyingType();
695 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000696 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000697 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
698 return;
699 }
700 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000701 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000702}
703
Mike Stumpbf916502009-07-24 19:02:52 +0000704static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000705HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
706 if (Attr.getNumArgs() != 0) {
707 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
708 return;
709 }
710
711 if (!isa<FunctionDecl>(D)) {
712 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
713 return;
714 }
715
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000716 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000717}
718
Steve Naroff9eae5762008-09-18 16:44:58 +0000719static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000720 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000721 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000722 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000723 return;
724 }
Mike Stumpbf916502009-07-24 19:02:52 +0000725
Steve Naroff9eae5762008-09-18 16:44:58 +0000726 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000727 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000728 return;
729 }
Mike Stumpbf916502009-07-24 19:02:52 +0000730
Steve Naroff9eae5762008-09-18 16:44:58 +0000731 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000732 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000733 type = BlocksAttr::ByRef;
734 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000735 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000736 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000737 return;
738 }
Mike Stumpbf916502009-07-24 19:02:52 +0000739
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000740 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000741}
742
Anders Carlsson77091822008-10-05 18:05:59 +0000743static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
744 // check the attribute arguments.
745 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
747 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000748 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000749 }
750
Anders Carlsson77091822008-10-05 18:05:59 +0000751 int sentinel = 0;
752 if (Attr.getNumArgs() > 0) {
753 Expr *E = static_cast<Expr *>(Attr.getArg(0));
754 llvm::APSInt Idx(32);
755 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000756 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000757 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000758 return;
759 }
760 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000761
Anders Carlsson77091822008-10-05 18:05:59 +0000762 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000763 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
764 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000765 return;
766 }
767 }
768
769 int nullPos = 0;
770 if (Attr.getNumArgs() > 1) {
771 Expr *E = static_cast<Expr *>(Attr.getArg(1));
772 llvm::APSInt Idx(32);
773 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000774 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000775 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000776 return;
777 }
778 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000779
Anders Carlsson77091822008-10-05 18:05:59 +0000780 if (nullPos > 1 || nullPos < 0) {
781 // FIXME: This error message could be improved, it would be nice
782 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000783 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
784 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000785 return;
786 }
787 }
788
789 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000790 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000791 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000792
Chris Lattner897cd902009-03-17 23:03:47 +0000793 if (isa<FunctionNoProtoType>(FT)) {
794 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
795 return;
796 }
Mike Stumpbf916502009-07-24 19:02:52 +0000797
Chris Lattner897cd902009-03-17 23:03:47 +0000798 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000799 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000800 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000801 }
Anders Carlsson77091822008-10-05 18:05:59 +0000802 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
803 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000804 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000805 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000806 }
807 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000808 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
809 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000810 ;
811 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000812 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000813 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000814 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000815 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000816 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000817 int m = Ty->isFunctionPointerType() ? 0 : 1;
818 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000819 return;
820 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000821 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000822 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000823 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000824 return;
825 }
Anders Carlsson77091822008-10-05 18:05:59 +0000826 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000827 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000828 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000829 return;
830 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000831 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000832}
833
Chris Lattner026dc962009-02-14 07:37:35 +0000834static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
835 // check the attribute arguments.
836 if (Attr.getNumArgs() != 0) {
837 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
838 return;
839 }
840
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000841 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000843 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000844 return;
845 }
Mike Stumpbf916502009-07-24 19:02:52 +0000846
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000847 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
848 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
849 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +0000850 return;
851 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000852 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
853 if (MD->getResultType()->isVoidType()) {
854 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
855 << Attr.getName() << 1;
856 return;
857 }
858
Nuno Lopesd20254f2009-12-20 23:11:08 +0000859 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000860}
861
862static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000863 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000864 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000865 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000866 return;
867 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000868
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000869 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000870 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000871 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
872 dyn_cast<NamedDecl>(D)->getNameAsString();
873 return;
874 }
875
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000876 // TODO: could also be applied to methods?
877 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
878 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000879 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000880 return;
881 }
Mike Stumpbf916502009-07-24 19:02:52 +0000882
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000883 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000884}
885
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000886static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
887 // check the attribute arguments.
888 if (Attr.getNumArgs() != 0) {
889 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
890 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000891 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000892
893 // weak_import only applies to variable & function declarations.
894 bool isDef = false;
895 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
896 isDef = (!VD->hasExternalStorage() || VD->getInit());
897 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000898 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000899 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
900 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000901 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000902 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanian3be17942010-04-12 16:57:31 +0000903 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
904 !isa<ObjCInterfaceDecl>(D) ||
905 S.Context.Target.getTriple().getDarwinMajorNumber() <= 10) {
906
907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
908 << Attr.getName() << 2 /*variable and function*/;
909 return;
910 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000911 }
912
913 // Merge should handle any subsequent violations.
914 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000915 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000916 diag::warn_attribute_weak_import_invalid_on_definition)
917 << "weak_import" << 2 /*variable and function*/;
918 return;
919 }
920
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000921 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000922}
923
Nate Begeman6f3d8382009-06-26 06:32:41 +0000924static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
925 Sema &S) {
926 // Attribute has 3 arguments.
927 if (Attr.getNumArgs() != 3) {
928 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
929 return;
930 }
931
932 unsigned WGSize[3];
933 for (unsigned i = 0; i < 3; ++i) {
934 Expr *E = static_cast<Expr *>(Attr.getArg(i));
935 llvm::APSInt ArgNum(32);
936 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
937 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
938 << "reqd_work_group_size" << E->getSourceRange();
939 return;
940 }
941 WGSize[i] = (unsigned) ArgNum.getZExtValue();
942 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000943 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000944 WGSize[2]));
945}
946
Chris Lattner026dc962009-02-14 07:37:35 +0000947static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000948 // Attribute has no arguments.
949 if (Attr.getNumArgs() != 1) {
950 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
951 return;
952 }
953
954 // Make sure that there is a string literal as the sections's single
955 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000956 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
957 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000958 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000959 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000960 return;
961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Chris Lattner797c3c42009-08-10 19:03:04 +0000963 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000964 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000965 if (!Error.empty()) {
966 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
967 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000968 return;
969 }
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000971 // This attribute cannot be applied to local variables.
972 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
973 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
974 return;
975 }
976
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000977 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000978}
979
Chris Lattner6b6b5372008-06-26 18:38:35 +0000980
Chris Lattner803d0802008-06-29 00:43:07 +0000981static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000982 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000983 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000984 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000985 return;
986 }
Mike Stumpbf916502009-07-24 19:02:52 +0000987
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000988 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000989}
990
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000991static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
992 // check the attribute arguments.
993 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000994 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000995 return;
996 }
Mike Stumpbf916502009-07-24 19:02:52 +0000997
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000998 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000999}
1000
1001static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1002 // check the attribute arguments.
1003 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001004 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001005 return;
1006 }
Mike Stumpbf916502009-07-24 19:02:52 +00001007
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001008 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001009}
1010
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001011static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001012 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001013 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1014 return;
1015 }
Mike Stumpbf916502009-07-24 19:02:52 +00001016
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001017 if (Attr.getNumArgs() != 0) {
1018 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1019 return;
1020 }
Mike Stumpbf916502009-07-24 19:02:52 +00001021
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001022 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001023
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001024 if (!VD || !VD->hasLocalStorage()) {
1025 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1026 return;
1027 }
Mike Stumpbf916502009-07-24 19:02:52 +00001028
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001029 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001030 NamedDecl *CleanupDecl
1031 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1032 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001033 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001035 Attr.getParameterName();
1036 return;
1037 }
Mike Stumpbf916502009-07-24 19:02:52 +00001038
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001039 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1040 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001041 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001042 Attr.getParameterName();
1043 return;
1044 }
1045
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001046 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001047 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001048 Attr.getParameterName();
1049 return;
1050 }
Mike Stumpbf916502009-07-24 19:02:52 +00001051
Anders Carlsson89941c12009-02-07 23:16:50 +00001052 // We're currently more strict than GCC about what function types we accept.
1053 // If this ever proves to be a problem it should be easy to fix.
1054 QualType Ty = S.Context.getPointerType(VD->getType());
1055 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001056 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001057 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001058 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1059 Attr.getParameterName() << ParamTy << Ty;
1060 return;
1061 }
Mike Stumpbf916502009-07-24 19:02:52 +00001062
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001063 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001064}
1065
Mike Stumpbf916502009-07-24 19:02:52 +00001066/// Handle __attribute__((format_arg((idx)))) attribute based on
1067/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1068static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001069 if (Attr.getNumArgs() != 1) {
1070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1071 return;
1072 }
1073 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1074 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1075 << Attr.getName() << 0 /*function*/;
1076 return;
1077 }
Mike Stumpbf916502009-07-24 19:02:52 +00001078 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1079 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001080 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1081 unsigned FirstIdx = 1;
1082 // checks for the 2nd argument
1083 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1084 llvm::APSInt Idx(32);
1085 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1087 << "format" << 2 << IdxExpr->getSourceRange();
1088 return;
1089 }
Mike Stumpbf916502009-07-24 19:02:52 +00001090
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001091 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1092 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1093 << "format" << 2 << IdxExpr->getSourceRange();
1094 return;
1095 }
Mike Stumpbf916502009-07-24 19:02:52 +00001096
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001097 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001098
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001099 // make sure the format string is really a string
1100 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001101
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001102 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1103 if (not_nsstring_type &&
1104 !isCFStringType(Ty, S.Context) &&
1105 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001106 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001107 // FIXME: Should highlight the actual expression that has the wrong type.
1108 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001109 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001110 << IdxExpr->getSourceRange();
1111 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001112 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001113 Ty = getFunctionOrMethodResultType(d);
1114 if (!isNSStringType(Ty, S.Context) &&
1115 !isCFStringType(Ty, S.Context) &&
1116 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001117 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001118 // FIXME: Should highlight the actual expression that has the wrong type.
1119 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001120 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001121 << IdxExpr->getSourceRange();
1122 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001123 }
1124
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001125 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001126}
1127
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001128enum FormatAttrKind {
1129 CFStringFormat,
1130 NSStringFormat,
1131 StrftimeFormat,
1132 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001133 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001134 InvalidFormat
1135};
1136
1137/// getFormatAttrKind - Map from format attribute names to supported format
1138/// types.
1139static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1140 // Check for formats that get handled specially.
1141 if (Format == "NSString")
1142 return NSStringFormat;
1143 if (Format == "CFString")
1144 return CFStringFormat;
1145 if (Format == "strftime")
1146 return StrftimeFormat;
1147
1148 // Otherwise, check for supported formats.
1149 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1150 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1151 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1152 Format == "zcmn_err")
1153 return SupportedFormat;
1154
Duncan Sandsbc525952010-03-23 14:44:19 +00001155 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1156 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001157 return IgnoredFormat;
1158
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001159 return InvalidFormat;
1160}
1161
Mike Stumpbf916502009-07-24 19:02:52 +00001162/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1163/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001164static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001165
Chris Lattner545dd342008-06-28 23:36:30 +00001166 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001167 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001168 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001169 return;
1170 }
1171
Chris Lattner545dd342008-06-28 23:36:30 +00001172 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001173 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001174 return;
1175 }
1176
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001177 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001178 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001179 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001180 return;
1181 }
1182
Daniel Dunbar35682492008-09-26 04:12:28 +00001183 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001184 unsigned FirstIdx = 1;
1185
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001186 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001187
1188 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001189 if (Format.startswith("__") && Format.endswith("__"))
1190 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001191
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001192 // Check for supported formats.
1193 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001194
1195 if (Kind == IgnoredFormat)
1196 return;
1197
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001198 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001199 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001200 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001201 return;
1202 }
1203
1204 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001205 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001206 llvm::APSInt Idx(32);
1207 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001209 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001210 return;
1211 }
1212
Anders Carlsson4fb77202009-08-25 14:12:34 +00001213 // FIXME: We should handle the implicit 'this' parameter in a more generic
1214 // way that can be used for other arguments.
1215 bool HasImplicitThisParam = false;
1216 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1217 if (MD->isInstance()) {
1218 HasImplicitThisParam = true;
1219 NumArgs++;
1220 }
1221 }
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Chris Lattner6b6b5372008-06-26 18:38:35 +00001223 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001225 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001226 return;
1227 }
1228
1229 // FIXME: Do we need to bounds check?
1230 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001231
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001232 if (HasImplicitThisParam) {
1233 if (ArgIdx == 0) {
1234 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1235 << "a string type" << IdxExpr->getSourceRange();
1236 return;
1237 }
1238 ArgIdx--;
1239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001242 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001243
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001244 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001245 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001246 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1247 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001248 return;
1249 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001250 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001251 // FIXME: do we need to check if the type is NSString*? What are the
1252 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001253 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001254 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001255 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1256 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001257 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001258 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001259 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001260 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001261 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001262 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1263 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001264 return;
1265 }
1266
1267 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001268 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001269 llvm::APSInt FirstArg(32);
1270 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001271 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001272 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001273 return;
1274 }
1275
1276 // check if the function is variadic if the 3rd argument non-zero
1277 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001278 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001279 ++NumArgs; // +1 for ...
1280 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001281 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001282 return;
1283 }
1284 }
1285
Chris Lattner3c73c412008-11-19 08:23:25 +00001286 // strftime requires FirstArg to be 0 because it doesn't read from any
1287 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001288 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001289 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001290 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1291 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001292 return;
1293 }
1294 // if 0 it disables parameter checking (to use with e.g. va_list)
1295 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001296 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001297 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001298 return;
1299 }
1300
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001301 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001302 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001303}
1304
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001305static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1306 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001307 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001308 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001309 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001310 return;
1311 }
1312
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001313 // Try to find the underlying union declaration.
1314 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001315 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001316 if (TD && TD->getUnderlyingType()->isUnionType())
1317 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1318 else
1319 RD = dyn_cast<RecordDecl>(d);
1320
1321 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001322 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001323 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001324 return;
1325 }
1326
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001327 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001328 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001329 diag::warn_transparent_union_attribute_not_definition);
1330 return;
1331 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001332
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001333 RecordDecl::field_iterator Field = RD->field_begin(),
1334 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001335 if (Field == FieldEnd) {
1336 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1337 return;
1338 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001339
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001340 FieldDecl *FirstField = *Field;
1341 QualType FirstType = FirstField->getType();
1342 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001343 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001344 diag::warn_transparent_union_attribute_floating);
1345 return;
1346 }
1347
1348 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1349 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1350 for (; Field != FieldEnd; ++Field) {
1351 QualType FieldType = Field->getType();
1352 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1353 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1354 // Warn if we drop the attribute.
1355 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001356 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001357 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001358 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001359 diag::warn_transparent_union_attribute_field_size_align)
1360 << isSize << Field->getDeclName() << FieldBits;
1361 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001362 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001363 diag::note_transparent_union_first_field_size_align)
1364 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001365 return;
1366 }
1367 }
1368
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001369 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001370}
1371
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001372static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001373 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001374 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001375 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001376 return;
1377 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001378 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1379 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001380
Chris Lattner6b6b5372008-06-26 18:38:35 +00001381 // Make sure that there is a string literal as the annotation's single
1382 // argument.
1383 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001384 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001385 return;
1386 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001387 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001388}
1389
Chris Lattner803d0802008-06-29 00:43:07 +00001390static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001391 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001392 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001393 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001394 return;
1395 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001396
1397 //FIXME: The C++0x version of this attribute has more limited applicabilty
1398 // than GNU's, and should error out when it is used to specify a
1399 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001400
1401 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001402 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001403 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001404 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001405 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001406 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001407 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001408 }
Mike Stumpbf916502009-07-24 19:02:52 +00001409
Chris Lattner49e2d342008-06-28 23:50:44 +00001410 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1411 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001412 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001413 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1414 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001415 return;
1416 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001417 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001418 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001419 << alignmentExpr->getSourceRange();
1420 return;
1421 }
1422
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001423 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001424}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001425
Mike Stumpbf916502009-07-24 19:02:52 +00001426/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1427/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001428///
Mike Stumpbf916502009-07-24 19:02:52 +00001429/// Despite what would be logical, the mode attribute is a decl attribute, not a
1430/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1431/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001432static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001433 // This attribute isn't documented, but glibc uses it. It changes
1434 // the width of an int or unsigned int to the specified size.
1435
1436 // Check that there aren't any arguments
1437 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001439 return;
1440 }
1441
1442 IdentifierInfo *Name = Attr.getParameterName();
1443 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001444 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001445 return;
1446 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001447
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001448 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001449
1450 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001451 if (Str.startswith("__") && Str.endswith("__"))
1452 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001453
1454 unsigned DestWidth = 0;
1455 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001456 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001457 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001458 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001459 switch (Str[0]) {
1460 case 'Q': DestWidth = 8; break;
1461 case 'H': DestWidth = 16; break;
1462 case 'S': DestWidth = 32; break;
1463 case 'D': DestWidth = 64; break;
1464 case 'X': DestWidth = 96; break;
1465 case 'T': DestWidth = 128; break;
1466 }
1467 if (Str[1] == 'F') {
1468 IntegerMode = false;
1469 } else if (Str[1] == 'C') {
1470 IntegerMode = false;
1471 ComplexMode = true;
1472 } else if (Str[1] != 'I') {
1473 DestWidth = 0;
1474 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001475 break;
1476 case 4:
1477 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1478 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001479 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001480 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001481 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001482 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001483 break;
1484 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001485 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001486 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001487 break;
1488 }
1489
1490 QualType OldTy;
1491 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1492 OldTy = TD->getUnderlyingType();
1493 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1494 OldTy = VD->getType();
1495 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001496 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1497 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001498 return;
1499 }
Eli Friedman73397492009-03-03 06:41:03 +00001500
John McCall183700f2009-09-21 23:43:11 +00001501 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001502 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1503 else if (IntegerMode) {
1504 if (!OldTy->isIntegralType())
1505 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1506 } else if (ComplexMode) {
1507 if (!OldTy->isComplexType())
1508 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1509 } else {
1510 if (!OldTy->isFloatingType())
1511 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1512 }
1513
Mike Stump390b4cc2009-05-16 07:39:55 +00001514 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1515 // and friends, at least with glibc.
1516 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1517 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001518 // FIXME: Make sure floating-point mappings are accurate
1519 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001520 QualType NewTy;
1521 switch (DestWidth) {
1522 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001523 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001524 return;
1525 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001526 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001527 return;
1528 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001529 if (!IntegerMode) {
1530 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1531 return;
1532 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001533 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001534 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001535 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001536 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001537 break;
1538 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001539 if (!IntegerMode) {
1540 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1541 return;
1542 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001543 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001544 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001545 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001546 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001547 break;
1548 case 32:
1549 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001550 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001551 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001552 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001553 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001554 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001555 break;
1556 case 64:
1557 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001558 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001559 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001560 if (S.Context.Target.getLongWidth() == 64)
1561 NewTy = S.Context.LongTy;
1562 else
1563 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001564 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001565 if (S.Context.Target.getLongWidth() == 64)
1566 NewTy = S.Context.UnsignedLongTy;
1567 else
1568 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001569 break;
Eli Friedman73397492009-03-03 06:41:03 +00001570 case 96:
1571 NewTy = S.Context.LongDoubleTy;
1572 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001573 case 128:
1574 if (!IntegerMode) {
1575 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1576 return;
1577 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001578 if (OldTy->isSignedIntegerType())
1579 NewTy = S.Context.Int128Ty;
1580 else
1581 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001582 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001583 }
1584
Eli Friedman73397492009-03-03 06:41:03 +00001585 if (ComplexMode) {
1586 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587 }
1588
1589 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001590 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1591 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001592 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001593 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001594 cast<ValueDecl>(D)->setType(NewTy);
1595}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001596
Mike Stump1feade82009-08-26 22:31:08 +00001597static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001598 // check the attribute arguments.
1599 if (Attr.getNumArgs() > 0) {
1600 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1601 return;
1602 }
Anders Carlssone896d982009-02-13 08:11:52 +00001603
Anders Carlsson5bab7882009-02-19 19:16:48 +00001604 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001605 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001606 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001607 return;
1608 }
Mike Stumpbf916502009-07-24 19:02:52 +00001609
Mike Stump1feade82009-08-26 22:31:08 +00001610 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001611}
1612
Mike Stump1feade82009-08-26 22:31:08 +00001613static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001614 // check the attribute arguments.
1615 if (Attr.getNumArgs() != 0) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1617 return;
1618 }
Mike Stumpbf916502009-07-24 19:02:52 +00001619
Chris Lattnerc5197432009-04-14 17:02:11 +00001620 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001621 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001622 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001623 return;
1624 }
Mike Stumpbf916502009-07-24 19:02:52 +00001625
Mike Stump1feade82009-08-26 22:31:08 +00001626 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001627}
1628
Chris Lattnercf2a7212009-04-20 19:12:28 +00001629static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001630 // check the attribute arguments.
1631 if (Attr.getNumArgs() != 0) {
1632 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1633 return;
1634 }
Mike Stumpbf916502009-07-24 19:02:52 +00001635
Chris Lattnerc5197432009-04-14 17:02:11 +00001636 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1637 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001638 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001639 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001640 return;
1641 }
Mike Stumpbf916502009-07-24 19:02:52 +00001642
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001643 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001644 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001645 return;
1646 }
Mike Stumpbf916502009-07-24 19:02:52 +00001647
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001648 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001649}
1650
Fariborz Jahanianee760332009-03-27 18:38:55 +00001651static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1652 // check the attribute arguments.
1653 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001655 return;
1656 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001657
Fariborz Jahanianee760332009-03-27 18:38:55 +00001658 if (!isFunctionOrMethod(d)) {
1659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001660 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001661 return;
1662 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001663
1664 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1665 llvm::APSInt NumParams(32);
1666 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1667 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1668 << "regparm" << NumParamsExpr->getSourceRange();
1669 return;
1670 }
1671
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001672 if (S.Context.Target.getRegParmMax() == 0) {
1673 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001674 << NumParamsExpr->getSourceRange();
1675 return;
1676 }
1677
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001678 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001679 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1680 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001681 return;
1682 }
1683
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001684 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001685}
1686
Sean Huntbbd37c62009-11-21 08:43:09 +00001687static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1688 // check the attribute arguments.
1689 if (Attr.getNumArgs() != 0) {
1690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1691 return;
1692 }
1693
1694 if (!isa<CXXRecordDecl>(d)
1695 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1696 S.Diag(Attr.getLoc(),
1697 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1698 : diag::warn_attribute_wrong_decl_type)
1699 << Attr.getName() << 7 /*virtual method or class*/;
1700 return;
1701 }
Sean Hunt7725e672009-11-25 04:20:27 +00001702
1703 // FIXME: Conform to C++0x redeclaration rules.
1704
1705 if (d->getAttr<FinalAttr>()) {
1706 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1707 return;
1708 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001709
1710 d->addAttr(::new (S.Context) FinalAttr());
1711}
1712
Chris Lattner0744e5f2008-06-29 00:23:49 +00001713//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001714// C++0x member checking attributes
1715//===----------------------------------------------------------------------===//
1716
1717static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1718 if (Attr.getNumArgs() != 0) {
1719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1720 return;
1721 }
1722
1723 if (!isa<CXXRecordDecl>(d)) {
1724 S.Diag(Attr.getLoc(),
1725 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1726 : diag::warn_attribute_wrong_decl_type)
1727 << Attr.getName() << 9 /*class*/;
1728 return;
1729 }
1730
1731 if (d->getAttr<BaseCheckAttr>()) {
1732 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1733 return;
1734 }
1735
1736 d->addAttr(::new (S.Context) BaseCheckAttr());
1737}
1738
1739static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1740 if (Attr.getNumArgs() != 0) {
1741 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1742 return;
1743 }
1744
1745 if (!isa<RecordDecl>(d->getDeclContext())) {
1746 // FIXME: It's not the type that's the problem
1747 S.Diag(Attr.getLoc(),
1748 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1749 : diag::warn_attribute_wrong_decl_type)
1750 << Attr.getName() << 11 /*member*/;
1751 return;
1752 }
1753
1754 // FIXME: Conform to C++0x redeclaration rules.
1755
1756 if (d->getAttr<HidingAttr>()) {
1757 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1758 return;
1759 }
1760
1761 d->addAttr(::new (S.Context) HidingAttr());
1762}
1763
1764static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1765 if (Attr.getNumArgs() != 0) {
1766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1767 return;
1768 }
1769
1770 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1771 // FIXME: It's not the type that's the problem
1772 S.Diag(Attr.getLoc(),
1773 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1774 : diag::warn_attribute_wrong_decl_type)
1775 << Attr.getName() << 10 /*virtual method*/;
1776 return;
1777 }
1778
1779 // FIXME: Conform to C++0x redeclaration rules.
1780
1781 if (d->getAttr<OverrideAttr>()) {
1782 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1783 return;
1784 }
1785
1786 d->addAttr(::new (S.Context) OverrideAttr());
1787}
1788
1789//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001790// Checker-specific attribute handlers.
1791//===----------------------------------------------------------------------===//
1792
1793static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1794 Sema &S) {
1795
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001796 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001797
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001798 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1799 RetTy = MD->getResultType();
1800 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1801 RetTy = FD->getResultType();
1802 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001803 SourceLocation L = Attr.getLoc();
1804 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1805 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001806 return;
1807 }
Mike Stumpbf916502009-07-24 19:02:52 +00001808
Ted Kremenek6217b802009-07-29 21:53:49 +00001809 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001810 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001811 SourceLocation L = Attr.getLoc();
1812 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1813 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001814 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001815 }
Mike Stumpbf916502009-07-24 19:02:52 +00001816
Ted Kremenekb71368d2009-05-09 02:44:38 +00001817 switch (Attr.getKind()) {
1818 default:
1819 assert(0 && "invalid ownership attribute");
1820 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00001821 case AttributeList::AT_cf_returns_not_retained:
1822 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1823 return;
1824 case AttributeList::AT_ns_returns_not_retained:
1825 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1826 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001827 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001828 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001829 return;
1830 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001831 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001832 return;
1833 };
1834}
1835
Charles Davisf0122fe2010-02-16 18:27:26 +00001836static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1837 return Attr.getKind() == AttributeList::AT_dllimport ||
1838 Attr.getKind() == AttributeList::AT_dllexport;
1839}
1840
Ted Kremenekb71368d2009-05-09 02:44:38 +00001841//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001842// Top Level Sema Entry Points
1843//===----------------------------------------------------------------------===//
1844
Sebastian Redla89d82c2008-12-21 19:24:58 +00001845/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001846/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001847/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1848/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001849static void ProcessDeclAttribute(Scope *scope, Decl *D,
1850 const AttributeList &Attr, Sema &S) {
Charles Davisf0122fe2010-02-16 18:27:26 +00001851 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1852 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00001853 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001854 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001855 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
1856 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001857 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001858 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001859 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001860 // Ignore these, these are type attributes, handled by
1861 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001862 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001863 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1864 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001865 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001866 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001867 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001868 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001869 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1870 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001871 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001872 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001873 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1874 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1875 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001876 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001877 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001878 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001879 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1880 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1881 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1882 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1883 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1884 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1885 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1886 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1887 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1888 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1889 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001890
1891 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00001892 case AttributeList::AT_ns_returns_not_retained:
1893 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001894 case AttributeList::AT_ns_returns_retained:
1895 case AttributeList::AT_cf_returns_retained:
1896 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1897
Nate Begeman6f3d8382009-06-26 06:32:41 +00001898 case AttributeList::AT_reqd_wg_size:
1899 HandleReqdWorkGroupSize(D, Attr, S); break;
1900
Sean Hunt7725e672009-11-25 04:20:27 +00001901 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1902 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001903 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1904 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1905 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001906 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001907 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1908 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001909 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001910 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001911 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001912 case AttributeList::AT_transparent_union:
1913 HandleTransparentUnionAttr(D, Attr, S);
1914 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001915 case AttributeList::AT_objc_exception:
1916 HandleObjCExceptionAttr(D, Attr, S);
1917 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001918 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001919 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1920 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1921 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1922 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1923 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1924 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1925 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1926 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1927 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001928 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001929 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001930 // Just ignore
1931 break;
John McCall04a67a62010-02-05 21:31:56 +00001932 case AttributeList::AT_stdcall:
1933 case AttributeList::AT_cdecl:
1934 case AttributeList::AT_fastcall:
1935 // These are all treated as type attributes.
1936 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001937 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001938 // Ask target about the attribute.
1939 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1940 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1941 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001942 break;
1943 }
1944}
1945
1946/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1947/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001948void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001949 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
1950 ProcessDeclAttribute(S, D, *l, *this);
1951 }
1952
1953 // GCC accepts
1954 // static int a9 __attribute__((weakref));
1955 // but that looks really pointless. We reject it.
1956 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
1957 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
1958 dyn_cast<NamedDecl>(D)->getNameAsString();
1959 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001960 }
1961}
1962
Ryan Flynne25ff832009-07-30 03:15:39 +00001963/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1964/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001965NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001966 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001967 NamedDecl *NewD = 0;
1968 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1969 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1970 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001971 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00001972 if (FD->getQualifier()) {
1973 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
1974 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
1975 }
Ryan Flynne25ff832009-07-30 03:15:39 +00001976 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1977 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1978 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001979 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001980 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00001981 if (VD->getQualifier()) {
1982 VarDecl *NewVD = cast<VarDecl>(NewD);
1983 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
1984 }
Ryan Flynne25ff832009-07-30 03:15:39 +00001985 }
1986 return NewD;
1987}
1988
1989/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1990/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001991void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001992 if (W.getUsed()) return; // only do this once
1993 W.setUsed(true);
1994 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1995 IdentifierInfo *NDId = ND->getIdentifier();
1996 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001997 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001998 NewD->addAttr(::new (Context) WeakAttr());
1999 WeakTopLevelDecl.push_back(NewD);
2000 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2001 // to insert Decl at TU scope, sorry.
2002 DeclContext *SavedContext = CurContext;
2003 CurContext = Context.getTranslationUnitDecl();
2004 PushOnScopeChains(NewD, S);
2005 CurContext = SavedContext;
2006 } else { // just add weak to existing
2007 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002008 }
2009}
2010
Chris Lattner0744e5f2008-06-29 00:23:49 +00002011/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2012/// it, apply them to D. This is a bit tricky because PD can have attributes
2013/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002014void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002015 // Handle #pragma weak
2016 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2017 if (ND->hasLinkage()) {
2018 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2019 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002020 // Identifier referenced by #pragma weak before it was declared
2021 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002022 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2023 }
2024 }
2025 }
2026
Chris Lattner0744e5f2008-06-29 00:23:49 +00002027 // Apply decl attributes from the DeclSpec if present.
2028 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002029 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002030
Chris Lattner0744e5f2008-06-29 00:23:49 +00002031 // Walk the declarator structure, applying decl attributes that were in a type
2032 // position to the decl itself. This handles cases like:
2033 // int *__attr__(x)** D;
2034 // when X is a decl attribute.
2035 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2036 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002037 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002038
Chris Lattner0744e5f2008-06-29 00:23:49 +00002039 // Finally, apply any attributes on the decl itself.
2040 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002041 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002042}
John McCall54abf7d2009-11-04 02:18:39 +00002043
2044/// PushParsingDeclaration - Enter a new "scope" of deprecation
2045/// warnings.
2046///
2047/// The state token we use is the start index of this scope
2048/// on the warning stack.
2049Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2050 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002051 return (ParsingDeclStackState) DelayedDiagnostics.size();
2052}
2053
2054void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2055 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2056 ParsingDeclDepth--;
2057
2058 if (DelayedDiagnostics.empty())
2059 return;
2060
2061 unsigned SavedIndex = (unsigned) S;
2062 assert(SavedIndex <= DelayedDiagnostics.size() &&
2063 "saved index is out of bounds");
2064
John McCall58e6f342010-03-16 05:22:47 +00002065 unsigned E = DelayedDiagnostics.size();
2066
John McCall2f514482010-01-27 03:50:35 +00002067 // We only want to actually emit delayed diagnostics when we
2068 // successfully parsed a decl.
2069 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2070 if (D) {
2071 // We really do want to start with 0 here. We get one push for a
2072 // decl spec and another for each declarator; in a decl group like:
2073 // deprecated_typedef foo, *bar, baz();
2074 // only the declarator pops will be passed decls. This is correct;
2075 // we really do need to consider delayed diagnostics from the decl spec
2076 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002077 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002078 if (DelayedDiagnostics[I].Triggered)
2079 continue;
2080
2081 switch (DelayedDiagnostics[I].Kind) {
2082 case DelayedDiagnostic::Deprecation:
2083 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2084 break;
2085
2086 case DelayedDiagnostic::Access:
2087 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2088 break;
2089 }
2090 }
2091 }
2092
John McCall58e6f342010-03-16 05:22:47 +00002093 // Destroy all the delayed diagnostics we're about to pop off.
2094 for (unsigned I = SavedIndex; I != E; ++I)
2095 DelayedDiagnostics[I].destroy();
2096
John McCall2f514482010-01-27 03:50:35 +00002097 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002098}
2099
2100static bool isDeclDeprecated(Decl *D) {
2101 do {
2102 if (D->hasAttr<DeprecatedAttr>())
2103 return true;
2104 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2105 return false;
2106}
2107
John McCall2f514482010-01-27 03:50:35 +00002108void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2109 Decl *Ctx) {
2110 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002111 return;
2112
John McCall2f514482010-01-27 03:50:35 +00002113 DD.Triggered = true;
2114 Diag(DD.Loc, diag::warn_deprecated)
2115 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002116}
2117
2118void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2119 // Delay if we're currently parsing a declaration.
2120 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002121 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002122 return;
2123 }
2124
2125 // Otherwise, don't warn if our current context is deprecated.
2126 if (isDeclDeprecated(cast<Decl>(CurContext)))
2127 return;
2128
2129 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2130}