blob: 01e8fda2ebf162e84820ee07023d3f6d9ed440b9 [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 Kremenek96329d42008-07-15 22:26:48 +0000228static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
229 // 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 Kremenek96329d42008-07-15 22:26:48 +0000235 // The IBOutlet attribute only applies to instance variables of Objective-C
236 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000237 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000238 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000239 else
Ted Kremenek32742602009-02-17 22:20:20 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000241}
242
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000243static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000244 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
245 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000246 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000248 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000249 return;
250 }
Mike Stumpbf916502009-07-24 19:02:52 +0000251
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000252 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000253
254 // The nonnull attribute only applies to pointers.
255 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000256
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000257 for (AttributeList::arg_iterator I=Attr.arg_begin(),
258 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000259
260
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000261 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000262 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000263 llvm::APSInt ArgNum(32);
264 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
266 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000267 return;
268 }
Mike Stumpbf916502009-07-24 19:02:52 +0000269
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000270 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000271
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000272 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000274 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000275 return;
276 }
Mike Stumpbf916502009-07-24 19:02:52 +0000277
Ted Kremenek465172f2008-07-21 22:09:15 +0000278 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000279
280 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000281 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000282 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000283 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000284 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
285 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000286 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000287 }
Mike Stumpbf916502009-07-24 19:02:52 +0000288
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000289 NonNullArgs.push_back(x);
290 }
Mike Stumpbf916502009-07-24 19:02:52 +0000291
292 // If no arguments were specified to __attribute__((nonnull)) then all pointer
293 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000294 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000295 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
296 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000297 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000298 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000299 }
Mike Stumpbf916502009-07-24 19:02:52 +0000300
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000301 if (NonNullArgs.empty()) {
302 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
303 return;
304 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000305 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000306
307 unsigned* start = &NonNullArgs[0];
308 unsigned size = NonNullArgs.size();
309 std::sort(start, start + size);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000310 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000311}
312
Chris Lattner803d0802008-06-29 00:43:07 +0000313static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000314 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000315 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000317 return;
318 }
Mike Stumpbf916502009-07-24 19:02:52 +0000319
Chris Lattner545dd342008-06-28 23:36:30 +0000320 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000321 Arg = Arg->IgnoreParenCasts();
322 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000323
Chris Lattner6b6b5372008-06-26 18:38:35 +0000324 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000326 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000327 return;
328 }
Mike Stumpbf916502009-07-24 19:02:52 +0000329
Chris Lattner6b6b5372008-06-26 18:38:35 +0000330 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000331
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000332 d->addAttr(::new (S.Context) AliasAttr(Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000333}
334
Mike Stumpbf916502009-07-24 19:02:52 +0000335static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000336 Sema &S) {
337 // check the attribute arguments.
338 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000340 return;
341 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000342
Chris Lattnerc5197432009-04-14 17:02:11 +0000343 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000344 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000345 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000346 return;
347 }
Mike Stumpbf916502009-07-24 19:02:52 +0000348
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000349 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000350}
351
Ryan Flynn76168e22009-08-09 20:07:29 +0000352static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
353 // check the attribute arguments.
354 if (Attr.getNumArgs() != 0) {
355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
356 return;
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000360 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000361 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
362 d->addAttr(::new (S.Context) MallocAttr());
363 return;
364 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000365 }
366
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000367 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000368}
369
Ted Kremenekb7252322009-04-10 00:01:14 +0000370static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000371 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000372 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000373 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000375 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000376 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000377
Mike Stump19c30c02009-04-29 19:03:13 +0000378 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
379 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000380 if (VD == 0 || (!VD->getType()->isBlockPointerType()
381 && !VD->getType()->isFunctionPointerType())) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000382 S.Diag(Attr.getLoc(),
383 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
384 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000385 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000386 return false;
387 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000388 }
Mike Stumpbf916502009-07-24 19:02:52 +0000389
Ted Kremenekb7252322009-04-10 00:01:14 +0000390 return true;
391}
392
393static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
John McCall04a67a62010-02-05 21:31:56 +0000394 // Don't apply as a decl attribute to ValueDecl.
395 // FIXME: probably ought to diagnose this.
396 if (isa<ValueDecl>(d))
397 return;
398
Mike Stumpbf916502009-07-24 19:02:52 +0000399 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000400 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000401}
402
403static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
404 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000405 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000406 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000407}
408
Sean Huntbbd37c62009-11-21 08:43:09 +0000409static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
410 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
411 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000412 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000413 return;
414 }
415 // FIXME: Actually store the attribute on the declaration
416}
417
Ted Kremenek73798892008-07-25 04:39:19 +0000418static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
419 // check the attribute arguments.
420 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000422 return;
423 }
Mike Stumpbf916502009-07-24 19:02:52 +0000424
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000425 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000426 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000427 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000428 return;
429 }
Mike Stumpbf916502009-07-24 19:02:52 +0000430
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000431 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000432}
433
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000434static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
435 // check the attribute arguments.
436 if (Attr.getNumArgs() != 0) {
437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
438 return;
439 }
Mike Stumpbf916502009-07-24 19:02:52 +0000440
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000441 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000442 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000443 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
444 return;
445 }
446 } else if (!isFunctionOrMethod(d)) {
447 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000448 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000449 return;
450 }
Mike Stumpbf916502009-07-24 19:02:52 +0000451
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000452 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000453}
454
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000455static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
456 // check the attribute arguments.
457 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
459 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000460 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000461 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000462
463 int priority = 65535; // FIXME: Do not hardcode such constants.
464 if (Attr.getNumArgs() > 0) {
465 Expr *E = static_cast<Expr *>(Attr.getArg(0));
466 llvm::APSInt Idx(32);
467 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000468 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000469 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000470 return;
471 }
472 priority = Idx.getZExtValue();
473 }
Mike Stumpbf916502009-07-24 19:02:52 +0000474
Chris Lattnerc5197432009-04-14 17:02:11 +0000475 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000476 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000477 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000478 return;
479 }
480
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000481 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000482}
483
484static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
485 // check the attribute arguments.
486 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000487 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
488 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000489 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000490 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000491
492 int priority = 65535; // FIXME: Do not hardcode such constants.
493 if (Attr.getNumArgs() > 0) {
494 Expr *E = static_cast<Expr *>(Attr.getArg(0));
495 llvm::APSInt Idx(32);
496 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000497 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000498 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000499 return;
500 }
501 priority = Idx.getZExtValue();
502 }
Mike Stumpbf916502009-07-24 19:02:52 +0000503
Anders Carlsson6782fc62008-08-22 22:10:48 +0000504 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000505 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000506 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000507 return;
508 }
509
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000510 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000511}
512
Chris Lattner803d0802008-06-29 00:43:07 +0000513static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000514 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000515 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000517 return;
518 }
Mike Stumpbf916502009-07-24 19:02:52 +0000519
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000520 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000521}
522
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000523static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
524 // check the attribute arguments.
525 if (Attr.getNumArgs() != 0) {
526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
527 return;
528 }
Mike Stumpbf916502009-07-24 19:02:52 +0000529
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000530 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000531}
532
Chris Lattner803d0802008-06-29 00:43:07 +0000533static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000534 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000535 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000537 return;
538 }
Mike Stumpbf916502009-07-24 19:02:52 +0000539
Chris Lattner545dd342008-06-28 23:36:30 +0000540 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000541 Arg = Arg->IgnoreParenCasts();
542 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000543
Chris Lattner6b6b5372008-06-26 18:38:35 +0000544 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000545 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000546 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000547 return;
548 }
Mike Stumpbf916502009-07-24 19:02:52 +0000549
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000550 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000551 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000552
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000553 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000554 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000555 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000556 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000557 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000558 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000559 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000560 type = VisibilityAttr::ProtectedVisibility;
561 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000562 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000563 return;
564 }
Mike Stumpbf916502009-07-24 19:02:52 +0000565
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000566 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000567}
568
Chris Lattner0db29ec2009-02-14 08:09:34 +0000569static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
570 Sema &S) {
571 if (Attr.getNumArgs() != 0) {
572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
573 return;
574 }
Mike Stumpbf916502009-07-24 19:02:52 +0000575
Chris Lattner0db29ec2009-02-14 08:09:34 +0000576 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
577 if (OCI == 0) {
578 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
579 return;
580 }
Mike Stumpbf916502009-07-24 19:02:52 +0000581
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000582 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000583}
584
585static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000586 if (Attr.getNumArgs() != 0) {
587 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
588 return;
589 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000590 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000591 QualType T = TD->getUnderlyingType();
592 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000593 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000594 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
595 return;
596 }
597 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000598 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000599}
600
Mike Stumpbf916502009-07-24 19:02:52 +0000601static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000602HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
603 if (Attr.getNumArgs() != 0) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
605 return;
606 }
607
608 if (!isa<FunctionDecl>(D)) {
609 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
610 return;
611 }
612
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000613 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000614}
615
Steve Naroff9eae5762008-09-18 16:44:58 +0000616static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000617 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000618 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000619 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000620 return;
621 }
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Steve Naroff9eae5762008-09-18 16:44:58 +0000623 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000625 return;
626 }
Mike Stumpbf916502009-07-24 19:02:52 +0000627
Steve Naroff9eae5762008-09-18 16:44:58 +0000628 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000629 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000630 type = BlocksAttr::ByRef;
631 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000632 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000633 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000634 return;
635 }
Mike Stumpbf916502009-07-24 19:02:52 +0000636
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000637 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000638}
639
Anders Carlsson77091822008-10-05 18:05:59 +0000640static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
641 // check the attribute arguments.
642 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
644 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000645 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000646 }
647
Anders Carlsson77091822008-10-05 18:05:59 +0000648 int sentinel = 0;
649 if (Attr.getNumArgs() > 0) {
650 Expr *E = static_cast<Expr *>(Attr.getArg(0));
651 llvm::APSInt Idx(32);
652 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000653 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000654 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000655 return;
656 }
657 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000658
Anders Carlsson77091822008-10-05 18:05:59 +0000659 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000660 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
661 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000662 return;
663 }
664 }
665
666 int nullPos = 0;
667 if (Attr.getNumArgs() > 1) {
668 Expr *E = static_cast<Expr *>(Attr.getArg(1));
669 llvm::APSInt Idx(32);
670 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000671 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000672 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000673 return;
674 }
675 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000676
Anders Carlsson77091822008-10-05 18:05:59 +0000677 if (nullPos > 1 || nullPos < 0) {
678 // FIXME: This error message could be improved, it would be nice
679 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000680 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
681 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000682 return;
683 }
684 }
685
686 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000687 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000688 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000689
Chris Lattner897cd902009-03-17 23:03:47 +0000690 if (isa<FunctionNoProtoType>(FT)) {
691 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
692 return;
693 }
Mike Stumpbf916502009-07-24 19:02:52 +0000694
Chris Lattner897cd902009-03-17 23:03:47 +0000695 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000696 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000697 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000698 }
Anders Carlsson77091822008-10-05 18:05:59 +0000699 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
700 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000701 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000702 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000703 }
704 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000705 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
706 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000707 ;
708 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000709 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000710 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000711 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000712 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000713 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000714 int m = Ty->isFunctionPointerType() ? 0 : 1;
715 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000716 return;
717 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000718 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000720 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000721 return;
722 }
Anders Carlsson77091822008-10-05 18:05:59 +0000723 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000725 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000726 return;
727 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000728 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000729}
730
Chris Lattner026dc962009-02-14 07:37:35 +0000731static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
732 // check the attribute arguments.
733 if (Attr.getNumArgs() != 0) {
734 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
735 return;
736 }
737
Nuno Lopesd20254f2009-12-20 23:11:08 +0000738 if (!isFunctionOrMethod(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000739 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000740 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000741 return;
742 }
Mike Stumpbf916502009-07-24 19:02:52 +0000743
Nuno Lopesf8577982009-12-22 23:59:52 +0000744 if (getFunctionType(D)->getResultType()->isVoidType()) {
745 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
746 << Attr.getName();
747 return;
748 }
749
Nuno Lopesd20254f2009-12-20 23:11:08 +0000750 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000751}
752
753static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000754 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000755 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000756 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000757 return;
758 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000759
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000760 /* weak only applies to non-static declarations */
761 bool isStatic = false;
762 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
763 isStatic = VD->getStorageClass() == VarDecl::Static;
764 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
765 isStatic = FD->getStorageClass() == FunctionDecl::Static;
766 }
767 if (isStatic) {
768 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
769 dyn_cast<NamedDecl>(D)->getNameAsString();
770 return;
771 }
772
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000773 // TODO: could also be applied to methods?
774 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
775 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000776 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000777 return;
778 }
Mike Stumpbf916502009-07-24 19:02:52 +0000779
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000780 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000781}
782
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000783static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
784 // check the attribute arguments.
785 if (Attr.getNumArgs() != 0) {
786 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
787 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000788 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000789
790 // weak_import only applies to variable & function declarations.
791 bool isDef = false;
792 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
793 isDef = (!VD->hasExternalStorage() || VD->getInit());
794 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000795 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000796 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
797 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000798 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000799 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000800 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000801 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000802 return;
803 }
804
805 // Merge should handle any subsequent violations.
806 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000807 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000808 diag::warn_attribute_weak_import_invalid_on_definition)
809 << "weak_import" << 2 /*variable and function*/;
810 return;
811 }
812
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000813 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000814}
815
Chris Lattner026dc962009-02-14 07:37:35 +0000816static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000817 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000818 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000819 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000820 return;
821 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000822
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000823 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000824 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000825 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000826 return;
827 }
828
Chris Lattner026dc962009-02-14 07:37:35 +0000829 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000830 if (!FD) {
831 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000832 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000833 return;
834 }
835
836 // Currently, the dllimport attribute is ignored for inlined functions.
837 // Warning is emitted.
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000838 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000839 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
840 return;
841 }
842
843 // The attribute is also overridden by a subsequent declaration as dllexport.
844 // Warning is emitted.
845 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
846 nextAttr = nextAttr->getNext()) {
847 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
848 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
849 return;
850 }
851 }
852
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000853 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000854 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
855 return;
856 }
857
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000858 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000859}
860
Chris Lattner026dc962009-02-14 07:37:35 +0000861static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000862 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000863 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000865 return;
866 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000867
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000868 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000869 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000870 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000871 return;
872 }
873
Chris Lattner026dc962009-02-14 07:37:35 +0000874 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000875 if (!FD) {
876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000877 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000878 return;
879 }
880
Mike Stumpbf916502009-07-24 19:02:52 +0000881 // Currently, the dllexport attribute is ignored for inlined functions, unless
882 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000883 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000884 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
886 return;
887 }
888
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000889 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000890}
891
Nate Begeman6f3d8382009-06-26 06:32:41 +0000892static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
893 Sema &S) {
894 // Attribute has 3 arguments.
895 if (Attr.getNumArgs() != 3) {
896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
897 return;
898 }
899
900 unsigned WGSize[3];
901 for (unsigned i = 0; i < 3; ++i) {
902 Expr *E = static_cast<Expr *>(Attr.getArg(i));
903 llvm::APSInt ArgNum(32);
904 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
905 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
906 << "reqd_work_group_size" << E->getSourceRange();
907 return;
908 }
909 WGSize[i] = (unsigned) ArgNum.getZExtValue();
910 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000911 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000912 WGSize[2]));
913}
914
Chris Lattner026dc962009-02-14 07:37:35 +0000915static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000916 // Attribute has no arguments.
917 if (Attr.getNumArgs() != 1) {
918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
919 return;
920 }
921
922 // Make sure that there is a string literal as the sections's single
923 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000924 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
925 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000926 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000927 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000928 return;
929 }
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Chris Lattner797c3c42009-08-10 19:03:04 +0000931 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000932 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000933 if (!Error.empty()) {
934 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
935 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000936 return;
937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000939 // This attribute cannot be applied to local variables.
940 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
941 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
942 return;
943 }
944
945 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000946}
947
Chris Lattner6b6b5372008-06-26 18:38:35 +0000948
Chris Lattner803d0802008-06-29 00:43:07 +0000949static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000950 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000951 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000952 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000953 return;
954 }
Mike Stumpbf916502009-07-24 19:02:52 +0000955
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000956 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000957}
958
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000959static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
960 // check the attribute arguments.
961 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000962 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000963 return;
964 }
Mike Stumpbf916502009-07-24 19:02:52 +0000965
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000966 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000967}
968
969static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
970 // check the attribute arguments.
971 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000972 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000973 return;
974 }
Mike Stumpbf916502009-07-24 19:02:52 +0000975
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000976 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000977}
978
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000979static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000980 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000981 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
982 return;
983 }
Mike Stumpbf916502009-07-24 19:02:52 +0000984
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000985 if (Attr.getNumArgs() != 0) {
986 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
987 return;
988 }
Mike Stumpbf916502009-07-24 19:02:52 +0000989
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000990 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +0000991
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000992 if (!VD || !VD->hasLocalStorage()) {
993 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
994 return;
995 }
Mike Stumpbf916502009-07-24 19:02:52 +0000996
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000997 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +0000998 NamedDecl *CleanupDecl
999 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1000 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001001 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001002 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001003 Attr.getParameterName();
1004 return;
1005 }
Mike Stumpbf916502009-07-24 19:02:52 +00001006
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001007 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1008 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001009 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001010 Attr.getParameterName();
1011 return;
1012 }
1013
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001014 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001015 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001016 Attr.getParameterName();
1017 return;
1018 }
Mike Stumpbf916502009-07-24 19:02:52 +00001019
Anders Carlsson89941c12009-02-07 23:16:50 +00001020 // We're currently more strict than GCC about what function types we accept.
1021 // If this ever proves to be a problem it should be easy to fix.
1022 QualType Ty = S.Context.getPointerType(VD->getType());
1023 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001024 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001025 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001026 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1027 Attr.getParameterName() << ParamTy << Ty;
1028 return;
1029 }
Mike Stumpbf916502009-07-24 19:02:52 +00001030
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001031 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001032}
1033
Mike Stumpbf916502009-07-24 19:02:52 +00001034/// Handle __attribute__((format_arg((idx)))) attribute based on
1035/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1036static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001037 if (Attr.getNumArgs() != 1) {
1038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1039 return;
1040 }
1041 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1042 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1043 << Attr.getName() << 0 /*function*/;
1044 return;
1045 }
Mike Stumpbf916502009-07-24 19:02:52 +00001046 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1047 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001048 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1049 unsigned FirstIdx = 1;
1050 // checks for the 2nd argument
1051 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1052 llvm::APSInt Idx(32);
1053 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1054 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1055 << "format" << 2 << IdxExpr->getSourceRange();
1056 return;
1057 }
Mike Stumpbf916502009-07-24 19:02:52 +00001058
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001059 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1060 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1061 << "format" << 2 << IdxExpr->getSourceRange();
1062 return;
1063 }
Mike Stumpbf916502009-07-24 19:02:52 +00001064
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001065 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001066
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001067 // make sure the format string is really a string
1068 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001069
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001070 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1071 if (not_nsstring_type &&
1072 !isCFStringType(Ty, S.Context) &&
1073 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001074 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001075 // FIXME: Should highlight the actual expression that has the wrong type.
1076 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001077 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001078 << IdxExpr->getSourceRange();
1079 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001080 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001081 Ty = getFunctionOrMethodResultType(d);
1082 if (!isNSStringType(Ty, S.Context) &&
1083 !isCFStringType(Ty, S.Context) &&
1084 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001085 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001086 // FIXME: Should highlight the actual expression that has the wrong type.
1087 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001088 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001089 << IdxExpr->getSourceRange();
1090 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001091 }
1092
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001093 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001094}
1095
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001096enum FormatAttrKind {
1097 CFStringFormat,
1098 NSStringFormat,
1099 StrftimeFormat,
1100 SupportedFormat,
1101 InvalidFormat
1102};
1103
1104/// getFormatAttrKind - Map from format attribute names to supported format
1105/// types.
1106static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1107 // Check for formats that get handled specially.
1108 if (Format == "NSString")
1109 return NSStringFormat;
1110 if (Format == "CFString")
1111 return CFStringFormat;
1112 if (Format == "strftime")
1113 return StrftimeFormat;
1114
1115 // Otherwise, check for supported formats.
1116 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1117 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1118 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1119 Format == "zcmn_err")
1120 return SupportedFormat;
1121
1122 return InvalidFormat;
1123}
1124
Mike Stumpbf916502009-07-24 19:02:52 +00001125/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1126/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001127static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001128
Chris Lattner545dd342008-06-28 23:36:30 +00001129 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001130 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001131 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001132 return;
1133 }
1134
Chris Lattner545dd342008-06-28 23:36:30 +00001135 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001136 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001137 return;
1138 }
1139
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001140 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001141 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001142 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001143 return;
1144 }
1145
Daniel Dunbar35682492008-09-26 04:12:28 +00001146 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001147 unsigned FirstIdx = 1;
1148
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001149 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001150
1151 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001152 if (Format.startswith("__") && Format.endswith("__"))
1153 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001154
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001155 // Check for supported formats.
1156 FormatAttrKind Kind = getFormatAttrKind(Format);
1157 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001158 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001159 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001160 return;
1161 }
1162
1163 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001164 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001165 llvm::APSInt Idx(32);
1166 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001167 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001168 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001169 return;
1170 }
1171
Anders Carlsson4fb77202009-08-25 14:12:34 +00001172 // FIXME: We should handle the implicit 'this' parameter in a more generic
1173 // way that can be used for other arguments.
1174 bool HasImplicitThisParam = false;
1175 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1176 if (MD->isInstance()) {
1177 HasImplicitThisParam = true;
1178 NumArgs++;
1179 }
1180 }
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Chris Lattner6b6b5372008-06-26 18:38:35 +00001182 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001183 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001184 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001185 return;
1186 }
1187
1188 // FIXME: Do we need to bounds check?
1189 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001190
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001191 if (HasImplicitThisParam) {
1192 if (ArgIdx == 0) {
1193 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1194 << "a string type" << IdxExpr->getSourceRange();
1195 return;
1196 }
1197 ArgIdx--;
1198 }
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattner6b6b5372008-06-26 18:38:35 +00001200 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001201 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001202
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001203 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001204 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001205 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1206 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001207 return;
1208 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001209 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001210 // FIXME: do we need to check if the type is NSString*? What are the
1211 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001212 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001213 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001214 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1215 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001216 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001217 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001218 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001219 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001220 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001221 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1222 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001223 return;
1224 }
1225
1226 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001227 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001228 llvm::APSInt FirstArg(32);
1229 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001231 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001232 return;
1233 }
1234
1235 // check if the function is variadic if the 3rd argument non-zero
1236 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001237 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001238 ++NumArgs; // +1 for ...
1239 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001240 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 return;
1242 }
1243 }
1244
Chris Lattner3c73c412008-11-19 08:23:25 +00001245 // strftime requires FirstArg to be 0 because it doesn't read from any
1246 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001247 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001248 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1250 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001251 return;
1252 }
1253 // if 0 it disables parameter checking (to use with e.g. va_list)
1254 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001255 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001256 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001257 return;
1258 }
1259
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001260 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1261 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001262}
1263
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001264static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1265 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001266 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001267 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001269 return;
1270 }
1271
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001272 // Try to find the underlying union declaration.
1273 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001274 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001275 if (TD && TD->getUnderlyingType()->isUnionType())
1276 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1277 else
1278 RD = dyn_cast<RecordDecl>(d);
1279
1280 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001281 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001282 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283 return;
1284 }
1285
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001286 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001287 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001288 diag::warn_transparent_union_attribute_not_definition);
1289 return;
1290 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001291
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001292 RecordDecl::field_iterator Field = RD->field_begin(),
1293 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001294 if (Field == FieldEnd) {
1295 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1296 return;
1297 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001298
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001299 FieldDecl *FirstField = *Field;
1300 QualType FirstType = FirstField->getType();
1301 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001302 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001303 diag::warn_transparent_union_attribute_floating);
1304 return;
1305 }
1306
1307 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1308 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1309 for (; Field != FieldEnd; ++Field) {
1310 QualType FieldType = Field->getType();
1311 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1312 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1313 // Warn if we drop the attribute.
1314 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001315 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001316 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001317 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001318 diag::warn_transparent_union_attribute_field_size_align)
1319 << isSize << Field->getDeclName() << FieldBits;
1320 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001321 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001322 diag::note_transparent_union_first_field_size_align)
1323 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001324 return;
1325 }
1326 }
1327
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001328 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001329}
1330
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001331static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001332 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001333 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001334 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001335 return;
1336 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001337 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1338 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001339
Chris Lattner6b6b5372008-06-26 18:38:35 +00001340 // Make sure that there is a string literal as the annotation's single
1341 // argument.
1342 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001343 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001344 return;
1345 }
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001346 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347}
1348
Chris Lattner803d0802008-06-29 00:43:07 +00001349static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001351 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001352 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001353 return;
1354 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001355
1356 //FIXME: The C++0x version of this attribute has more limited applicabilty
1357 // than GNU's, and should error out when it is used to specify a
1358 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001359
1360 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001361 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001362 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001363 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001364 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001365 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001366 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001367 }
Mike Stumpbf916502009-07-24 19:02:52 +00001368
Chris Lattner49e2d342008-06-28 23:50:44 +00001369 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1370 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001371 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001372 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1373 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001374 return;
1375 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001376 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001377 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001378 << alignmentExpr->getSourceRange();
1379 return;
1380 }
1381
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001382 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001383}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001384
Mike Stumpbf916502009-07-24 19:02:52 +00001385/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1386/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001387///
Mike Stumpbf916502009-07-24 19:02:52 +00001388/// Despite what would be logical, the mode attribute is a decl attribute, not a
1389/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1390/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001391static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001392 // This attribute isn't documented, but glibc uses it. It changes
1393 // the width of an int or unsigned int to the specified size.
1394
1395 // Check that there aren't any arguments
1396 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001397 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001398 return;
1399 }
1400
1401 IdentifierInfo *Name = Attr.getParameterName();
1402 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001403 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001404 return;
1405 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001406
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001407 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001408
1409 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001410 if (Str.startswith("__") && Str.endswith("__"))
1411 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001412
1413 unsigned DestWidth = 0;
1414 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001415 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001416 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001417 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001418 switch (Str[0]) {
1419 case 'Q': DestWidth = 8; break;
1420 case 'H': DestWidth = 16; break;
1421 case 'S': DestWidth = 32; break;
1422 case 'D': DestWidth = 64; break;
1423 case 'X': DestWidth = 96; break;
1424 case 'T': DestWidth = 128; break;
1425 }
1426 if (Str[1] == 'F') {
1427 IntegerMode = false;
1428 } else if (Str[1] == 'C') {
1429 IntegerMode = false;
1430 ComplexMode = true;
1431 } else if (Str[1] != 'I') {
1432 DestWidth = 0;
1433 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001434 break;
1435 case 4:
1436 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1437 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001438 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001439 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001440 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001441 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001442 break;
1443 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001444 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001445 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001446 break;
1447 }
1448
1449 QualType OldTy;
1450 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1451 OldTy = TD->getUnderlyingType();
1452 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1453 OldTy = VD->getType();
1454 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001455 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1456 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001457 return;
1458 }
Eli Friedman73397492009-03-03 06:41:03 +00001459
John McCall183700f2009-09-21 23:43:11 +00001460 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001461 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1462 else if (IntegerMode) {
1463 if (!OldTy->isIntegralType())
1464 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1465 } else if (ComplexMode) {
1466 if (!OldTy->isComplexType())
1467 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1468 } else {
1469 if (!OldTy->isFloatingType())
1470 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1471 }
1472
Mike Stump390b4cc2009-05-16 07:39:55 +00001473 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1474 // and friends, at least with glibc.
1475 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1476 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001477 // FIXME: Make sure floating-point mappings are accurate
1478 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001479 QualType NewTy;
1480 switch (DestWidth) {
1481 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001482 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001483 return;
1484 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001485 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001486 return;
1487 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001488 if (!IntegerMode) {
1489 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1490 return;
1491 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001492 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001493 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001494 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001495 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001496 break;
1497 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001498 if (!IntegerMode) {
1499 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1500 return;
1501 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001502 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001503 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001504 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001505 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001506 break;
1507 case 32:
1508 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001509 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001510 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001511 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001512 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001513 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001514 break;
1515 case 64:
1516 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001517 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001518 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001519 if (S.Context.Target.getLongWidth() == 64)
1520 NewTy = S.Context.LongTy;
1521 else
1522 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001523 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001524 if (S.Context.Target.getLongWidth() == 64)
1525 NewTy = S.Context.UnsignedLongTy;
1526 else
1527 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001528 break;
Eli Friedman73397492009-03-03 06:41:03 +00001529 case 96:
1530 NewTy = S.Context.LongDoubleTy;
1531 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001532 case 128:
1533 if (!IntegerMode) {
1534 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1535 return;
1536 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001537 if (OldTy->isSignedIntegerType())
1538 NewTy = S.Context.Int128Ty;
1539 else
1540 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001541 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001542 }
1543
Eli Friedman73397492009-03-03 06:41:03 +00001544 if (ComplexMode) {
1545 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001546 }
1547
1548 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001549 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1550 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001551 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001552 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001553 cast<ValueDecl>(D)->setType(NewTy);
1554}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001555
Mike Stump1feade82009-08-26 22:31:08 +00001556static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001557 // check the attribute arguments.
1558 if (Attr.getNumArgs() > 0) {
1559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1560 return;
1561 }
Anders Carlssone896d982009-02-13 08:11:52 +00001562
Anders Carlsson5bab7882009-02-19 19:16:48 +00001563 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001564 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001565 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001566 return;
1567 }
Mike Stumpbf916502009-07-24 19:02:52 +00001568
Mike Stump1feade82009-08-26 22:31:08 +00001569 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001570}
1571
Mike Stump1feade82009-08-26 22:31:08 +00001572static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001573 // check the attribute arguments.
1574 if (Attr.getNumArgs() != 0) {
1575 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1576 return;
1577 }
Mike Stumpbf916502009-07-24 19:02:52 +00001578
Chris Lattnerc5197432009-04-14 17:02:11 +00001579 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001580 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001581 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001582 return;
1583 }
Mike Stumpbf916502009-07-24 19:02:52 +00001584
Mike Stump1feade82009-08-26 22:31:08 +00001585 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001586}
1587
Chris Lattnercf2a7212009-04-20 19:12:28 +00001588static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001589 // check the attribute arguments.
1590 if (Attr.getNumArgs() != 0) {
1591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1592 return;
1593 }
Mike Stumpbf916502009-07-24 19:02:52 +00001594
Chris Lattnerc5197432009-04-14 17:02:11 +00001595 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1596 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001598 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001599 return;
1600 }
Mike Stumpbf916502009-07-24 19:02:52 +00001601
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001602 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001603 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001604 return;
1605 }
Mike Stumpbf916502009-07-24 19:02:52 +00001606
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001607 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001608}
1609
Fariborz Jahanianee760332009-03-27 18:38:55 +00001610static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1611 // check the attribute arguments.
1612 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001613 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001614 return;
1615 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001616
Fariborz Jahanianee760332009-03-27 18:38:55 +00001617 if (!isFunctionOrMethod(d)) {
1618 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001619 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001620 return;
1621 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001622
1623 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1624 llvm::APSInt NumParams(32);
1625 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1626 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1627 << "regparm" << NumParamsExpr->getSourceRange();
1628 return;
1629 }
1630
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001631 if (S.Context.Target.getRegParmMax() == 0) {
1632 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001633 << NumParamsExpr->getSourceRange();
1634 return;
1635 }
1636
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001637 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001638 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1639 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001640 return;
1641 }
1642
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001643 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001644}
1645
Sean Huntbbd37c62009-11-21 08:43:09 +00001646static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1647 // check the attribute arguments.
1648 if (Attr.getNumArgs() != 0) {
1649 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1650 return;
1651 }
1652
1653 if (!isa<CXXRecordDecl>(d)
1654 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1655 S.Diag(Attr.getLoc(),
1656 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1657 : diag::warn_attribute_wrong_decl_type)
1658 << Attr.getName() << 7 /*virtual method or class*/;
1659 return;
1660 }
Sean Hunt7725e672009-11-25 04:20:27 +00001661
1662 // FIXME: Conform to C++0x redeclaration rules.
1663
1664 if (d->getAttr<FinalAttr>()) {
1665 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1666 return;
1667 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001668
1669 d->addAttr(::new (S.Context) FinalAttr());
1670}
1671
Chris Lattner0744e5f2008-06-29 00:23:49 +00001672//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001673// C++0x member checking attributes
1674//===----------------------------------------------------------------------===//
1675
1676static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1677 if (Attr.getNumArgs() != 0) {
1678 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1679 return;
1680 }
1681
1682 if (!isa<CXXRecordDecl>(d)) {
1683 S.Diag(Attr.getLoc(),
1684 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1685 : diag::warn_attribute_wrong_decl_type)
1686 << Attr.getName() << 9 /*class*/;
1687 return;
1688 }
1689
1690 if (d->getAttr<BaseCheckAttr>()) {
1691 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1692 return;
1693 }
1694
1695 d->addAttr(::new (S.Context) BaseCheckAttr());
1696}
1697
1698static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1699 if (Attr.getNumArgs() != 0) {
1700 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1701 return;
1702 }
1703
1704 if (!isa<RecordDecl>(d->getDeclContext())) {
1705 // FIXME: It's not the type that's the problem
1706 S.Diag(Attr.getLoc(),
1707 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1708 : diag::warn_attribute_wrong_decl_type)
1709 << Attr.getName() << 11 /*member*/;
1710 return;
1711 }
1712
1713 // FIXME: Conform to C++0x redeclaration rules.
1714
1715 if (d->getAttr<HidingAttr>()) {
1716 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1717 return;
1718 }
1719
1720 d->addAttr(::new (S.Context) HidingAttr());
1721}
1722
1723static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1724 if (Attr.getNumArgs() != 0) {
1725 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1726 return;
1727 }
1728
1729 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1730 // FIXME: It's not the type that's the problem
1731 S.Diag(Attr.getLoc(),
1732 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1733 : diag::warn_attribute_wrong_decl_type)
1734 << Attr.getName() << 10 /*virtual method*/;
1735 return;
1736 }
1737
1738 // FIXME: Conform to C++0x redeclaration rules.
1739
1740 if (d->getAttr<OverrideAttr>()) {
1741 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1742 return;
1743 }
1744
1745 d->addAttr(::new (S.Context) OverrideAttr());
1746}
1747
1748//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001749// Checker-specific attribute handlers.
1750//===----------------------------------------------------------------------===//
1751
1752static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1753 Sema &S) {
1754
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001755 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001756
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001757 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1758 RetTy = MD->getResultType();
1759 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1760 RetTy = FD->getResultType();
1761 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001762 SourceLocation L = Attr.getLoc();
1763 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1764 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001765 return;
1766 }
Mike Stumpbf916502009-07-24 19:02:52 +00001767
Ted Kremenek6217b802009-07-29 21:53:49 +00001768 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001769 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001770 SourceLocation L = Attr.getLoc();
1771 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1772 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001773 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001774 }
Mike Stumpbf916502009-07-24 19:02:52 +00001775
Ted Kremenekb71368d2009-05-09 02:44:38 +00001776 switch (Attr.getKind()) {
1777 default:
1778 assert(0 && "invalid ownership attribute");
1779 return;
1780 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001781 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001782 return;
1783 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001784 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001785 return;
1786 };
1787}
1788
1789//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001790// Top Level Sema Entry Points
1791//===----------------------------------------------------------------------===//
1792
Sebastian Redla89d82c2008-12-21 19:24:58 +00001793/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001794/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001795/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1796/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001797static void ProcessDeclAttribute(Scope *scope, Decl *D,
1798 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001799 if (Attr.isDeclspecAttribute())
1800 // FIXME: Try to deal with __declspec attributes!
1801 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001802 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001803 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001804 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001805 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001806 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001807 // Ignore these, these are type attributes, handled by
1808 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001809 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001810 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1811 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001812 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001813 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001814 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001815 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001816 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1817 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001818 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001819 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001820 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1821 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1822 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1823 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1824 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001825 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001826 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001827 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001828 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1829 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1830 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1831 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1832 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1833 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1834 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1835 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1836 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1837 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1838 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001839
1840 // Checker-specific.
1841 case AttributeList::AT_ns_returns_retained:
1842 case AttributeList::AT_cf_returns_retained:
1843 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1844
Nate Begeman6f3d8382009-06-26 06:32:41 +00001845 case AttributeList::AT_reqd_wg_size:
1846 HandleReqdWorkGroupSize(D, Attr, S); break;
1847
Sean Hunt7725e672009-11-25 04:20:27 +00001848 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1849 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001850 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1851 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1852 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001853 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001854 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1855 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001856 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1857 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001858 case AttributeList::AT_transparent_union:
1859 HandleTransparentUnionAttr(D, Attr, S);
1860 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001861 case AttributeList::AT_objc_exception:
1862 HandleObjCExceptionAttr(D, Attr, S);
1863 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001864 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001865 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1866 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1867 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1868 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1869 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1870 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1871 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1872 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1873 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001874 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001875 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001876 // Just ignore
1877 break;
John McCall04a67a62010-02-05 21:31:56 +00001878 case AttributeList::AT_stdcall:
1879 case AttributeList::AT_cdecl:
1880 case AttributeList::AT_fastcall:
1881 // These are all treated as type attributes.
1882 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001883 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001884 // Ask target about the attribute.
1885 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1886 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1887 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001888 break;
1889 }
1890}
1891
1892/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1893/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001894void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001895 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001896 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001897 AttrList = AttrList->getNext();
1898 }
1899}
1900
Ryan Flynne25ff832009-07-30 03:15:39 +00001901/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1902/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001903NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001904 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001905 NamedDecl *NewD = 0;
1906 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1907 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1908 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001909 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001910 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1911 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1912 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001913 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001914 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001915 }
1916 return NewD;
1917}
1918
1919/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1920/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001921void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001922 if (W.getUsed()) return; // only do this once
1923 W.setUsed(true);
1924 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1925 IdentifierInfo *NDId = ND->getIdentifier();
1926 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1927 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1928 NewD->addAttr(::new (Context) WeakAttr());
1929 WeakTopLevelDecl.push_back(NewD);
1930 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1931 // to insert Decl at TU scope, sorry.
1932 DeclContext *SavedContext = CurContext;
1933 CurContext = Context.getTranslationUnitDecl();
1934 PushOnScopeChains(NewD, S);
1935 CurContext = SavedContext;
1936 } else { // just add weak to existing
1937 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001938 }
1939}
1940
Chris Lattner0744e5f2008-06-29 00:23:49 +00001941/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1942/// it, apply them to D. This is a bit tricky because PD can have attributes
1943/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001944void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001945 // Handle #pragma weak
1946 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1947 if (ND->hasLinkage()) {
1948 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1949 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001950 // Identifier referenced by #pragma weak before it was declared
1951 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001952 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1953 }
1954 }
1955 }
1956
Chris Lattner0744e5f2008-06-29 00:23:49 +00001957 // Apply decl attributes from the DeclSpec if present.
1958 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001959 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001960
Chris Lattner0744e5f2008-06-29 00:23:49 +00001961 // Walk the declarator structure, applying decl attributes that were in a type
1962 // position to the decl itself. This handles cases like:
1963 // int *__attr__(x)** D;
1964 // when X is a decl attribute.
1965 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1966 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001967 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001968
Chris Lattner0744e5f2008-06-29 00:23:49 +00001969 // Finally, apply any attributes on the decl itself.
1970 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001971 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001972}
John McCall54abf7d2009-11-04 02:18:39 +00001973
1974/// PushParsingDeclaration - Enter a new "scope" of deprecation
1975/// warnings.
1976///
1977/// The state token we use is the start index of this scope
1978/// on the warning stack.
1979Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
1980 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00001981 return (ParsingDeclStackState) DelayedDiagnostics.size();
1982}
1983
1984void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
1985 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
1986 ParsingDeclDepth--;
1987
1988 if (DelayedDiagnostics.empty())
1989 return;
1990
1991 unsigned SavedIndex = (unsigned) S;
1992 assert(SavedIndex <= DelayedDiagnostics.size() &&
1993 "saved index is out of bounds");
1994
1995 // We only want to actually emit delayed diagnostics when we
1996 // successfully parsed a decl.
1997 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
1998 if (D) {
1999 // We really do want to start with 0 here. We get one push for a
2000 // decl spec and another for each declarator; in a decl group like:
2001 // deprecated_typedef foo, *bar, baz();
2002 // only the declarator pops will be passed decls. This is correct;
2003 // we really do need to consider delayed diagnostics from the decl spec
2004 // for each of the different declarations.
2005 for (unsigned I = 0, E = DelayedDiagnostics.size(); I != E; ++I) {
2006 if (DelayedDiagnostics[I].Triggered)
2007 continue;
2008
2009 switch (DelayedDiagnostics[I].Kind) {
2010 case DelayedDiagnostic::Deprecation:
2011 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2012 break;
2013
2014 case DelayedDiagnostic::Access:
2015 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2016 break;
2017 }
2018 }
2019 }
2020
2021 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002022}
2023
2024static bool isDeclDeprecated(Decl *D) {
2025 do {
2026 if (D->hasAttr<DeprecatedAttr>())
2027 return true;
2028 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2029 return false;
2030}
2031
John McCall2f514482010-01-27 03:50:35 +00002032void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2033 Decl *Ctx) {
2034 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002035 return;
2036
John McCall2f514482010-01-27 03:50:35 +00002037 DD.Triggered = true;
2038 Diag(DD.Loc, diag::warn_deprecated)
2039 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002040}
2041
2042void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2043 // Delay if we're currently parsing a declaration.
2044 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002045 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002046 return;
2047 }
2048
2049 // Otherwise, don't warn if our current context is deprecated.
2050 if (isDeclDeprecated(cast<Decl>(CurContext)))
2051 return;
2052
2053 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2054}