blob: 23dccdf62ecf5f50f6c2af7b2e1da1bcb834dd57 [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"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Ted Kremeneka18d7d82009-08-14 20:49:40 +000027static const FunctionType *getFunctionType(const Decl *d,
28 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000030 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000031 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000032 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000033 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000034 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000035 Ty = decl->getUnderlyingType();
36 else
37 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000038
Chris Lattner6b6b5372008-06-26 18:38:35 +000039 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000040 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000041 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000042 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000043
John McCall183700f2009-09-21 23:43:11 +000044 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000045}
46
Daniel Dunbar35682492008-09-26 04:12:28 +000047// FIXME: We should provide an abstraction around a method or function
48// to provide the following bits of information.
49
Daniel Dunbard3f2c102008-10-19 02:04:16 +000050/// isFunctionOrMethod - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000051/// type (function or function-typed variable).
52static bool isFunction(const Decl *d) {
53 return getFunctionType(d, false) != NULL;
54}
55
56/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000057/// type (function or function-typed variable) or an Objective-C
58/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000059static bool isFunctionOrMethod(const Decl *d) {
60 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000061}
62
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000063/// isFunctionOrMethodOrBlock - Return true if the given decl has function
64/// type (function or function-typed variable) or an Objective-C
65/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000066static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000067 if (isFunctionOrMethod(d))
68 return true;
69 // check for block is more involved.
70 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
71 QualType Ty = V->getType();
72 return Ty->isBlockPointerType();
73 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000074 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000075}
76
Daniel Dunbard3f2c102008-10-19 02:04:16 +000077/// hasFunctionProto - Return true if the given decl has a argument
78/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000079/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000080static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000081 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000082 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000083 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000084 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000085 return true;
86 }
87}
88
89/// getFunctionOrMethodNumArgs - Return number of function or method
90/// arguments. It is an error to call this on a K&R function (use
91/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000092static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000093 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000094 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
96 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000097 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000098}
99
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000100static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000101 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000102 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000103 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
104 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000105
Chris Lattner89951a82009-02-20 18:43:26 +0000106 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000107}
108
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000109static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000110 if (const FunctionType *FnTy = getFunctionType(d))
111 return cast<FunctionProtoType>(FnTy)->getResultType();
112 return cast<ObjCMethodDecl>(d)->getResultType();
113}
114
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000115static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000116 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000118 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000119 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
120 return BD->IsVariadic();
121 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000122 return cast<ObjCMethodDecl>(d)->isVariadic();
123 }
124}
125
Chris Lattner6b6b5372008-06-26 18:38:35 +0000126static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000127 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000128 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000129 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000130
John McCall183700f2009-09-21 23:43:11 +0000131 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000132 if (!ClsT)
133 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000134
Chris Lattner6b6b5372008-06-26 18:38:35 +0000135 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000136
Chris Lattner6b6b5372008-06-26 18:38:35 +0000137 // FIXME: Should we walk the chain of classes?
138 return ClsName == &Ctx.Idents.get("NSString") ||
139 ClsName == &Ctx.Idents.get("NSMutableString");
140}
141
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000142static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000143 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000144 if (!PT)
145 return false;
146
Ted Kremenek6217b802009-07-29 21:53:49 +0000147 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000148 if (!RT)
149 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000150
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000151 const RecordDecl *RD = RT->getDecl();
152 if (RD->getTagKind() != TagDecl::TK_struct)
153 return false;
154
155 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
156}
157
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000158//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159// Attribute Implementations
160//===----------------------------------------------------------------------===//
161
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000162// FIXME: All this manual attribute parsing code is gross. At the
163// least add some helper functions to check most argument patterns (#
164// and types of args).
165
Mike Stumpbf916502009-07-24 19:02:52 +0000166static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000167 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000168 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
169 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000171 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 }
Mike Stumpbf916502009-07-24 19:02:52 +0000173
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000175
176 Expr *sizeExpr;
177
178 // Special case where the argument is a template id.
179 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000180 CXXScopeSpec SS;
181 UnqualifiedId id;
182 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
183 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000184 } else {
185 // check the attribute arguments.
186 if (Attr.getNumArgs() != 1) {
187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
188 return;
189 }
190 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000191 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000192
193 // Instantiate/Install the vector type, and let Sema build the type for us.
194 // This will run the reguired checks.
195 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
196 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000197 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000198 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000199
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000200 // Remember this typedef decl, we will need it later for diagnostics.
201 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000202 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203}
204
Chris Lattner803d0802008-06-29 00:43:07 +0000205static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000206 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000207 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000208 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000209 return;
210 }
Mike Stumpbf916502009-07-24 19:02:52 +0000211
Chris Lattner6b6b5372008-06-26 18:38:35 +0000212 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000213 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000214 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
215 // If the alignment is less than or equal to 8 bits, the packed attribute
216 // has no effect.
217 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000218 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000219 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000220 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000221 else
Anders Carlssona860e752009-08-08 18:23:56 +0000222 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000223 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000224 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000225}
226
Ted Kremenek96329d42008-07-15 22:26:48 +0000227static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
228 // check the attribute arguments.
229 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000231 return;
232 }
Mike Stumpbf916502009-07-24 19:02:52 +0000233
Ted Kremenek96329d42008-07-15 22:26:48 +0000234 // The IBOutlet attribute only applies to instance variables of Objective-C
235 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000236 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000237 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000238 else
Ted Kremenek32742602009-02-17 22:20:20 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000240}
241
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000242static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000243 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
244 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000245 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000246 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000247 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000248 return;
249 }
Mike Stumpbf916502009-07-24 19:02:52 +0000250
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000251 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000252
253 // The nonnull attribute only applies to pointers.
254 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000255
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000256 for (AttributeList::arg_iterator I=Attr.arg_begin(),
257 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000258
259
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000260 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000261 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000262 llvm::APSInt ArgNum(32);
263 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000264 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
265 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000266 return;
267 }
Mike Stumpbf916502009-07-24 19:02:52 +0000268
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000269 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000270
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000271 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000272 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000273 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000274 return;
275 }
Mike Stumpbf916502009-07-24 19:02:52 +0000276
Ted Kremenek465172f2008-07-21 22:09:15 +0000277 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000278
279 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000280 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000281 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000282 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000283 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
284 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000285 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000286 }
Mike Stumpbf916502009-07-24 19:02:52 +0000287
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000288 NonNullArgs.push_back(x);
289 }
Mike Stumpbf916502009-07-24 19:02:52 +0000290
291 // If no arguments were specified to __attribute__((nonnull)) then all pointer
292 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000293 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000294 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
295 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000296 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000297 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000298 }
Mike Stumpbf916502009-07-24 19:02:52 +0000299
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000300 if (NonNullArgs.empty()) {
301 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
302 return;
303 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000304 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000305
306 unsigned* start = &NonNullArgs[0];
307 unsigned size = NonNullArgs.size();
308 std::sort(start, start + size);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000309 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000310}
311
Chris Lattner803d0802008-06-29 00:43:07 +0000312static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000313 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000314 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000316 return;
317 }
Mike Stumpbf916502009-07-24 19:02:52 +0000318
Chris Lattner545dd342008-06-28 23:36:30 +0000319 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000320 Arg = Arg->IgnoreParenCasts();
321 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000322
Chris Lattner6b6b5372008-06-26 18:38:35 +0000323 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000324 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000325 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000326 return;
327 }
Mike Stumpbf916502009-07-24 19:02:52 +0000328
Chris Lattner6b6b5372008-06-26 18:38:35 +0000329 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000330
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000331 d->addAttr(::new (S.Context) AliasAttr(Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000332}
333
Mike Stumpbf916502009-07-24 19:02:52 +0000334static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000335 Sema &S) {
336 // check the attribute arguments.
337 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000338 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000339 return;
340 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000341
Chris Lattnerc5197432009-04-14 17:02:11 +0000342 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000343 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000344 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000345 return;
346 }
Mike Stumpbf916502009-07-24 19:02:52 +0000347
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000348 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000349}
350
Ryan Flynn76168e22009-08-09 20:07:29 +0000351static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
352 // check the attribute arguments.
353 if (Attr.getNumArgs() != 0) {
354 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
355 return;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000358 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000359 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000360 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
361 d->addAttr(::new (S.Context) MallocAttr());
362 return;
363 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000364 }
365
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000366 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000367}
368
Ted Kremenekb7252322009-04-10 00:01:14 +0000369static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000370 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000371 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000372 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000373 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000374 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000375 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000376
Mike Stump19c30c02009-04-29 19:03:13 +0000377 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
378 ValueDecl *VD = dyn_cast<ValueDecl>(d);
379 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000380 S.Diag(Attr.getLoc(),
381 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
382 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000383 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000384 return false;
385 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000386 }
Mike Stumpbf916502009-07-24 19:02:52 +0000387
Ted Kremenekb7252322009-04-10 00:01:14 +0000388 return true;
389}
390
391static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000392 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000393 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000394}
395
396static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
397 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000398 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000399 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000400}
401
Sean Huntbbd37c62009-11-21 08:43:09 +0000402static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
403 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
404 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
405 << Attr.getName() << 8; /*function, method, or parameter*/
406 return;
407 }
408 // FIXME: Actually store the attribute on the declaration
409}
410
Ted Kremenek73798892008-07-25 04:39:19 +0000411static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
412 // check the attribute arguments.
413 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000415 return;
416 }
Mike Stumpbf916502009-07-24 19:02:52 +0000417
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000418 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000420 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000421 return;
422 }
Mike Stumpbf916502009-07-24 19:02:52 +0000423
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000424 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000425}
426
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000427static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
428 // check the attribute arguments.
429 if (Attr.getNumArgs() != 0) {
430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
431 return;
432 }
Mike Stumpbf916502009-07-24 19:02:52 +0000433
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000434 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000435 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000436 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
437 return;
438 }
439 } else if (!isFunctionOrMethod(d)) {
440 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000441 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000442 return;
443 }
Mike Stumpbf916502009-07-24 19:02:52 +0000444
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000445 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000446}
447
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000448static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
449 // check the attribute arguments.
450 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000451 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
452 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000453 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000454 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000455
456 int priority = 65535; // FIXME: Do not hardcode such constants.
457 if (Attr.getNumArgs() > 0) {
458 Expr *E = static_cast<Expr *>(Attr.getArg(0));
459 llvm::APSInt Idx(32);
460 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000461 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000462 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000463 return;
464 }
465 priority = Idx.getZExtValue();
466 }
Mike Stumpbf916502009-07-24 19:02:52 +0000467
Chris Lattnerc5197432009-04-14 17:02:11 +0000468 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000469 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000470 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000471 return;
472 }
473
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000474 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000475}
476
477static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
478 // check the attribute arguments.
479 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
481 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000482 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000483 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000484
485 int priority = 65535; // FIXME: Do not hardcode such constants.
486 if (Attr.getNumArgs() > 0) {
487 Expr *E = static_cast<Expr *>(Attr.getArg(0));
488 llvm::APSInt Idx(32);
489 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000490 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000491 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000492 return;
493 }
494 priority = Idx.getZExtValue();
495 }
Mike Stumpbf916502009-07-24 19:02:52 +0000496
Anders Carlsson6782fc62008-08-22 22:10:48 +0000497 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000499 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000500 return;
501 }
502
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000503 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000504}
505
Chris Lattner803d0802008-06-29 00:43:07 +0000506static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000507 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000508 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000510 return;
511 }
Mike Stumpbf916502009-07-24 19:02:52 +0000512
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000513 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000514}
515
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000516static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
517 // check the attribute arguments.
518 if (Attr.getNumArgs() != 0) {
519 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
520 return;
521 }
Mike Stumpbf916502009-07-24 19:02:52 +0000522
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000523 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000524}
525
Chris Lattner803d0802008-06-29 00:43:07 +0000526static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000527 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000528 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000529 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000530 return;
531 }
Mike Stumpbf916502009-07-24 19:02:52 +0000532
Chris Lattner545dd342008-06-28 23:36:30 +0000533 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000534 Arg = Arg->IgnoreParenCasts();
535 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000536
Chris Lattner6b6b5372008-06-26 18:38:35 +0000537 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000539 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000540 return;
541 }
Mike Stumpbf916502009-07-24 19:02:52 +0000542
Chris Lattner6b6b5372008-06-26 18:38:35 +0000543 const char *TypeStr = Str->getStrData();
544 unsigned TypeLen = Str->getByteLength();
545 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000546
Chris Lattner6b6b5372008-06-26 18:38:35 +0000547 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
548 type = VisibilityAttr::DefaultVisibility;
549 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
550 type = VisibilityAttr::HiddenVisibility;
551 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
552 type = VisibilityAttr::HiddenVisibility; // FIXME
553 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
554 type = VisibilityAttr::ProtectedVisibility;
555 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000556 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000557 return;
558 }
Mike Stumpbf916502009-07-24 19:02:52 +0000559
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000560 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000561}
562
Chris Lattner0db29ec2009-02-14 08:09:34 +0000563static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
564 Sema &S) {
565 if (Attr.getNumArgs() != 0) {
566 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
567 return;
568 }
Mike Stumpbf916502009-07-24 19:02:52 +0000569
Chris Lattner0db29ec2009-02-14 08:09:34 +0000570 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
571 if (OCI == 0) {
572 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
573 return;
574 }
Mike Stumpbf916502009-07-24 19:02:52 +0000575
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000576 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000577}
578
579static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000580 if (Attr.getNumArgs() != 0) {
581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
582 return;
583 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000584 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000585 QualType T = TD->getUnderlyingType();
586 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000587 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000588 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
589 return;
590 }
591 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000592 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000593}
594
Mike Stumpbf916502009-07-24 19:02:52 +0000595static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000596HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
597 if (Attr.getNumArgs() != 0) {
598 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
599 return;
600 }
601
602 if (!isa<FunctionDecl>(D)) {
603 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
604 return;
605 }
606
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000607 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000608}
609
Steve Naroff9eae5762008-09-18 16:44:58 +0000610static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000611 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000612 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000613 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000614 return;
615 }
Mike Stumpbf916502009-07-24 19:02:52 +0000616
Steve Naroff9eae5762008-09-18 16:44:58 +0000617 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000618 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000619 return;
620 }
Mike Stumpbf916502009-07-24 19:02:52 +0000621
Steve Naroff9eae5762008-09-18 16:44:58 +0000622 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000623 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000624 type = BlocksAttr::ByRef;
625 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000626 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000627 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000628 return;
629 }
Mike Stumpbf916502009-07-24 19:02:52 +0000630
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000631 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000632}
633
Anders Carlsson77091822008-10-05 18:05:59 +0000634static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
635 // check the attribute arguments.
636 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
638 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000639 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000640 }
641
Anders Carlsson77091822008-10-05 18:05:59 +0000642 int sentinel = 0;
643 if (Attr.getNumArgs() > 0) {
644 Expr *E = static_cast<Expr *>(Attr.getArg(0));
645 llvm::APSInt Idx(32);
646 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000647 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000648 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000649 return;
650 }
651 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000652
Anders Carlsson77091822008-10-05 18:05:59 +0000653 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000654 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
655 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000656 return;
657 }
658 }
659
660 int nullPos = 0;
661 if (Attr.getNumArgs() > 1) {
662 Expr *E = static_cast<Expr *>(Attr.getArg(1));
663 llvm::APSInt Idx(32);
664 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000666 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000667 return;
668 }
669 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000670
Anders Carlsson77091822008-10-05 18:05:59 +0000671 if (nullPos > 1 || nullPos < 0) {
672 // FIXME: This error message could be improved, it would be nice
673 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000674 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
675 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000676 return;
677 }
678 }
679
680 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000681 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000682 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000683
Chris Lattner897cd902009-03-17 23:03:47 +0000684 if (isa<FunctionNoProtoType>(FT)) {
685 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
686 return;
687 }
Mike Stumpbf916502009-07-24 19:02:52 +0000688
Chris Lattner897cd902009-03-17 23:03:47 +0000689 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000690 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000691 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000692 }
Anders Carlsson77091822008-10-05 18:05:59 +0000693 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
694 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000695 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000696 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000697 }
698 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000699 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
700 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000701 ;
702 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000703 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000704 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000705 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000706 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000707 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000708 int m = Ty->isFunctionPointerType() ? 0 : 1;
709 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000710 return;
711 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000712 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000713 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000714 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000715 return;
716 }
Anders Carlsson77091822008-10-05 18:05:59 +0000717 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000718 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000719 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000720 return;
721 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000722 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000723}
724
Chris Lattner026dc962009-02-14 07:37:35 +0000725static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
726 // check the attribute arguments.
727 if (Attr.getNumArgs() != 0) {
728 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
729 return;
730 }
731
732 // TODO: could also be applied to methods?
733 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
734 if (!Fn) {
735 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
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000740 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000741}
742
743static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000744 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000745 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000747 return;
748 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000749
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000750 /* weak only applies to non-static declarations */
751 bool isStatic = false;
752 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
753 isStatic = VD->getStorageClass() == VarDecl::Static;
754 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
755 isStatic = FD->getStorageClass() == FunctionDecl::Static;
756 }
757 if (isStatic) {
758 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
759 dyn_cast<NamedDecl>(D)->getNameAsString();
760 return;
761 }
762
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000763 // TODO: could also be applied to methods?
764 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
765 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000766 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000767 return;
768 }
Mike Stumpbf916502009-07-24 19:02:52 +0000769
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000770 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000771}
772
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000773static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
774 // check the attribute arguments.
775 if (Attr.getNumArgs() != 0) {
776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
777 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000778 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000779
780 // weak_import only applies to variable & function declarations.
781 bool isDef = false;
782 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
783 isDef = (!VD->hasExternalStorage() || VD->getInit());
784 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000785 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000786 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
787 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000788 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000789 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000790 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000791 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000792 return;
793 }
794
795 // Merge should handle any subsequent violations.
796 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000797 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000798 diag::warn_attribute_weak_import_invalid_on_definition)
799 << "weak_import" << 2 /*variable and function*/;
800 return;
801 }
802
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000803 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000804}
805
Chris Lattner026dc962009-02-14 07:37:35 +0000806static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000807 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000808 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000810 return;
811 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000812
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000813 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000814 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000815 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000816 return;
817 }
818
Chris Lattner026dc962009-02-14 07:37:35 +0000819 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000820 if (!FD) {
821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000822 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000823 return;
824 }
825
826 // Currently, the dllimport attribute is ignored for inlined functions.
827 // Warning is emitted.
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000828 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000829 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
830 return;
831 }
832
833 // The attribute is also overridden by a subsequent declaration as dllexport.
834 // Warning is emitted.
835 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
836 nextAttr = nextAttr->getNext()) {
837 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
838 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
839 return;
840 }
841 }
842
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000843 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000844 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
845 return;
846 }
847
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000848 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000849}
850
Chris Lattner026dc962009-02-14 07:37:35 +0000851static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000852 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000853 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000854 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000855 return;
856 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000857
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000858 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000859 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000860 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000861 return;
862 }
863
Chris Lattner026dc962009-02-14 07:37:35 +0000864 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000865 if (!FD) {
866 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000867 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000868 return;
869 }
870
Mike Stumpbf916502009-07-24 19:02:52 +0000871 // Currently, the dllexport attribute is ignored for inlined functions, unless
872 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000873 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000874 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
875 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
876 return;
877 }
878
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000879 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000880}
881
Nate Begeman6f3d8382009-06-26 06:32:41 +0000882static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
883 Sema &S) {
884 // Attribute has 3 arguments.
885 if (Attr.getNumArgs() != 3) {
886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
887 return;
888 }
889
890 unsigned WGSize[3];
891 for (unsigned i = 0; i < 3; ++i) {
892 Expr *E = static_cast<Expr *>(Attr.getArg(i));
893 llvm::APSInt ArgNum(32);
894 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
895 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
896 << "reqd_work_group_size" << E->getSourceRange();
897 return;
898 }
899 WGSize[i] = (unsigned) ArgNum.getZExtValue();
900 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000901 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000902 WGSize[2]));
903}
904
Chris Lattner026dc962009-02-14 07:37:35 +0000905static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000906 // Attribute has no arguments.
907 if (Attr.getNumArgs() != 1) {
908 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
909 return;
910 }
911
912 // Make sure that there is a string literal as the sections's single
913 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000914 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
915 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000916 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000917 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000918 return;
919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Chris Lattner797c3c42009-08-10 19:03:04 +0000921 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000922 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner797c3c42009-08-10 19:03:04 +0000923 if (Error.empty()) {
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000924 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Chris Lattner797c3c42009-08-10 19:03:04 +0000925 return;
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Chris Lattner797c3c42009-08-10 19:03:04 +0000928 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
929 << Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000931}
932
Eli Friedman8f4c59e2009-11-09 18:38:53 +0000933static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
934 // Attribute has no arguments.
935 if (Attr.getNumArgs() != 0) {
936 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
937 return;
938 }
939
940 // Attribute can be applied only to functions.
941 if (!isa<FunctionDecl>(d)) {
942 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
943 << Attr.getName() << 0 /*function*/;
944 return;
945 }
946
947 // cdecl and fastcall attributes are mutually incompatible.
948 if (d->getAttr<FastCallAttr>()) {
949 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
950 << "cdecl" << "fastcall";
951 return;
952 }
953
954 // cdecl and stdcall attributes are mutually incompatible.
955 if (d->getAttr<StdCallAttr>()) {
956 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
957 << "cdecl" << "stdcall";
958 return;
959 }
960
961 d->addAttr(::new (S.Context) CDeclAttr());
962}
963
964
Chris Lattner803d0802008-06-29 00:43:07 +0000965static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000966 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000967 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000968 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000969 return;
970 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000971
972 // Attribute can be applied only to functions.
973 if (!isa<FunctionDecl>(d)) {
974 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000975 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000976 return;
977 }
978
979 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000980 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000981 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
982 << "stdcall" << "fastcall";
983 return;
984 }
985
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000986 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000987}
988
John McCall9112b932009-11-04 03:36:09 +0000989/// Diagnose the use of a non-standard calling convention on the given
990/// function.
991static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
992 SourceLocation Loc, Sema &S) {
993 if (!D->hasPrototype()) {
994 S.Diag(Loc, diag::err_cconv_knr) << CConv;
995 return;
996 }
997
998 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
999 if (T->isVariadic()) {
1000 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1001 return;
1002 }
1003}
1004
Chris Lattner803d0802008-06-29 00:43:07 +00001005static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001006 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001007 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001008 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001009 return;
1010 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001011
1012 if (!isa<FunctionDecl>(d)) {
1013 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001014 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001015 return;
1016 }
1017
John McCall9112b932009-11-04 03:36:09 +00001018 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1019
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001020 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001021 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001022 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1023 << "fastcall" << "stdcall";
1024 return;
1025 }
1026
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001027 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001028}
1029
Chris Lattner803d0802008-06-29 00:43:07 +00001030static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001031 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001032 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001033 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001034 return;
1035 }
Mike Stumpbf916502009-07-24 19:02:52 +00001036
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001037 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001038}
1039
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001040static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1041 // check the attribute arguments.
1042 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001044 return;
1045 }
Mike Stumpbf916502009-07-24 19:02:52 +00001046
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001047 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001048}
1049
1050static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1051 // check the attribute arguments.
1052 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001053 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001054 return;
1055 }
Mike Stumpbf916502009-07-24 19:02:52 +00001056
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001057 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001058}
1059
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001060static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001061 // Match gcc which ignores cleanup attrs when compiling C++.
1062 if (S.getLangOptions().CPlusPlus)
1063 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001064
1065 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001066 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1067 return;
1068 }
Mike Stumpbf916502009-07-24 19:02:52 +00001069
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001070 if (Attr.getNumArgs() != 0) {
1071 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1072 return;
1073 }
Mike Stumpbf916502009-07-24 19:02:52 +00001074
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001075 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001076
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001077 if (!VD || !VD->hasLocalStorage()) {
1078 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1079 return;
1080 }
Mike Stumpbf916502009-07-24 19:02:52 +00001081
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001082 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001083 NamedDecl *CleanupDecl
1084 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1085 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001086 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001087 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001088 Attr.getParameterName();
1089 return;
1090 }
Mike Stumpbf916502009-07-24 19:02:52 +00001091
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001092 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1093 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001094 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001095 Attr.getParameterName();
1096 return;
1097 }
1098
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001099 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001100 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001101 Attr.getParameterName();
1102 return;
1103 }
Mike Stumpbf916502009-07-24 19:02:52 +00001104
Anders Carlsson89941c12009-02-07 23:16:50 +00001105 // We're currently more strict than GCC about what function types we accept.
1106 // If this ever proves to be a problem it should be easy to fix.
1107 QualType Ty = S.Context.getPointerType(VD->getType());
1108 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001109 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001110 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001111 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1112 Attr.getParameterName() << ParamTy << Ty;
1113 return;
1114 }
Mike Stumpbf916502009-07-24 19:02:52 +00001115
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001116 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001117}
1118
Mike Stumpbf916502009-07-24 19:02:52 +00001119/// Handle __attribute__((format_arg((idx)))) attribute based on
1120/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1121static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001122 if (Attr.getNumArgs() != 1) {
1123 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1124 return;
1125 }
1126 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1127 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1128 << Attr.getName() << 0 /*function*/;
1129 return;
1130 }
Mike Stumpbf916502009-07-24 19:02:52 +00001131 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1132 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001133 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1134 unsigned FirstIdx = 1;
1135 // checks for the 2nd argument
1136 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1137 llvm::APSInt Idx(32);
1138 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1140 << "format" << 2 << IdxExpr->getSourceRange();
1141 return;
1142 }
Mike Stumpbf916502009-07-24 19:02:52 +00001143
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001144 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1145 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1146 << "format" << 2 << IdxExpr->getSourceRange();
1147 return;
1148 }
Mike Stumpbf916502009-07-24 19:02:52 +00001149
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001150 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001151
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001152 // make sure the format string is really a string
1153 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001154
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001155 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1156 if (not_nsstring_type &&
1157 !isCFStringType(Ty, S.Context) &&
1158 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001159 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001160 // FIXME: Should highlight the actual expression that has the wrong type.
1161 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001162 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001163 << IdxExpr->getSourceRange();
1164 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001165 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001166 Ty = getFunctionOrMethodResultType(d);
1167 if (!isNSStringType(Ty, S.Context) &&
1168 !isCFStringType(Ty, S.Context) &&
1169 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001170 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001171 // FIXME: Should highlight the actual expression that has the wrong type.
1172 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001173 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001174 << IdxExpr->getSourceRange();
1175 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001176 }
1177
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001178 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001179}
1180
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001181enum FormatAttrKind {
1182 CFStringFormat,
1183 NSStringFormat,
1184 StrftimeFormat,
1185 SupportedFormat,
1186 InvalidFormat
1187};
1188
1189/// getFormatAttrKind - Map from format attribute names to supported format
1190/// types.
1191static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1192 // Check for formats that get handled specially.
1193 if (Format == "NSString")
1194 return NSStringFormat;
1195 if (Format == "CFString")
1196 return CFStringFormat;
1197 if (Format == "strftime")
1198 return StrftimeFormat;
1199
1200 // Otherwise, check for supported formats.
1201 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1202 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1203 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1204 Format == "zcmn_err")
1205 return SupportedFormat;
1206
1207 return InvalidFormat;
1208}
1209
Mike Stumpbf916502009-07-24 19:02:52 +00001210/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1211/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001212static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001213
Chris Lattner545dd342008-06-28 23:36:30 +00001214 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001215 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001216 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001217 return;
1218 }
1219
Chris Lattner545dd342008-06-28 23:36:30 +00001220 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001221 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001222 return;
1223 }
1224
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001225 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001227 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228 return;
1229 }
1230
Daniel Dunbar35682492008-09-26 04:12:28 +00001231 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001232 unsigned FirstIdx = 1;
1233
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001234 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001235
1236 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001237 if (Format.startswith("__") && Format.endswith("__"))
1238 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001239
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001240 // Check for supported formats.
1241 FormatAttrKind Kind = getFormatAttrKind(Format);
1242 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001243 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001244 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001245 return;
1246 }
1247
1248 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001249 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001250 llvm::APSInt Idx(32);
1251 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001252 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001253 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001254 return;
1255 }
1256
Anders Carlsson4fb77202009-08-25 14:12:34 +00001257 // FIXME: We should handle the implicit 'this' parameter in a more generic
1258 // way that can be used for other arguments.
1259 bool HasImplicitThisParam = false;
1260 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1261 if (MD->isInstance()) {
1262 HasImplicitThisParam = true;
1263 NumArgs++;
1264 }
1265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Chris Lattner6b6b5372008-06-26 18:38:35 +00001267 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001269 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001270 return;
1271 }
1272
1273 // FIXME: Do we need to bounds check?
1274 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001275
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001276 if (HasImplicitThisParam) {
1277 if (ArgIdx == 0) {
1278 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1279 << "a string type" << IdxExpr->getSourceRange();
1280 return;
1281 }
1282 ArgIdx--;
1283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Chris Lattner6b6b5372008-06-26 18:38:35 +00001285 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001286 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001287
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001288 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001289 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001290 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1291 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001292 return;
1293 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001294 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001295 // FIXME: do we need to check if the type is NSString*? What are the
1296 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001297 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001298 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001299 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1300 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001301 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001302 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001303 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001304 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
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 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001308 return;
1309 }
1310
1311 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001312 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001313 llvm::APSInt FirstArg(32);
1314 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001315 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001316 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001317 return;
1318 }
1319
1320 // check if the function is variadic if the 3rd argument non-zero
1321 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001322 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001323 ++NumArgs; // +1 for ...
1324 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001325 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001326 return;
1327 }
1328 }
1329
Chris Lattner3c73c412008-11-19 08:23:25 +00001330 // strftime requires FirstArg to be 0 because it doesn't read from any
1331 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001332 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001333 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001334 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1335 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001336 return;
1337 }
1338 // if 0 it disables parameter checking (to use with e.g. va_list)
1339 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001340 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001341 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001342 return;
1343 }
1344
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001345 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1346 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347}
1348
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001349static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1350 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001351 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001352 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001353 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001354 return;
1355 }
1356
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001357 // Try to find the underlying union declaration.
1358 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001359 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001360 if (TD && TD->getUnderlyingType()->isUnionType())
1361 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1362 else
1363 RD = dyn_cast<RecordDecl>(d);
1364
1365 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001366 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001367 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001368 return;
1369 }
1370
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001371 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001372 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001373 diag::warn_transparent_union_attribute_not_definition);
1374 return;
1375 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001376
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001377 RecordDecl::field_iterator Field = RD->field_begin(),
1378 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001379 if (Field == FieldEnd) {
1380 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1381 return;
1382 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001383
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001384 FieldDecl *FirstField = *Field;
1385 QualType FirstType = FirstField->getType();
1386 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001387 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001388 diag::warn_transparent_union_attribute_floating);
1389 return;
1390 }
1391
1392 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1393 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1394 for (; Field != FieldEnd; ++Field) {
1395 QualType FieldType = Field->getType();
1396 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1397 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1398 // Warn if we drop the attribute.
1399 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001400 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001401 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001402 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001403 diag::warn_transparent_union_attribute_field_size_align)
1404 << isSize << Field->getDeclName() << FieldBits;
1405 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001406 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001407 diag::note_transparent_union_first_field_size_align)
1408 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001409 return;
1410 }
1411 }
1412
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001413 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001414}
1415
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001416static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001417 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001418 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001419 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001420 return;
1421 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001422 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1423 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001424
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425 // Make sure that there is a string literal as the annotation's single
1426 // argument.
1427 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001428 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001429 return;
1430 }
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001431 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001432}
1433
Chris Lattner803d0802008-06-29 00:43:07 +00001434static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001435 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001436 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001438 return;
1439 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001440
1441 //FIXME: The C++0x version of this attribute has more limited applicabilty
1442 // than GNU's, and should error out when it is used to specify a
1443 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444
1445 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001446 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001447 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001448 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001449 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001450 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001451 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001452 }
Mike Stumpbf916502009-07-24 19:02:52 +00001453
Chris Lattner49e2d342008-06-28 23:50:44 +00001454 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1455 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001456 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1458 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001459 return;
1460 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001461 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001462 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001463 << alignmentExpr->getSourceRange();
1464 return;
1465 }
1466
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001467 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001468}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001469
Mike Stumpbf916502009-07-24 19:02:52 +00001470/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1471/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001472///
Mike Stumpbf916502009-07-24 19:02:52 +00001473/// Despite what would be logical, the mode attribute is a decl attribute, not a
1474/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1475/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001476static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001477 // This attribute isn't documented, but glibc uses it. It changes
1478 // the width of an int or unsigned int to the specified size.
1479
1480 // Check that there aren't any arguments
1481 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001482 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001483 return;
1484 }
1485
1486 IdentifierInfo *Name = Attr.getParameterName();
1487 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001489 return;
1490 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001491
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001492 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001493
1494 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001495 if (Str.startswith("__") && Str.endswith("__"))
1496 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001497
1498 unsigned DestWidth = 0;
1499 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001500 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001501 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001502 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001503 switch (Str[0]) {
1504 case 'Q': DestWidth = 8; break;
1505 case 'H': DestWidth = 16; break;
1506 case 'S': DestWidth = 32; break;
1507 case 'D': DestWidth = 64; break;
1508 case 'X': DestWidth = 96; break;
1509 case 'T': DestWidth = 128; break;
1510 }
1511 if (Str[1] == 'F') {
1512 IntegerMode = false;
1513 } else if (Str[1] == 'C') {
1514 IntegerMode = false;
1515 ComplexMode = true;
1516 } else if (Str[1] != 'I') {
1517 DestWidth = 0;
1518 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001519 break;
1520 case 4:
1521 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1522 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001523 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001524 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001525 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001526 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001527 break;
1528 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001529 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001530 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001531 break;
1532 }
1533
1534 QualType OldTy;
1535 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1536 OldTy = TD->getUnderlyingType();
1537 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1538 OldTy = VD->getType();
1539 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001540 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1541 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001542 return;
1543 }
Eli Friedman73397492009-03-03 06:41:03 +00001544
John McCall183700f2009-09-21 23:43:11 +00001545 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001546 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1547 else if (IntegerMode) {
1548 if (!OldTy->isIntegralType())
1549 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1550 } else if (ComplexMode) {
1551 if (!OldTy->isComplexType())
1552 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1553 } else {
1554 if (!OldTy->isFloatingType())
1555 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1556 }
1557
Mike Stump390b4cc2009-05-16 07:39:55 +00001558 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1559 // and friends, at least with glibc.
1560 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1561 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001562 // FIXME: Make sure floating-point mappings are accurate
1563 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001564 QualType NewTy;
1565 switch (DestWidth) {
1566 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001567 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001568 return;
1569 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001570 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001571 return;
1572 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001573 if (!IntegerMode) {
1574 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1575 return;
1576 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001577 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001578 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001579 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001580 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001581 break;
1582 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001583 if (!IntegerMode) {
1584 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1585 return;
1586 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001588 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001589 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001590 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001591 break;
1592 case 32:
1593 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001594 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001596 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001597 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001598 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001599 break;
1600 case 64:
1601 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001602 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001603 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001604 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001605 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001606 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001607 break;
Eli Friedman73397492009-03-03 06:41:03 +00001608 case 96:
1609 NewTy = S.Context.LongDoubleTy;
1610 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001611 case 128:
1612 if (!IntegerMode) {
1613 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1614 return;
1615 }
1616 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001617 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001618 }
1619
Eli Friedman73397492009-03-03 06:41:03 +00001620 if (ComplexMode) {
1621 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001622 }
1623
1624 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001625 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1626 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001627 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001628 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001629 cast<ValueDecl>(D)->setType(NewTy);
1630}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001631
Mike Stump1feade82009-08-26 22:31:08 +00001632static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001633 // check the attribute arguments.
1634 if (Attr.getNumArgs() > 0) {
1635 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1636 return;
1637 }
Anders Carlssone896d982009-02-13 08:11:52 +00001638
Anders Carlsson5bab7882009-02-19 19:16:48 +00001639 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001641 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001642 return;
1643 }
Mike Stumpbf916502009-07-24 19:02:52 +00001644
Mike Stump1feade82009-08-26 22:31:08 +00001645 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001646}
1647
Mike Stump1feade82009-08-26 22:31:08 +00001648static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001649 // check the attribute arguments.
1650 if (Attr.getNumArgs() != 0) {
1651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1652 return;
1653 }
Mike Stumpbf916502009-07-24 19:02:52 +00001654
Chris Lattnerc5197432009-04-14 17:02:11 +00001655 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001656 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001657 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001658 return;
1659 }
Mike Stumpbf916502009-07-24 19:02:52 +00001660
Mike Stump1feade82009-08-26 22:31:08 +00001661 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001662}
1663
Chris Lattnercf2a7212009-04-20 19:12:28 +00001664static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001665 // check the attribute arguments.
1666 if (Attr.getNumArgs() != 0) {
1667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1668 return;
1669 }
Mike Stumpbf916502009-07-24 19:02:52 +00001670
Chris Lattnerc5197432009-04-14 17:02:11 +00001671 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1672 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001673 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001674 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001675 return;
1676 }
Mike Stumpbf916502009-07-24 19:02:52 +00001677
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001678 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001679 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001680 return;
1681 }
Mike Stumpbf916502009-07-24 19:02:52 +00001682
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001683 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001684}
1685
Fariborz Jahanianee760332009-03-27 18:38:55 +00001686static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1687 // check the attribute arguments.
1688 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001690 return;
1691 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001692
Fariborz Jahanianee760332009-03-27 18:38:55 +00001693 if (!isFunctionOrMethod(d)) {
1694 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001695 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001696 return;
1697 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001698
1699 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1700 llvm::APSInt NumParams(32);
1701 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1702 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1703 << "regparm" << NumParamsExpr->getSourceRange();
1704 return;
1705 }
1706
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001707 if (S.Context.Target.getRegParmMax() == 0) {
1708 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001709 << NumParamsExpr->getSourceRange();
1710 return;
1711 }
1712
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001713 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001714 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1715 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001716 return;
1717 }
1718
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001719 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001720}
1721
Sean Huntbbd37c62009-11-21 08:43:09 +00001722static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1723 // check the attribute arguments.
1724 if (Attr.getNumArgs() != 0) {
1725 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1726 return;
1727 }
1728
1729 if (!isa<CXXRecordDecl>(d)
1730 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1731 S.Diag(Attr.getLoc(),
1732 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1733 : diag::warn_attribute_wrong_decl_type)
1734 << Attr.getName() << 7 /*virtual method or class*/;
1735 return;
1736 }
Sean Hunt7725e672009-11-25 04:20:27 +00001737
1738 // FIXME: Conform to C++0x redeclaration rules.
1739
1740 if (d->getAttr<FinalAttr>()) {
1741 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1742 return;
1743 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001744
1745 d->addAttr(::new (S.Context) FinalAttr());
1746}
1747
Chris Lattner0744e5f2008-06-29 00:23:49 +00001748//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001749// C++0x member checking attributes
1750//===----------------------------------------------------------------------===//
1751
1752static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1753 if (Attr.getNumArgs() != 0) {
1754 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1755 return;
1756 }
1757
1758 if (!isa<CXXRecordDecl>(d)) {
1759 S.Diag(Attr.getLoc(),
1760 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1761 : diag::warn_attribute_wrong_decl_type)
1762 << Attr.getName() << 9 /*class*/;
1763 return;
1764 }
1765
1766 if (d->getAttr<BaseCheckAttr>()) {
1767 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1768 return;
1769 }
1770
1771 d->addAttr(::new (S.Context) BaseCheckAttr());
1772}
1773
1774static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1775 if (Attr.getNumArgs() != 0) {
1776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1777 return;
1778 }
1779
1780 if (!isa<RecordDecl>(d->getDeclContext())) {
1781 // FIXME: It's not the type that's the problem
1782 S.Diag(Attr.getLoc(),
1783 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1784 : diag::warn_attribute_wrong_decl_type)
1785 << Attr.getName() << 11 /*member*/;
1786 return;
1787 }
1788
1789 // FIXME: Conform to C++0x redeclaration rules.
1790
1791 if (d->getAttr<HidingAttr>()) {
1792 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1793 return;
1794 }
1795
1796 d->addAttr(::new (S.Context) HidingAttr());
1797}
1798
1799static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1800 if (Attr.getNumArgs() != 0) {
1801 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1802 return;
1803 }
1804
1805 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1806 // FIXME: It's not the type that's the problem
1807 S.Diag(Attr.getLoc(),
1808 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1809 : diag::warn_attribute_wrong_decl_type)
1810 << Attr.getName() << 10 /*virtual method*/;
1811 return;
1812 }
1813
1814 // FIXME: Conform to C++0x redeclaration rules.
1815
1816 if (d->getAttr<OverrideAttr>()) {
1817 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1818 return;
1819 }
1820
1821 d->addAttr(::new (S.Context) OverrideAttr());
1822}
1823
1824//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001825// Checker-specific attribute handlers.
1826//===----------------------------------------------------------------------===//
1827
1828static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1829 Sema &S) {
1830
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001831 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001832
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001833 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1834 RetTy = MD->getResultType();
1835 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1836 RetTy = FD->getResultType();
1837 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001838 SourceLocation L = Attr.getLoc();
1839 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1840 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001841 return;
1842 }
Mike Stumpbf916502009-07-24 19:02:52 +00001843
Ted Kremenek6217b802009-07-29 21:53:49 +00001844 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001845 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001846 SourceLocation L = Attr.getLoc();
1847 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1848 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001849 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001850 }
Mike Stumpbf916502009-07-24 19:02:52 +00001851
Ted Kremenekb71368d2009-05-09 02:44:38 +00001852 switch (Attr.getKind()) {
1853 default:
1854 assert(0 && "invalid ownership attribute");
1855 return;
1856 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001857 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001858 return;
1859 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001860 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001861 return;
1862 };
1863}
1864
1865//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001866// Top Level Sema Entry Points
1867//===----------------------------------------------------------------------===//
1868
Sebastian Redla89d82c2008-12-21 19:24:58 +00001869/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001870/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001871/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1872/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001873static void ProcessDeclAttribute(Scope *scope, Decl *D,
1874 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001875 if (Attr.isDeclspecAttribute())
1876 // FIXME: Try to deal with __declspec attributes!
1877 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001878 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001879 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001880 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001881 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001882 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001883 // Ignore these, these are type attributes, handled by
1884 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001885 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001886 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1887 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001888 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001889 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001890 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001891 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001892 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1893 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001894 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001895 HandleDependencyAttr (D, Attr, S); break;
1896 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1897 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1898 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1899 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1900 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1901 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001902 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001903 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001904 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001905 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1906 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1907 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1908 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1909 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1910 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1911 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1912 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1913 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1914 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1915 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1916 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001917
1918 // Checker-specific.
1919 case AttributeList::AT_ns_returns_retained:
1920 case AttributeList::AT_cf_returns_retained:
1921 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1922
Nate Begeman6f3d8382009-06-26 06:32:41 +00001923 case AttributeList::AT_reqd_wg_size:
1924 HandleReqdWorkGroupSize(D, Attr, S); break;
1925
Sean Hunt7725e672009-11-25 04:20:27 +00001926 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1927 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1928 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1929 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1930 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1931 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001932 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001933 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1934 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001935 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1936 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001937 case AttributeList::AT_transparent_union:
1938 HandleTransparentUnionAttr(D, Attr, S);
1939 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001940 case AttributeList::AT_objc_exception:
1941 HandleObjCExceptionAttr(D, Attr, S);
1942 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001943 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001944 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1945 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1946 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1947 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1948 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1949 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1950 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1951 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1952 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001953 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001954 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001955 // Just ignore
1956 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001957 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001958 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001959 break;
1960 }
1961}
1962
1963/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1964/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001965void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001966 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001967 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001968 AttrList = AttrList->getNext();
1969 }
1970}
1971
Ryan Flynne25ff832009-07-30 03:15:39 +00001972/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1973/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001974NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001975 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001976 NamedDecl *NewD = 0;
1977 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1978 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1979 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001980 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001981 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1982 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1983 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001984 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001985 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001986 }
1987 return NewD;
1988}
1989
1990/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1991/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001992void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001993 if (W.getUsed()) return; // only do this once
1994 W.setUsed(true);
1995 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1996 IdentifierInfo *NDId = ND->getIdentifier();
1997 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1998 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1999 NewD->addAttr(::new (Context) WeakAttr());
2000 WeakTopLevelDecl.push_back(NewD);
2001 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2002 // to insert Decl at TU scope, sorry.
2003 DeclContext *SavedContext = CurContext;
2004 CurContext = Context.getTranslationUnitDecl();
2005 PushOnScopeChains(NewD, S);
2006 CurContext = SavedContext;
2007 } else { // just add weak to existing
2008 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002009 }
2010}
2011
Chris Lattner0744e5f2008-06-29 00:23:49 +00002012/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2013/// it, apply them to D. This is a bit tricky because PD can have attributes
2014/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002015void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002016 // Handle #pragma weak
2017 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2018 if (ND->hasLinkage()) {
2019 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2020 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002021 // Identifier referenced by #pragma weak before it was declared
2022 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002023 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2024 }
2025 }
2026 }
2027
Chris Lattner0744e5f2008-06-29 00:23:49 +00002028 // Apply decl attributes from the DeclSpec if present.
2029 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002030 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002031
Chris Lattner0744e5f2008-06-29 00:23:49 +00002032 // Walk the declarator structure, applying decl attributes that were in a type
2033 // position to the decl itself. This handles cases like:
2034 // int *__attr__(x)** D;
2035 // when X is a decl attribute.
2036 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2037 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002038 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002039
Chris Lattner0744e5f2008-06-29 00:23:49 +00002040 // Finally, apply any attributes on the decl itself.
2041 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002042 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002043}
John McCall54abf7d2009-11-04 02:18:39 +00002044
2045/// PushParsingDeclaration - Enter a new "scope" of deprecation
2046/// warnings.
2047///
2048/// The state token we use is the start index of this scope
2049/// on the warning stack.
2050Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2051 ParsingDeclDepth++;
2052 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2053}
2054
2055static bool isDeclDeprecated(Decl *D) {
2056 do {
2057 if (D->hasAttr<DeprecatedAttr>())
2058 return true;
2059 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2060 return false;
2061}
2062
2063void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2064 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2065 ParsingDeclDepth--;
2066
2067 if (DelayedDeprecationWarnings.empty())
2068 return;
2069
2070 unsigned SavedIndex = (unsigned) S;
2071 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2072 "saved index is out of bounds");
2073
2074 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2075 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2076 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2077 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2078 if (ND) {
2079 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2080
2081 // Prevent this from triggering multiple times.
2082 ND = 0;
2083 }
2084 }
2085 }
2086
2087 DelayedDeprecationWarnings.set_size(SavedIndex);
2088}
2089
2090void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2091 // Delay if we're currently parsing a declaration.
2092 if (ParsingDeclDepth) {
2093 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2094 return;
2095 }
2096
2097 // Otherwise, don't warn if our current context is deprecated.
2098 if (isDeclDeprecated(cast<Decl>(CurContext)))
2099 return;
2100
2101 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2102}