blob: e219923f6dae207de163499e7a4cc6aae1411c50 [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) {
Mike Stumpbf916502009-07-24 19:02:52 +00001061 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1063 return;
1064 }
Mike Stumpbf916502009-07-24 19:02:52 +00001065
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001066 if (Attr.getNumArgs() != 0) {
1067 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1068 return;
1069 }
Mike Stumpbf916502009-07-24 19:02:52 +00001070
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001071 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001072
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001073 if (!VD || !VD->hasLocalStorage()) {
1074 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1075 return;
1076 }
Mike Stumpbf916502009-07-24 19:02:52 +00001077
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001078 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001079 NamedDecl *CleanupDecl
1080 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1081 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001082 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001083 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001084 Attr.getParameterName();
1085 return;
1086 }
Mike Stumpbf916502009-07-24 19:02:52 +00001087
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001088 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1089 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001090 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001091 Attr.getParameterName();
1092 return;
1093 }
1094
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001095 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001096 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001097 Attr.getParameterName();
1098 return;
1099 }
Mike Stumpbf916502009-07-24 19:02:52 +00001100
Anders Carlsson89941c12009-02-07 23:16:50 +00001101 // We're currently more strict than GCC about what function types we accept.
1102 // If this ever proves to be a problem it should be easy to fix.
1103 QualType Ty = S.Context.getPointerType(VD->getType());
1104 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001105 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001106 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001107 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1108 Attr.getParameterName() << ParamTy << Ty;
1109 return;
1110 }
Mike Stumpbf916502009-07-24 19:02:52 +00001111
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001112 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001113}
1114
Mike Stumpbf916502009-07-24 19:02:52 +00001115/// Handle __attribute__((format_arg((idx)))) attribute based on
1116/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1117static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001118 if (Attr.getNumArgs() != 1) {
1119 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1120 return;
1121 }
1122 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1123 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1124 << Attr.getName() << 0 /*function*/;
1125 return;
1126 }
Mike Stumpbf916502009-07-24 19:02:52 +00001127 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1128 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001129 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1130 unsigned FirstIdx = 1;
1131 // checks for the 2nd argument
1132 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1133 llvm::APSInt Idx(32);
1134 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1136 << "format" << 2 << IdxExpr->getSourceRange();
1137 return;
1138 }
Mike Stumpbf916502009-07-24 19:02:52 +00001139
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001140 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1141 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1142 << "format" << 2 << IdxExpr->getSourceRange();
1143 return;
1144 }
Mike Stumpbf916502009-07-24 19:02:52 +00001145
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001146 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001147
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001148 // make sure the format string is really a string
1149 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001150
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001151 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1152 if (not_nsstring_type &&
1153 !isCFStringType(Ty, S.Context) &&
1154 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001155 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001156 // FIXME: Should highlight the actual expression that has the wrong type.
1157 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001158 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001159 << IdxExpr->getSourceRange();
1160 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001161 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001162 Ty = getFunctionOrMethodResultType(d);
1163 if (!isNSStringType(Ty, S.Context) &&
1164 !isCFStringType(Ty, S.Context) &&
1165 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001166 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001167 // FIXME: Should highlight the actual expression that has the wrong type.
1168 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001169 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001170 << IdxExpr->getSourceRange();
1171 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001172 }
1173
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001174 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001175}
1176
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001177enum FormatAttrKind {
1178 CFStringFormat,
1179 NSStringFormat,
1180 StrftimeFormat,
1181 SupportedFormat,
1182 InvalidFormat
1183};
1184
1185/// getFormatAttrKind - Map from format attribute names to supported format
1186/// types.
1187static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1188 // Check for formats that get handled specially.
1189 if (Format == "NSString")
1190 return NSStringFormat;
1191 if (Format == "CFString")
1192 return CFStringFormat;
1193 if (Format == "strftime")
1194 return StrftimeFormat;
1195
1196 // Otherwise, check for supported formats.
1197 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1198 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1199 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1200 Format == "zcmn_err")
1201 return SupportedFormat;
1202
1203 return InvalidFormat;
1204}
1205
Mike Stumpbf916502009-07-24 19:02:52 +00001206/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1207/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001208static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001209
Chris Lattner545dd342008-06-28 23:36:30 +00001210 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001211 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001212 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001213 return;
1214 }
1215
Chris Lattner545dd342008-06-28 23:36:30 +00001216 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001218 return;
1219 }
1220
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001221 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001222 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001223 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001224 return;
1225 }
1226
Daniel Dunbar35682492008-09-26 04:12:28 +00001227 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228 unsigned FirstIdx = 1;
1229
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001230 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001231
1232 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001233 if (Format.startswith("__") && Format.endswith("__"))
1234 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001235
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001236 // Check for supported formats.
1237 FormatAttrKind Kind = getFormatAttrKind(Format);
1238 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001239 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001240 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 return;
1242 }
1243
1244 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001245 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001246 llvm::APSInt Idx(32);
1247 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001248 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001249 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001250 return;
1251 }
1252
Anders Carlsson4fb77202009-08-25 14:12:34 +00001253 // FIXME: We should handle the implicit 'this' parameter in a more generic
1254 // way that can be used for other arguments.
1255 bool HasImplicitThisParam = false;
1256 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1257 if (MD->isInstance()) {
1258 HasImplicitThisParam = true;
1259 NumArgs++;
1260 }
1261 }
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001264 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001265 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001266 return;
1267 }
1268
1269 // FIXME: Do we need to bounds check?
1270 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001271
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001272 if (HasImplicitThisParam) {
1273 if (ArgIdx == 0) {
1274 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1275 << "a string type" << IdxExpr->getSourceRange();
1276 return;
1277 }
1278 ArgIdx--;
1279 }
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Chris Lattner6b6b5372008-06-26 18:38:35 +00001281 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001282 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001284 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001285 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001286 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1287 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001288 return;
1289 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001290 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001291 // FIXME: do we need to check if the type is NSString*? What are the
1292 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001293 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001294 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001295 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1296 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001297 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001298 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001299 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001300 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001301 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001302 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1303 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001304 return;
1305 }
1306
1307 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001308 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001309 llvm::APSInt FirstArg(32);
1310 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001312 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313 return;
1314 }
1315
1316 // check if the function is variadic if the 3rd argument non-zero
1317 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001318 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001319 ++NumArgs; // +1 for ...
1320 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001321 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001322 return;
1323 }
1324 }
1325
Chris Lattner3c73c412008-11-19 08:23:25 +00001326 // strftime requires FirstArg to be 0 because it doesn't read from any
1327 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001328 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001329 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001330 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1331 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001332 return;
1333 }
1334 // if 0 it disables parameter checking (to use with e.g. va_list)
1335 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001337 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001338 return;
1339 }
1340
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001341 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1342 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001343}
1344
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001345static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1346 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001348 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 return;
1351 }
1352
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001353 // Try to find the underlying union declaration.
1354 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001355 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001356 if (TD && TD->getUnderlyingType()->isUnionType())
1357 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1358 else
1359 RD = dyn_cast<RecordDecl>(d);
1360
1361 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001362 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001363 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001364 return;
1365 }
1366
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001367 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001368 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001369 diag::warn_transparent_union_attribute_not_definition);
1370 return;
1371 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001372
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001373 RecordDecl::field_iterator Field = RD->field_begin(),
1374 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001375 if (Field == FieldEnd) {
1376 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1377 return;
1378 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001379
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001380 FieldDecl *FirstField = *Field;
1381 QualType FirstType = FirstField->getType();
1382 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001383 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001384 diag::warn_transparent_union_attribute_floating);
1385 return;
1386 }
1387
1388 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1389 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1390 for (; Field != FieldEnd; ++Field) {
1391 QualType FieldType = Field->getType();
1392 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1393 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1394 // Warn if we drop the attribute.
1395 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001396 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001397 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001398 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001399 diag::warn_transparent_union_attribute_field_size_align)
1400 << isSize << Field->getDeclName() << FieldBits;
1401 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001402 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001403 diag::note_transparent_union_first_field_size_align)
1404 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001405 return;
1406 }
1407 }
1408
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001409 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001410}
1411
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001412static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001413 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001414 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001416 return;
1417 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001418 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1419 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001420
Chris Lattner6b6b5372008-06-26 18:38:35 +00001421 // Make sure that there is a string literal as the annotation's single
1422 // argument.
1423 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001424 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425 return;
1426 }
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001427 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001428}
1429
Chris Lattner803d0802008-06-29 00:43:07 +00001430static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001431 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001432 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001434 return;
1435 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001436
1437 //FIXME: The C++0x version of this attribute has more limited applicabilty
1438 // than GNU's, and should error out when it is used to specify a
1439 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001440
1441 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001442 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001443 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001444 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001445 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001446 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001447 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001448 }
Mike Stumpbf916502009-07-24 19:02:52 +00001449
Chris Lattner49e2d342008-06-28 23:50:44 +00001450 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1451 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001452 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001453 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1454 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001455 return;
1456 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001457 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001459 << alignmentExpr->getSourceRange();
1460 return;
1461 }
1462
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001463 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001464}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001465
Mike Stumpbf916502009-07-24 19:02:52 +00001466/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1467/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001468///
Mike Stumpbf916502009-07-24 19:02:52 +00001469/// Despite what would be logical, the mode attribute is a decl attribute, not a
1470/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1471/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001472static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001473 // This attribute isn't documented, but glibc uses it. It changes
1474 // the width of an int or unsigned int to the specified size.
1475
1476 // Check that there aren't any arguments
1477 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001479 return;
1480 }
1481
1482 IdentifierInfo *Name = Attr.getParameterName();
1483 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001484 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001485 return;
1486 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001487
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001488 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001489
1490 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001491 if (Str.startswith("__") && Str.endswith("__"))
1492 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001493
1494 unsigned DestWidth = 0;
1495 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001496 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001497 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001498 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001499 switch (Str[0]) {
1500 case 'Q': DestWidth = 8; break;
1501 case 'H': DestWidth = 16; break;
1502 case 'S': DestWidth = 32; break;
1503 case 'D': DestWidth = 64; break;
1504 case 'X': DestWidth = 96; break;
1505 case 'T': DestWidth = 128; break;
1506 }
1507 if (Str[1] == 'F') {
1508 IntegerMode = false;
1509 } else if (Str[1] == 'C') {
1510 IntegerMode = false;
1511 ComplexMode = true;
1512 } else if (Str[1] != 'I') {
1513 DestWidth = 0;
1514 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001515 break;
1516 case 4:
1517 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1518 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001519 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001520 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001521 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001522 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001523 break;
1524 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001525 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001526 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001527 break;
1528 }
1529
1530 QualType OldTy;
1531 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1532 OldTy = TD->getUnderlyingType();
1533 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1534 OldTy = VD->getType();
1535 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001536 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1537 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001538 return;
1539 }
Eli Friedman73397492009-03-03 06:41:03 +00001540
John McCall183700f2009-09-21 23:43:11 +00001541 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001542 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1543 else if (IntegerMode) {
1544 if (!OldTy->isIntegralType())
1545 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1546 } else if (ComplexMode) {
1547 if (!OldTy->isComplexType())
1548 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1549 } else {
1550 if (!OldTy->isFloatingType())
1551 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1552 }
1553
Mike Stump390b4cc2009-05-16 07:39:55 +00001554 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1555 // and friends, at least with glibc.
1556 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1557 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001558 // FIXME: Make sure floating-point mappings are accurate
1559 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001560 QualType NewTy;
1561 switch (DestWidth) {
1562 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001563 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001564 return;
1565 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001566 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001567 return;
1568 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001569 if (!IntegerMode) {
1570 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1571 return;
1572 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001573 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001574 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001575 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001576 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001577 break;
1578 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001579 if (!IntegerMode) {
1580 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1581 return;
1582 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001583 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001584 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001585 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001586 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587 break;
1588 case 32:
1589 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001590 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001591 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001592 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001593 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001594 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 break;
1596 case 64:
1597 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001598 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001599 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001600 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001601 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001602 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001603 break;
Eli Friedman73397492009-03-03 06:41:03 +00001604 case 96:
1605 NewTy = S.Context.LongDoubleTy;
1606 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001607 case 128:
1608 if (!IntegerMode) {
1609 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1610 return;
1611 }
1612 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001613 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001614 }
1615
Eli Friedman73397492009-03-03 06:41:03 +00001616 if (ComplexMode) {
1617 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001618 }
1619
1620 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001621 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1622 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001623 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001624 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001625 cast<ValueDecl>(D)->setType(NewTy);
1626}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001627
Mike Stump1feade82009-08-26 22:31:08 +00001628static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001629 // check the attribute arguments.
1630 if (Attr.getNumArgs() > 0) {
1631 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1632 return;
1633 }
Anders Carlssone896d982009-02-13 08:11:52 +00001634
Anders Carlsson5bab7882009-02-19 19:16:48 +00001635 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001636 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001637 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001638 return;
1639 }
Mike Stumpbf916502009-07-24 19:02:52 +00001640
Mike Stump1feade82009-08-26 22:31:08 +00001641 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001642}
1643
Mike Stump1feade82009-08-26 22:31:08 +00001644static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001645 // check the attribute arguments.
1646 if (Attr.getNumArgs() != 0) {
1647 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1648 return;
1649 }
Mike Stumpbf916502009-07-24 19:02:52 +00001650
Chris Lattnerc5197432009-04-14 17:02:11 +00001651 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001653 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001654 return;
1655 }
Mike Stumpbf916502009-07-24 19:02:52 +00001656
Mike Stump1feade82009-08-26 22:31:08 +00001657 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001658}
1659
Chris Lattnercf2a7212009-04-20 19:12:28 +00001660static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001661 // check the attribute arguments.
1662 if (Attr.getNumArgs() != 0) {
1663 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1664 return;
1665 }
Mike Stumpbf916502009-07-24 19:02:52 +00001666
Chris Lattnerc5197432009-04-14 17:02:11 +00001667 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1668 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001670 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001671 return;
1672 }
Mike Stumpbf916502009-07-24 19:02:52 +00001673
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001674 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001675 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001676 return;
1677 }
Mike Stumpbf916502009-07-24 19:02:52 +00001678
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001679 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001680}
1681
Fariborz Jahanianee760332009-03-27 18:38:55 +00001682static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1683 // check the attribute arguments.
1684 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001686 return;
1687 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001688
Fariborz Jahanianee760332009-03-27 18:38:55 +00001689 if (!isFunctionOrMethod(d)) {
1690 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001691 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001692 return;
1693 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001694
1695 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1696 llvm::APSInt NumParams(32);
1697 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1698 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1699 << "regparm" << NumParamsExpr->getSourceRange();
1700 return;
1701 }
1702
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001703 if (S.Context.Target.getRegParmMax() == 0) {
1704 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001705 << NumParamsExpr->getSourceRange();
1706 return;
1707 }
1708
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001709 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001710 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1711 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001712 return;
1713 }
1714
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001715 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001716}
1717
Sean Huntbbd37c62009-11-21 08:43:09 +00001718static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1719 // check the attribute arguments.
1720 if (Attr.getNumArgs() != 0) {
1721 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1722 return;
1723 }
1724
1725 if (!isa<CXXRecordDecl>(d)
1726 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1727 S.Diag(Attr.getLoc(),
1728 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1729 : diag::warn_attribute_wrong_decl_type)
1730 << Attr.getName() << 7 /*virtual method or class*/;
1731 return;
1732 }
Sean Hunt7725e672009-11-25 04:20:27 +00001733
1734 // FIXME: Conform to C++0x redeclaration rules.
1735
1736 if (d->getAttr<FinalAttr>()) {
1737 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1738 return;
1739 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001740
1741 d->addAttr(::new (S.Context) FinalAttr());
1742}
1743
Chris Lattner0744e5f2008-06-29 00:23:49 +00001744//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001745// C++0x member checking attributes
1746//===----------------------------------------------------------------------===//
1747
1748static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1749 if (Attr.getNumArgs() != 0) {
1750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1751 return;
1752 }
1753
1754 if (!isa<CXXRecordDecl>(d)) {
1755 S.Diag(Attr.getLoc(),
1756 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1757 : diag::warn_attribute_wrong_decl_type)
1758 << Attr.getName() << 9 /*class*/;
1759 return;
1760 }
1761
1762 if (d->getAttr<BaseCheckAttr>()) {
1763 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1764 return;
1765 }
1766
1767 d->addAttr(::new (S.Context) BaseCheckAttr());
1768}
1769
1770static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1771 if (Attr.getNumArgs() != 0) {
1772 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1773 return;
1774 }
1775
1776 if (!isa<RecordDecl>(d->getDeclContext())) {
1777 // FIXME: It's not the type that's the problem
1778 S.Diag(Attr.getLoc(),
1779 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1780 : diag::warn_attribute_wrong_decl_type)
1781 << Attr.getName() << 11 /*member*/;
1782 return;
1783 }
1784
1785 // FIXME: Conform to C++0x redeclaration rules.
1786
1787 if (d->getAttr<HidingAttr>()) {
1788 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1789 return;
1790 }
1791
1792 d->addAttr(::new (S.Context) HidingAttr());
1793}
1794
1795static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1796 if (Attr.getNumArgs() != 0) {
1797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1798 return;
1799 }
1800
1801 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1802 // FIXME: It's not the type that's the problem
1803 S.Diag(Attr.getLoc(),
1804 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1805 : diag::warn_attribute_wrong_decl_type)
1806 << Attr.getName() << 10 /*virtual method*/;
1807 return;
1808 }
1809
1810 // FIXME: Conform to C++0x redeclaration rules.
1811
1812 if (d->getAttr<OverrideAttr>()) {
1813 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1814 return;
1815 }
1816
1817 d->addAttr(::new (S.Context) OverrideAttr());
1818}
1819
1820//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001821// Checker-specific attribute handlers.
1822//===----------------------------------------------------------------------===//
1823
1824static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1825 Sema &S) {
1826
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001827 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001828
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001829 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1830 RetTy = MD->getResultType();
1831 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1832 RetTy = FD->getResultType();
1833 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001834 SourceLocation L = Attr.getLoc();
1835 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1836 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001837 return;
1838 }
Mike Stumpbf916502009-07-24 19:02:52 +00001839
Ted Kremenek6217b802009-07-29 21:53:49 +00001840 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001841 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001842 SourceLocation L = Attr.getLoc();
1843 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1844 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001845 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001846 }
Mike Stumpbf916502009-07-24 19:02:52 +00001847
Ted Kremenekb71368d2009-05-09 02:44:38 +00001848 switch (Attr.getKind()) {
1849 default:
1850 assert(0 && "invalid ownership attribute");
1851 return;
1852 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001853 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001854 return;
1855 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001856 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001857 return;
1858 };
1859}
1860
1861//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001862// Top Level Sema Entry Points
1863//===----------------------------------------------------------------------===//
1864
Sebastian Redla89d82c2008-12-21 19:24:58 +00001865/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001866/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001867/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1868/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001869static void ProcessDeclAttribute(Scope *scope, Decl *D,
1870 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001871 if (Attr.isDeclspecAttribute())
1872 // FIXME: Try to deal with __declspec attributes!
1873 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001874 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001875 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001876 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001877 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001878 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001879 // Ignore these, these are type attributes, handled by
1880 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001881 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001882 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1883 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001884 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001885 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001886 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001887 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001888 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1889 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001890 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001891 HandleDependencyAttr (D, Attr, S); break;
1892 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1893 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1894 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1895 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1896 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1897 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001898 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001899 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001900 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001901 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1902 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1903 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1904 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1905 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1906 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1907 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1908 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1909 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1910 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1911 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1912 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001913
1914 // Checker-specific.
1915 case AttributeList::AT_ns_returns_retained:
1916 case AttributeList::AT_cf_returns_retained:
1917 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1918
Nate Begeman6f3d8382009-06-26 06:32:41 +00001919 case AttributeList::AT_reqd_wg_size:
1920 HandleReqdWorkGroupSize(D, Attr, S); break;
1921
Sean Hunt7725e672009-11-25 04:20:27 +00001922 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1923 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1924 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1925 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1926 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1927 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001928 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001929 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1930 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001931 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1932 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001933 case AttributeList::AT_transparent_union:
1934 HandleTransparentUnionAttr(D, Attr, S);
1935 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001936 case AttributeList::AT_objc_exception:
1937 HandleObjCExceptionAttr(D, Attr, S);
1938 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001939 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001940 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1941 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1942 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1943 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1944 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1945 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1946 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1947 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1948 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001949 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001950 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001951 // Just ignore
1952 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001953 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001954 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001955 break;
1956 }
1957}
1958
1959/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1960/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001961void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001962 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001963 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001964 AttrList = AttrList->getNext();
1965 }
1966}
1967
Ryan Flynne25ff832009-07-30 03:15:39 +00001968/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1969/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001970NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001971 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001972 NamedDecl *NewD = 0;
1973 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1974 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1975 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001976 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001977 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1978 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1979 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00001980 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001981 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001982 }
1983 return NewD;
1984}
1985
1986/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1987/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001988void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001989 if (W.getUsed()) return; // only do this once
1990 W.setUsed(true);
1991 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1992 IdentifierInfo *NDId = ND->getIdentifier();
1993 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1994 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1995 NewD->addAttr(::new (Context) WeakAttr());
1996 WeakTopLevelDecl.push_back(NewD);
1997 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1998 // to insert Decl at TU scope, sorry.
1999 DeclContext *SavedContext = CurContext;
2000 CurContext = Context.getTranslationUnitDecl();
2001 PushOnScopeChains(NewD, S);
2002 CurContext = SavedContext;
2003 } else { // just add weak to existing
2004 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002005 }
2006}
2007
Chris Lattner0744e5f2008-06-29 00:23:49 +00002008/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2009/// it, apply them to D. This is a bit tricky because PD can have attributes
2010/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002011void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002012 // Handle #pragma weak
2013 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2014 if (ND->hasLinkage()) {
2015 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2016 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002017 // Identifier referenced by #pragma weak before it was declared
2018 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002019 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2020 }
2021 }
2022 }
2023
Chris Lattner0744e5f2008-06-29 00:23:49 +00002024 // Apply decl attributes from the DeclSpec if present.
2025 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002026 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002027
Chris Lattner0744e5f2008-06-29 00:23:49 +00002028 // Walk the declarator structure, applying decl attributes that were in a type
2029 // position to the decl itself. This handles cases like:
2030 // int *__attr__(x)** D;
2031 // when X is a decl attribute.
2032 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2033 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002034 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002035
Chris Lattner0744e5f2008-06-29 00:23:49 +00002036 // Finally, apply any attributes on the decl itself.
2037 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002038 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002039}
John McCall54abf7d2009-11-04 02:18:39 +00002040
2041/// PushParsingDeclaration - Enter a new "scope" of deprecation
2042/// warnings.
2043///
2044/// The state token we use is the start index of this scope
2045/// on the warning stack.
2046Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2047 ParsingDeclDepth++;
2048 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2049}
2050
2051static bool isDeclDeprecated(Decl *D) {
2052 do {
2053 if (D->hasAttr<DeprecatedAttr>())
2054 return true;
2055 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2056 return false;
2057}
2058
2059void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2060 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2061 ParsingDeclDepth--;
2062
2063 if (DelayedDeprecationWarnings.empty())
2064 return;
2065
2066 unsigned SavedIndex = (unsigned) S;
2067 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2068 "saved index is out of bounds");
2069
2070 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2071 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2072 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2073 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2074 if (ND) {
2075 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2076
2077 // Prevent this from triggering multiple times.
2078 ND = 0;
2079 }
2080 }
2081 }
2082
2083 DelayedDeprecationWarnings.set_size(SavedIndex);
2084}
2085
2086void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2087 // Delay if we're currently parsing a declaration.
2088 if (ParsingDeclDepth) {
2089 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2090 return;
2091 }
2092
2093 // Otherwise, don't warn if our current context is deprecated.
2094 if (isDeclDeprecated(cast<Decl>(CurContext)))
2095 return;
2096
2097 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2098}