blob: 1a12208e5a94155f7b01d23f84d20ae274722cc6 [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) {
Mike Stumpbf916502009-07-24 19:02:52 +0000394 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000395 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000396}
397
398static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
399 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000400 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000401 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000402}
403
Sean Huntbbd37c62009-11-21 08:43:09 +0000404static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
405 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
407 << Attr.getName() << 8; /*function, method, or parameter*/
408 return;
409 }
410 // FIXME: Actually store the attribute on the declaration
411}
412
Ted Kremenek73798892008-07-25 04:39:19 +0000413static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
414 // check the attribute arguments.
415 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000416 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000417 return;
418 }
Mike Stumpbf916502009-07-24 19:02:52 +0000419
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000420 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000421 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000422 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000423 return;
424 }
Mike Stumpbf916502009-07-24 19:02:52 +0000425
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000426 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000427}
428
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000429static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
430 // check the attribute arguments.
431 if (Attr.getNumArgs() != 0) {
432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
433 return;
434 }
Mike Stumpbf916502009-07-24 19:02:52 +0000435
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000436 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000437 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000438 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
439 return;
440 }
441 } else if (!isFunctionOrMethod(d)) {
442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000443 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000444 return;
445 }
Mike Stumpbf916502009-07-24 19:02:52 +0000446
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000447 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000448}
449
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000450static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
451 // check the attribute arguments.
452 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
454 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000455 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000456 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000457
458 int priority = 65535; // FIXME: Do not hardcode such constants.
459 if (Attr.getNumArgs() > 0) {
460 Expr *E = static_cast<Expr *>(Attr.getArg(0));
461 llvm::APSInt Idx(32);
462 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000464 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000465 return;
466 }
467 priority = Idx.getZExtValue();
468 }
Mike Stumpbf916502009-07-24 19:02:52 +0000469
Chris Lattnerc5197432009-04-14 17:02:11 +0000470 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000472 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000473 return;
474 }
475
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000476 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000477}
478
479static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
480 // check the attribute arguments.
481 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000482 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
483 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000484 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000485 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000486
487 int priority = 65535; // FIXME: Do not hardcode such constants.
488 if (Attr.getNumArgs() > 0) {
489 Expr *E = static_cast<Expr *>(Attr.getArg(0));
490 llvm::APSInt Idx(32);
491 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000492 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000493 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000494 return;
495 }
496 priority = Idx.getZExtValue();
497 }
Mike Stumpbf916502009-07-24 19:02:52 +0000498
Anders Carlsson6782fc62008-08-22 22:10:48 +0000499 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000500 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000501 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000502 return;
503 }
504
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000505 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000506}
507
Chris Lattner803d0802008-06-29 00:43:07 +0000508static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000509 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000510 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000512 return;
513 }
Mike Stumpbf916502009-07-24 19:02:52 +0000514
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000515 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000516}
517
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000518static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
519 // check the attribute arguments.
520 if (Attr.getNumArgs() != 0) {
521 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
522 return;
523 }
Mike Stumpbf916502009-07-24 19:02:52 +0000524
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000525 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000526}
527
Chris Lattner803d0802008-06-29 00:43:07 +0000528static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000529 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000530 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000531 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000532 return;
533 }
Mike Stumpbf916502009-07-24 19:02:52 +0000534
Chris Lattner545dd342008-06-28 23:36:30 +0000535 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000536 Arg = Arg->IgnoreParenCasts();
537 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000538
Chris Lattner6b6b5372008-06-26 18:38:35 +0000539 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000541 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000542 return;
543 }
Mike Stumpbf916502009-07-24 19:02:52 +0000544
Chris Lattner6b6b5372008-06-26 18:38:35 +0000545 const char *TypeStr = Str->getStrData();
546 unsigned TypeLen = Str->getByteLength();
547 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000548
Chris Lattner6b6b5372008-06-26 18:38:35 +0000549 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
550 type = VisibilityAttr::DefaultVisibility;
551 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
552 type = VisibilityAttr::HiddenVisibility;
553 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
554 type = VisibilityAttr::HiddenVisibility; // FIXME
555 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
556 type = VisibilityAttr::ProtectedVisibility;
557 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000558 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000559 return;
560 }
Mike Stumpbf916502009-07-24 19:02:52 +0000561
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000562 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000563}
564
Chris Lattner0db29ec2009-02-14 08:09:34 +0000565static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
566 Sema &S) {
567 if (Attr.getNumArgs() != 0) {
568 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
569 return;
570 }
Mike Stumpbf916502009-07-24 19:02:52 +0000571
Chris Lattner0db29ec2009-02-14 08:09:34 +0000572 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
573 if (OCI == 0) {
574 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
575 return;
576 }
Mike Stumpbf916502009-07-24 19:02:52 +0000577
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000578 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000579}
580
581static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000582 if (Attr.getNumArgs() != 0) {
583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
584 return;
585 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000586 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000587 QualType T = TD->getUnderlyingType();
588 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000589 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000590 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
591 return;
592 }
593 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000594 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000595}
596
Mike Stumpbf916502009-07-24 19:02:52 +0000597static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000598HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
599 if (Attr.getNumArgs() != 0) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
601 return;
602 }
603
604 if (!isa<FunctionDecl>(D)) {
605 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
606 return;
607 }
608
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000609 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000610}
611
Steve Naroff9eae5762008-09-18 16:44:58 +0000612static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000613 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000614 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000615 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000616 return;
617 }
Mike Stumpbf916502009-07-24 19:02:52 +0000618
Steve Naroff9eae5762008-09-18 16:44:58 +0000619 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000621 return;
622 }
Mike Stumpbf916502009-07-24 19:02:52 +0000623
Steve Naroff9eae5762008-09-18 16:44:58 +0000624 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000625 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000626 type = BlocksAttr::ByRef;
627 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000628 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000629 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000630 return;
631 }
Mike Stumpbf916502009-07-24 19:02:52 +0000632
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000633 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000634}
635
Anders Carlsson77091822008-10-05 18:05:59 +0000636static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
637 // check the attribute arguments.
638 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
640 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000641 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000642 }
643
Anders Carlsson77091822008-10-05 18:05:59 +0000644 int sentinel = 0;
645 if (Attr.getNumArgs() > 0) {
646 Expr *E = static_cast<Expr *>(Attr.getArg(0));
647 llvm::APSInt Idx(32);
648 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000649 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000650 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000651 return;
652 }
653 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000654
Anders Carlsson77091822008-10-05 18:05:59 +0000655 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
657 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000658 return;
659 }
660 }
661
662 int nullPos = 0;
663 if (Attr.getNumArgs() > 1) {
664 Expr *E = static_cast<Expr *>(Attr.getArg(1));
665 llvm::APSInt Idx(32);
666 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000667 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000668 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000669 return;
670 }
671 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000672
Anders Carlsson77091822008-10-05 18:05:59 +0000673 if (nullPos > 1 || nullPos < 0) {
674 // FIXME: This error message could be improved, it would be nice
675 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000676 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
677 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000678 return;
679 }
680 }
681
682 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000683 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000684 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000685
Chris Lattner897cd902009-03-17 23:03:47 +0000686 if (isa<FunctionNoProtoType>(FT)) {
687 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
688 return;
689 }
Mike Stumpbf916502009-07-24 19:02:52 +0000690
Chris Lattner897cd902009-03-17 23:03:47 +0000691 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000692 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000693 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000694 }
Anders Carlsson77091822008-10-05 18:05:59 +0000695 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
696 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000697 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000698 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000699 }
700 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000701 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
702 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000703 ;
704 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000705 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000706 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000707 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000708 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000709 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000710 int m = Ty->isFunctionPointerType() ? 0 : 1;
711 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000712 return;
713 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000714 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000716 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000717 return;
718 }
Anders Carlsson77091822008-10-05 18:05:59 +0000719 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000720 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000721 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000722 return;
723 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000724 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000725}
726
Chris Lattner026dc962009-02-14 07:37:35 +0000727static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
728 // check the attribute arguments.
729 if (Attr.getNumArgs() != 0) {
730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
731 return;
732 }
733
Nuno Lopesd20254f2009-12-20 23:11:08 +0000734 if (!isFunctionOrMethod(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000735 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000736 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000737 return;
738 }
Mike Stumpbf916502009-07-24 19:02:52 +0000739
Nuno Lopesf8577982009-12-22 23:59:52 +0000740 if (getFunctionType(D)->getResultType()->isVoidType()) {
741 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
742 << Attr.getName();
743 return;
744 }
745
Nuno Lopesd20254f2009-12-20 23:11:08 +0000746 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000747}
748
749static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000750 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000751 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000753 return;
754 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000755
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000756 /* weak only applies to non-static declarations */
757 bool isStatic = false;
758 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
759 isStatic = VD->getStorageClass() == VarDecl::Static;
760 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
761 isStatic = FD->getStorageClass() == FunctionDecl::Static;
762 }
763 if (isStatic) {
764 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
765 dyn_cast<NamedDecl>(D)->getNameAsString();
766 return;
767 }
768
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000769 // TODO: could also be applied to methods?
770 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
771 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000772 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000773 return;
774 }
Mike Stumpbf916502009-07-24 19:02:52 +0000775
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000776 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000777}
778
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000779static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
780 // check the attribute arguments.
781 if (Attr.getNumArgs() != 0) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
783 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000784 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000785
786 // weak_import only applies to variable & function declarations.
787 bool isDef = false;
788 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
789 isDef = (!VD->hasExternalStorage() || VD->getInit());
790 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000791 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000792 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
793 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000794 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000795 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000797 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000798 return;
799 }
800
801 // Merge should handle any subsequent violations.
802 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000803 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000804 diag::warn_attribute_weak_import_invalid_on_definition)
805 << "weak_import" << 2 /*variable and function*/;
806 return;
807 }
808
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000809 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000810}
811
Chris Lattner026dc962009-02-14 07:37:35 +0000812static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000813 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000814 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000816 return;
817 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000818
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000819 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000820 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000821 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000822 return;
823 }
824
Chris Lattner026dc962009-02-14 07:37:35 +0000825 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000826 if (!FD) {
827 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000828 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000829 return;
830 }
831
832 // Currently, the dllimport attribute is ignored for inlined functions.
833 // Warning is emitted.
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000834 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000835 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
836 return;
837 }
838
839 // The attribute is also overridden by a subsequent declaration as dllexport.
840 // Warning is emitted.
841 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
842 nextAttr = nextAttr->getNext()) {
843 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
844 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
845 return;
846 }
847 }
848
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000849 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000850 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
851 return;
852 }
853
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000854 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000855}
856
Chris Lattner026dc962009-02-14 07:37:35 +0000857static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000858 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000859 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000861 return;
862 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000863
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000864 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000865 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000866 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000867 return;
868 }
869
Chris Lattner026dc962009-02-14 07:37:35 +0000870 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000871 if (!FD) {
872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000873 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000874 return;
875 }
876
Mike Stumpbf916502009-07-24 19:02:52 +0000877 // Currently, the dllexport attribute is ignored for inlined functions, unless
878 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000879 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000880 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
881 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
882 return;
883 }
884
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000885 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000886}
887
Nate Begeman6f3d8382009-06-26 06:32:41 +0000888static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
889 Sema &S) {
890 // Attribute has 3 arguments.
891 if (Attr.getNumArgs() != 3) {
892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
893 return;
894 }
895
896 unsigned WGSize[3];
897 for (unsigned i = 0; i < 3; ++i) {
898 Expr *E = static_cast<Expr *>(Attr.getArg(i));
899 llvm::APSInt ArgNum(32);
900 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
902 << "reqd_work_group_size" << E->getSourceRange();
903 return;
904 }
905 WGSize[i] = (unsigned) ArgNum.getZExtValue();
906 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000907 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000908 WGSize[2]));
909}
910
Chris Lattner026dc962009-02-14 07:37:35 +0000911static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000912 // Attribute has no arguments.
913 if (Attr.getNumArgs() != 1) {
914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
915 return;
916 }
917
918 // Make sure that there is a string literal as the sections's single
919 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000920 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
921 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000922 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000923 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000924 return;
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattner797c3c42009-08-10 19:03:04 +0000927 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000928 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000929 if (!Error.empty()) {
930 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
931 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000932 return;
933 }
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000935 // This attribute cannot be applied to local variables.
936 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
937 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
938 return;
939 }
940
941 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000942}
943
Eli Friedman8f4c59e2009-11-09 18:38:53 +0000944static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
945 // Attribute has no arguments.
946 if (Attr.getNumArgs() != 0) {
947 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
948 return;
949 }
950
951 // Attribute can be applied only to functions.
952 if (!isa<FunctionDecl>(d)) {
953 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
954 << Attr.getName() << 0 /*function*/;
955 return;
956 }
957
958 // cdecl and fastcall attributes are mutually incompatible.
959 if (d->getAttr<FastCallAttr>()) {
960 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
961 << "cdecl" << "fastcall";
962 return;
963 }
964
965 // cdecl and stdcall attributes are mutually incompatible.
966 if (d->getAttr<StdCallAttr>()) {
967 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
968 << "cdecl" << "stdcall";
969 return;
970 }
971
972 d->addAttr(::new (S.Context) CDeclAttr());
973}
974
975
Chris Lattner803d0802008-06-29 00:43:07 +0000976static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000977 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000978 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000980 return;
981 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000982
983 // Attribute can be applied only to functions.
984 if (!isa<FunctionDecl>(d)) {
985 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000986 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000987 return;
988 }
989
990 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000991 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000992 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
993 << "stdcall" << "fastcall";
994 return;
995 }
996
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000997 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000998}
999
John McCall9112b932009-11-04 03:36:09 +00001000/// Diagnose the use of a non-standard calling convention on the given
1001/// function.
1002static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1003 SourceLocation Loc, Sema &S) {
1004 if (!D->hasPrototype()) {
1005 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1006 return;
1007 }
1008
1009 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1010 if (T->isVariadic()) {
1011 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1012 return;
1013 }
1014}
1015
Chris Lattner803d0802008-06-29 00:43:07 +00001016static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001017 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001018 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001019 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001020 return;
1021 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001022
1023 if (!isa<FunctionDecl>(d)) {
1024 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001025 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001026 return;
1027 }
1028
John McCall9112b932009-11-04 03:36:09 +00001029 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1030
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001031 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001032 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001033 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1034 << "fastcall" << "stdcall";
1035 return;
1036 }
1037
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001038 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001039}
1040
Chris Lattner803d0802008-06-29 00:43:07 +00001041static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001042 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001043 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001044 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001045 return;
1046 }
Mike Stumpbf916502009-07-24 19:02:52 +00001047
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001048 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001049}
1050
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001051static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1052 // check the attribute arguments.
1053 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001054 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001055 return;
1056 }
Mike Stumpbf916502009-07-24 19:02:52 +00001057
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001058 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001059}
1060
1061static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1062 // check the attribute arguments.
1063 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001065 return;
1066 }
Mike Stumpbf916502009-07-24 19:02:52 +00001067
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001068 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001069}
1070
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001071static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001072 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001073 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1074 return;
1075 }
Mike Stumpbf916502009-07-24 19:02:52 +00001076
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001077 if (Attr.getNumArgs() != 0) {
1078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1079 return;
1080 }
Mike Stumpbf916502009-07-24 19:02:52 +00001081
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001082 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001083
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001084 if (!VD || !VD->hasLocalStorage()) {
1085 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1086 return;
1087 }
Mike Stumpbf916502009-07-24 19:02:52 +00001088
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001089 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001090 NamedDecl *CleanupDecl
1091 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1092 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001093 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001094 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001095 Attr.getParameterName();
1096 return;
1097 }
Mike Stumpbf916502009-07-24 19:02:52 +00001098
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001099 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1100 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001102 Attr.getParameterName();
1103 return;
1104 }
1105
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001106 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001107 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001108 Attr.getParameterName();
1109 return;
1110 }
Mike Stumpbf916502009-07-24 19:02:52 +00001111
Anders Carlsson89941c12009-02-07 23:16:50 +00001112 // We're currently more strict than GCC about what function types we accept.
1113 // If this ever proves to be a problem it should be easy to fix.
1114 QualType Ty = S.Context.getPointerType(VD->getType());
1115 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001116 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001117 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001118 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1119 Attr.getParameterName() << ParamTy << Ty;
1120 return;
1121 }
Mike Stumpbf916502009-07-24 19:02:52 +00001122
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001123 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001124}
1125
Mike Stumpbf916502009-07-24 19:02:52 +00001126/// Handle __attribute__((format_arg((idx)))) attribute based on
1127/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1128static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001129 if (Attr.getNumArgs() != 1) {
1130 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1131 return;
1132 }
1133 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1134 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1135 << Attr.getName() << 0 /*function*/;
1136 return;
1137 }
Mike Stumpbf916502009-07-24 19:02:52 +00001138 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1139 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001140 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1141 unsigned FirstIdx = 1;
1142 // checks for the 2nd argument
1143 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1144 llvm::APSInt Idx(32);
1145 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1146 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1147 << "format" << 2 << IdxExpr->getSourceRange();
1148 return;
1149 }
Mike Stumpbf916502009-07-24 19:02:52 +00001150
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001151 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1153 << "format" << 2 << IdxExpr->getSourceRange();
1154 return;
1155 }
Mike Stumpbf916502009-07-24 19:02:52 +00001156
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001157 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001158
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001159 // make sure the format string is really a string
1160 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001161
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001162 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1163 if (not_nsstring_type &&
1164 !isCFStringType(Ty, S.Context) &&
1165 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001166 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001167 // FIXME: Should highlight the actual expression that has the wrong type.
1168 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001169 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001170 << IdxExpr->getSourceRange();
1171 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001172 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001173 Ty = getFunctionOrMethodResultType(d);
1174 if (!isNSStringType(Ty, S.Context) &&
1175 !isCFStringType(Ty, S.Context) &&
1176 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001177 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001178 // FIXME: Should highlight the actual expression that has the wrong type.
1179 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001180 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001181 << IdxExpr->getSourceRange();
1182 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001183 }
1184
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001185 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001186}
1187
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001188enum FormatAttrKind {
1189 CFStringFormat,
1190 NSStringFormat,
1191 StrftimeFormat,
1192 SupportedFormat,
1193 InvalidFormat
1194};
1195
1196/// getFormatAttrKind - Map from format attribute names to supported format
1197/// types.
1198static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1199 // Check for formats that get handled specially.
1200 if (Format == "NSString")
1201 return NSStringFormat;
1202 if (Format == "CFString")
1203 return CFStringFormat;
1204 if (Format == "strftime")
1205 return StrftimeFormat;
1206
1207 // Otherwise, check for supported formats.
1208 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1209 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1210 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1211 Format == "zcmn_err")
1212 return SupportedFormat;
1213
1214 return InvalidFormat;
1215}
1216
Mike Stumpbf916502009-07-24 19:02:52 +00001217/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1218/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001219static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001220
Chris Lattner545dd342008-06-28 23:36:30 +00001221 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001222 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001223 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001224 return;
1225 }
1226
Chris Lattner545dd342008-06-28 23:36:30 +00001227 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001228 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001229 return;
1230 }
1231
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001232 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001233 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001234 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001235 return;
1236 }
1237
Daniel Dunbar35682492008-09-26 04:12:28 +00001238 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001239 unsigned FirstIdx = 1;
1240
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001241 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001242
1243 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001244 if (Format.startswith("__") && Format.endswith("__"))
1245 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001246
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001247 // Check for supported formats.
1248 FormatAttrKind Kind = getFormatAttrKind(Format);
1249 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001250 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001251 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001252 return;
1253 }
1254
1255 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001256 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001257 llvm::APSInt Idx(32);
1258 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001259 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001260 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001261 return;
1262 }
1263
Anders Carlsson4fb77202009-08-25 14:12:34 +00001264 // FIXME: We should handle the implicit 'this' parameter in a more generic
1265 // way that can be used for other arguments.
1266 bool HasImplicitThisParam = false;
1267 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1268 if (MD->isInstance()) {
1269 HasImplicitThisParam = true;
1270 NumArgs++;
1271 }
1272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001276 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001277 return;
1278 }
1279
1280 // FIXME: Do we need to bounds check?
1281 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001282
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001283 if (HasImplicitThisParam) {
1284 if (ArgIdx == 0) {
1285 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1286 << "a string type" << IdxExpr->getSourceRange();
1287 return;
1288 }
1289 ArgIdx--;
1290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattner6b6b5372008-06-26 18:38:35 +00001292 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001293 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001294
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001295 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001296 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001297 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1298 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001299 return;
1300 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001301 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001302 // FIXME: do we need to check if the type is NSString*? What are the
1303 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001304 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001305 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001306 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1307 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001308 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001309 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001310 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001311 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001312 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001313 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1314 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001315 return;
1316 }
1317
1318 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001319 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001320 llvm::APSInt FirstArg(32);
1321 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001323 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001324 return;
1325 }
1326
1327 // check if the function is variadic if the 3rd argument non-zero
1328 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001329 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001330 ++NumArgs; // +1 for ...
1331 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001332 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001333 return;
1334 }
1335 }
1336
Chris Lattner3c73c412008-11-19 08:23:25 +00001337 // strftime requires FirstArg to be 0 because it doesn't read from any
1338 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001339 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001340 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001341 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1342 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001343 return;
1344 }
1345 // if 0 it disables parameter checking (to use with e.g. va_list)
1346 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001347 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001348 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001349 return;
1350 }
1351
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001352 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1353 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001354}
1355
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001356static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1357 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001358 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001359 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001361 return;
1362 }
1363
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001364 // Try to find the underlying union declaration.
1365 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001366 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001367 if (TD && TD->getUnderlyingType()->isUnionType())
1368 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1369 else
1370 RD = dyn_cast<RecordDecl>(d);
1371
1372 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001373 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001374 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001375 return;
1376 }
1377
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001378 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001379 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001380 diag::warn_transparent_union_attribute_not_definition);
1381 return;
1382 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001383
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001384 RecordDecl::field_iterator Field = RD->field_begin(),
1385 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001386 if (Field == FieldEnd) {
1387 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1388 return;
1389 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001390
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001391 FieldDecl *FirstField = *Field;
1392 QualType FirstType = FirstField->getType();
1393 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001394 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001395 diag::warn_transparent_union_attribute_floating);
1396 return;
1397 }
1398
1399 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1400 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1401 for (; Field != FieldEnd; ++Field) {
1402 QualType FieldType = Field->getType();
1403 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1404 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1405 // Warn if we drop the attribute.
1406 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001407 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001408 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001409 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001410 diag::warn_transparent_union_attribute_field_size_align)
1411 << isSize << Field->getDeclName() << FieldBits;
1412 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001413 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001414 diag::note_transparent_union_first_field_size_align)
1415 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001416 return;
1417 }
1418 }
1419
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001420 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001421}
1422
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001423static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001424 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001425 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001427 return;
1428 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001429 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1430 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001431
Chris Lattner6b6b5372008-06-26 18:38:35 +00001432 // Make sure that there is a string literal as the annotation's single
1433 // argument.
1434 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001435 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001436 return;
1437 }
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001438 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001439}
1440
Chris Lattner803d0802008-06-29 00:43:07 +00001441static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001442 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001443 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001444 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001445 return;
1446 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001447
1448 //FIXME: The C++0x version of this attribute has more limited applicabilty
1449 // than GNU's, and should error out when it is used to specify a
1450 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001451
1452 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001453 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001454 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001455 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001456 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001457 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001458 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001459 }
Mike Stumpbf916502009-07-24 19:02:52 +00001460
Chris Lattner49e2d342008-06-28 23:50:44 +00001461 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1462 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001463 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1465 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001466 return;
1467 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001468 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001469 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001470 << alignmentExpr->getSourceRange();
1471 return;
1472 }
1473
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001474 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001475}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001476
Mike Stumpbf916502009-07-24 19:02:52 +00001477/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1478/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001479///
Mike Stumpbf916502009-07-24 19:02:52 +00001480/// Despite what would be logical, the mode attribute is a decl attribute, not a
1481/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1482/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001483static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001484 // This attribute isn't documented, but glibc uses it. It changes
1485 // the width of an int or unsigned int to the specified size.
1486
1487 // Check that there aren't any arguments
1488 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001489 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001490 return;
1491 }
1492
1493 IdentifierInfo *Name = Attr.getParameterName();
1494 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001495 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001496 return;
1497 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001498
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001499 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001500
1501 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001502 if (Str.startswith("__") && Str.endswith("__"))
1503 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001504
1505 unsigned DestWidth = 0;
1506 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001507 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001508 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001509 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001510 switch (Str[0]) {
1511 case 'Q': DestWidth = 8; break;
1512 case 'H': DestWidth = 16; break;
1513 case 'S': DestWidth = 32; break;
1514 case 'D': DestWidth = 64; break;
1515 case 'X': DestWidth = 96; break;
1516 case 'T': DestWidth = 128; break;
1517 }
1518 if (Str[1] == 'F') {
1519 IntegerMode = false;
1520 } else if (Str[1] == 'C') {
1521 IntegerMode = false;
1522 ComplexMode = true;
1523 } else if (Str[1] != 'I') {
1524 DestWidth = 0;
1525 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001526 break;
1527 case 4:
1528 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1529 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001530 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001531 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001532 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001533 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001534 break;
1535 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001536 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001537 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001538 break;
1539 }
1540
1541 QualType OldTy;
1542 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1543 OldTy = TD->getUnderlyingType();
1544 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1545 OldTy = VD->getType();
1546 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001547 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1548 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001549 return;
1550 }
Eli Friedman73397492009-03-03 06:41:03 +00001551
John McCall183700f2009-09-21 23:43:11 +00001552 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001553 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1554 else if (IntegerMode) {
1555 if (!OldTy->isIntegralType())
1556 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1557 } else if (ComplexMode) {
1558 if (!OldTy->isComplexType())
1559 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1560 } else {
1561 if (!OldTy->isFloatingType())
1562 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1563 }
1564
Mike Stump390b4cc2009-05-16 07:39:55 +00001565 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1566 // and friends, at least with glibc.
1567 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1568 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001569 // FIXME: Make sure floating-point mappings are accurate
1570 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001571 QualType NewTy;
1572 switch (DestWidth) {
1573 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001574 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001575 return;
1576 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001577 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001578 return;
1579 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001580 if (!IntegerMode) {
1581 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1582 return;
1583 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001584 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001585 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001586 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001587 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001588 break;
1589 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001590 if (!IntegerMode) {
1591 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1592 return;
1593 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001594 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001595 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001596 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001597 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001598 break;
1599 case 32:
1600 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001601 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001602 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001603 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001604 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001605 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001606 break;
1607 case 64:
1608 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001609 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001610 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001611 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001612 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001613 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001614 break;
Eli Friedman73397492009-03-03 06:41:03 +00001615 case 96:
1616 NewTy = S.Context.LongDoubleTy;
1617 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001618 case 128:
1619 if (!IntegerMode) {
1620 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1621 return;
1622 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001623 if (OldTy->isSignedIntegerType())
1624 NewTy = S.Context.Int128Ty;
1625 else
1626 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001627 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001628 }
1629
Eli Friedman73397492009-03-03 06:41:03 +00001630 if (ComplexMode) {
1631 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001632 }
1633
1634 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001635 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1636 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001637 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001638 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001639 cast<ValueDecl>(D)->setType(NewTy);
1640}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001641
Mike Stump1feade82009-08-26 22:31:08 +00001642static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001643 // check the attribute arguments.
1644 if (Attr.getNumArgs() > 0) {
1645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1646 return;
1647 }
Anders Carlssone896d982009-02-13 08:11:52 +00001648
Anders Carlsson5bab7882009-02-19 19:16:48 +00001649 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001650 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001651 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001652 return;
1653 }
Mike Stumpbf916502009-07-24 19:02:52 +00001654
Mike Stump1feade82009-08-26 22:31:08 +00001655 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001656}
1657
Mike Stump1feade82009-08-26 22:31:08 +00001658static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001659 // check the attribute arguments.
1660 if (Attr.getNumArgs() != 0) {
1661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1662 return;
1663 }
Mike Stumpbf916502009-07-24 19:02:52 +00001664
Chris Lattnerc5197432009-04-14 17:02:11 +00001665 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001667 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001668 return;
1669 }
Mike Stumpbf916502009-07-24 19:02:52 +00001670
Mike Stump1feade82009-08-26 22:31:08 +00001671 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001672}
1673
Chris Lattnercf2a7212009-04-20 19:12:28 +00001674static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001675 // check the attribute arguments.
1676 if (Attr.getNumArgs() != 0) {
1677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1678 return;
1679 }
Mike Stumpbf916502009-07-24 19:02:52 +00001680
Chris Lattnerc5197432009-04-14 17:02:11 +00001681 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1682 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001683 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001684 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001685 return;
1686 }
Mike Stumpbf916502009-07-24 19:02:52 +00001687
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001688 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001689 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001690 return;
1691 }
Mike Stumpbf916502009-07-24 19:02:52 +00001692
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001693 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001694}
1695
Fariborz Jahanianee760332009-03-27 18:38:55 +00001696static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1697 // check the attribute arguments.
1698 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001699 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001700 return;
1701 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001702
Fariborz Jahanianee760332009-03-27 18:38:55 +00001703 if (!isFunctionOrMethod(d)) {
1704 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001705 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001706 return;
1707 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001708
1709 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1710 llvm::APSInt NumParams(32);
1711 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1712 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1713 << "regparm" << NumParamsExpr->getSourceRange();
1714 return;
1715 }
1716
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001717 if (S.Context.Target.getRegParmMax() == 0) {
1718 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001719 << NumParamsExpr->getSourceRange();
1720 return;
1721 }
1722
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001723 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001724 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1725 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001726 return;
1727 }
1728
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001729 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001730}
1731
Sean Huntbbd37c62009-11-21 08:43:09 +00001732static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1733 // check the attribute arguments.
1734 if (Attr.getNumArgs() != 0) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1736 return;
1737 }
1738
1739 if (!isa<CXXRecordDecl>(d)
1740 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1741 S.Diag(Attr.getLoc(),
1742 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1743 : diag::warn_attribute_wrong_decl_type)
1744 << Attr.getName() << 7 /*virtual method or class*/;
1745 return;
1746 }
Sean Hunt7725e672009-11-25 04:20:27 +00001747
1748 // FIXME: Conform to C++0x redeclaration rules.
1749
1750 if (d->getAttr<FinalAttr>()) {
1751 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1752 return;
1753 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001754
1755 d->addAttr(::new (S.Context) FinalAttr());
1756}
1757
Chris Lattner0744e5f2008-06-29 00:23:49 +00001758//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001759// C++0x member checking attributes
1760//===----------------------------------------------------------------------===//
1761
1762static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1763 if (Attr.getNumArgs() != 0) {
1764 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1765 return;
1766 }
1767
1768 if (!isa<CXXRecordDecl>(d)) {
1769 S.Diag(Attr.getLoc(),
1770 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1771 : diag::warn_attribute_wrong_decl_type)
1772 << Attr.getName() << 9 /*class*/;
1773 return;
1774 }
1775
1776 if (d->getAttr<BaseCheckAttr>()) {
1777 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1778 return;
1779 }
1780
1781 d->addAttr(::new (S.Context) BaseCheckAttr());
1782}
1783
1784static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1785 if (Attr.getNumArgs() != 0) {
1786 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1787 return;
1788 }
1789
1790 if (!isa<RecordDecl>(d->getDeclContext())) {
1791 // FIXME: It's not the type that's the problem
1792 S.Diag(Attr.getLoc(),
1793 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1794 : diag::warn_attribute_wrong_decl_type)
1795 << Attr.getName() << 11 /*member*/;
1796 return;
1797 }
1798
1799 // FIXME: Conform to C++0x redeclaration rules.
1800
1801 if (d->getAttr<HidingAttr>()) {
1802 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1803 return;
1804 }
1805
1806 d->addAttr(::new (S.Context) HidingAttr());
1807}
1808
1809static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1810 if (Attr.getNumArgs() != 0) {
1811 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1812 return;
1813 }
1814
1815 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1816 // FIXME: It's not the type that's the problem
1817 S.Diag(Attr.getLoc(),
1818 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1819 : diag::warn_attribute_wrong_decl_type)
1820 << Attr.getName() << 10 /*virtual method*/;
1821 return;
1822 }
1823
1824 // FIXME: Conform to C++0x redeclaration rules.
1825
1826 if (d->getAttr<OverrideAttr>()) {
1827 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1828 return;
1829 }
1830
1831 d->addAttr(::new (S.Context) OverrideAttr());
1832}
1833
1834//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001835// Checker-specific attribute handlers.
1836//===----------------------------------------------------------------------===//
1837
1838static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1839 Sema &S) {
1840
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001841 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001842
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001843 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1844 RetTy = MD->getResultType();
1845 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1846 RetTy = FD->getResultType();
1847 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001848 SourceLocation L = Attr.getLoc();
1849 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1850 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001851 return;
1852 }
Mike Stumpbf916502009-07-24 19:02:52 +00001853
Ted Kremenek6217b802009-07-29 21:53:49 +00001854 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001855 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001856 SourceLocation L = Attr.getLoc();
1857 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1858 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001859 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001860 }
Mike Stumpbf916502009-07-24 19:02:52 +00001861
Ted Kremenekb71368d2009-05-09 02:44:38 +00001862 switch (Attr.getKind()) {
1863 default:
1864 assert(0 && "invalid ownership attribute");
1865 return;
1866 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001867 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001868 return;
1869 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001870 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001871 return;
1872 };
1873}
1874
1875//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001876// Top Level Sema Entry Points
1877//===----------------------------------------------------------------------===//
1878
Sebastian Redla89d82c2008-12-21 19:24:58 +00001879/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001880/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001881/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1882/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001883static void ProcessDeclAttribute(Scope *scope, Decl *D,
1884 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001885 if (Attr.isDeclspecAttribute())
1886 // FIXME: Try to deal with __declspec attributes!
1887 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001888 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001889 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001890 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001891 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001892 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001893 // Ignore these, these are type attributes, handled by
1894 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001895 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001896 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1897 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001898 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001899 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001900 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001901 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001902 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1903 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001904 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001905 HandleDependencyAttr (D, Attr, S); break;
1906 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1907 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1908 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1909 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1910 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1911 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001912 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001913 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001914 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001915 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1916 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1917 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1918 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1919 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1920 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1921 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1922 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1923 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1924 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1925 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1926 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001927
1928 // Checker-specific.
1929 case AttributeList::AT_ns_returns_retained:
1930 case AttributeList::AT_cf_returns_retained:
1931 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1932
Nate Begeman6f3d8382009-06-26 06:32:41 +00001933 case AttributeList::AT_reqd_wg_size:
1934 HandleReqdWorkGroupSize(D, Attr, S); break;
1935
Sean Hunt7725e672009-11-25 04:20:27 +00001936 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1937 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1938 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1939 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1940 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1941 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001942 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001943 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1944 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001945 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1946 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001947 case AttributeList::AT_transparent_union:
1948 HandleTransparentUnionAttr(D, Attr, S);
1949 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001950 case AttributeList::AT_objc_exception:
1951 HandleObjCExceptionAttr(D, Attr, S);
1952 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001953 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001954 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1955 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1956 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1957 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1958 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1959 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1960 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1961 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1962 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001963 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001964 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001965 // Just ignore
1966 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001967 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001968 // Ask target about the attribute.
1969 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1970 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001972 break;
1973 }
1974}
1975
1976/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1977/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001978void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001979 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001980 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001981 AttrList = AttrList->getNext();
1982 }
1983}
1984
Ryan Flynne25ff832009-07-30 03:15:39 +00001985/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1986/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001987NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001988 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001989 NamedDecl *NewD = 0;
1990 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1991 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1992 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001993 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001994 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1995 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1996 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001997 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001998 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001999 }
2000 return NewD;
2001}
2002
2003/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2004/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002005void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002006 if (W.getUsed()) return; // only do this once
2007 W.setUsed(true);
2008 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2009 IdentifierInfo *NDId = ND->getIdentifier();
2010 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
2011 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2012 NewD->addAttr(::new (Context) WeakAttr());
2013 WeakTopLevelDecl.push_back(NewD);
2014 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2015 // to insert Decl at TU scope, sorry.
2016 DeclContext *SavedContext = CurContext;
2017 CurContext = Context.getTranslationUnitDecl();
2018 PushOnScopeChains(NewD, S);
2019 CurContext = SavedContext;
2020 } else { // just add weak to existing
2021 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002022 }
2023}
2024
Chris Lattner0744e5f2008-06-29 00:23:49 +00002025/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2026/// it, apply them to D. This is a bit tricky because PD can have attributes
2027/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002028void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002029 // Handle #pragma weak
2030 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2031 if (ND->hasLinkage()) {
2032 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2033 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002034 // Identifier referenced by #pragma weak before it was declared
2035 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002036 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2037 }
2038 }
2039 }
2040
Chris Lattner0744e5f2008-06-29 00:23:49 +00002041 // Apply decl attributes from the DeclSpec if present.
2042 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002043 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002044
Chris Lattner0744e5f2008-06-29 00:23:49 +00002045 // Walk the declarator structure, applying decl attributes that were in a type
2046 // position to the decl itself. This handles cases like:
2047 // int *__attr__(x)** D;
2048 // when X is a decl attribute.
2049 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2050 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002051 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002052
Chris Lattner0744e5f2008-06-29 00:23:49 +00002053 // Finally, apply any attributes on the decl itself.
2054 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002055 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002056}
John McCall54abf7d2009-11-04 02:18:39 +00002057
2058/// PushParsingDeclaration - Enter a new "scope" of deprecation
2059/// warnings.
2060///
2061/// The state token we use is the start index of this scope
2062/// on the warning stack.
2063Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2064 ParsingDeclDepth++;
2065 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2066}
2067
2068static bool isDeclDeprecated(Decl *D) {
2069 do {
2070 if (D->hasAttr<DeprecatedAttr>())
2071 return true;
2072 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2073 return false;
2074}
2075
2076void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2077 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2078 ParsingDeclDepth--;
2079
2080 if (DelayedDeprecationWarnings.empty())
2081 return;
2082
2083 unsigned SavedIndex = (unsigned) S;
2084 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2085 "saved index is out of bounds");
2086
2087 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2088 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2089 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2090 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2091 if (ND) {
2092 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2093
2094 // Prevent this from triggering multiple times.
2095 ND = 0;
2096 }
2097 }
2098 }
2099
2100 DelayedDeprecationWarnings.set_size(SavedIndex);
2101}
2102
2103void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2104 // Delay if we're currently parsing a declaration.
2105 if (ParsingDeclDepth) {
2106 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2107 return;
2108 }
2109
2110 // Otherwise, don't warn if our current context is deprecated.
2111 if (isDeclDeprecated(cast<Decl>(CurContext)))
2112 return;
2113
2114 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2115}