blob: a391a0eaed1ca63fdd3541db0a7784d3f978ac43 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremeneka18d7d82009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000039
Chris Lattner6b6b5372008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000044
John McCall183700f2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000046}
47
Daniel Dunbar35682492008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopesd20254f2009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000062}
63
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076}
77
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000084 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000106
Chris Lattner89951a82009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000108}
109
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
121 return BD->IsVariadic();
122 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner6b6b5372008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000131
John McCall183700f2009-09-21 23:43:11 +0000132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000133 if (!ClsT)
134 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000135
Chris Lattner6b6b5372008-06-26 18:38:35 +0000136 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000137
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
153 if (RD->getTagKind() != TagDecl::TK_struct)
154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpbf916502009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000172 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 }
Mike Stumpbf916502009-07-24 19:02:52 +0000174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204}
205
Chris Lattner803d0802008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 else
Anders Carlssona860e752009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226}
227
Ted Kremenek96329d42008-07-15 22:26:48 +0000228static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpbf916502009-07-24 19:02:52 +0000234
Ted Kremenek96329d42008-07-15 22:26:48 +0000235 // The IBOutlet attribute only applies to instance variables of Objective-C
236 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000237 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000238 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000239 else
Ted Kremenek32742602009-02-17 22:20:20 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000241}
242
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000243static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000244 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
245 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000246 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000248 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000249 return;
250 }
Mike Stumpbf916502009-07-24 19:02:52 +0000251
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000252 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000253
254 // The nonnull attribute only applies to pointers.
255 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000256
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000257 for (AttributeList::arg_iterator I=Attr.arg_begin(),
258 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000259
260
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000261 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000262 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000263 llvm::APSInt ArgNum(32);
264 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
266 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000267 return;
268 }
Mike Stumpbf916502009-07-24 19:02:52 +0000269
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000270 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000271
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000272 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000274 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000275 return;
276 }
Mike Stumpbf916502009-07-24 19:02:52 +0000277
Ted Kremenek465172f2008-07-21 22:09:15 +0000278 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000279
280 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000281 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000282 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000283 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000284 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
285 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000286 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000287 }
Mike Stumpbf916502009-07-24 19:02:52 +0000288
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000289 NonNullArgs.push_back(x);
290 }
Mike Stumpbf916502009-07-24 19:02:52 +0000291
292 // If no arguments were specified to __attribute__((nonnull)) then all pointer
293 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000294 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000295 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
296 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000297 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000298 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000299 }
Mike Stumpbf916502009-07-24 19:02:52 +0000300
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000301 if (NonNullArgs.empty()) {
302 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
303 return;
304 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000305 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000306
307 unsigned* start = &NonNullArgs[0];
308 unsigned size = NonNullArgs.size();
309 std::sort(start, start + size);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000310 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000311}
312
Chris Lattner803d0802008-06-29 00:43:07 +0000313static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000314 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000315 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000317 return;
318 }
Mike Stumpbf916502009-07-24 19:02:52 +0000319
Chris Lattner545dd342008-06-28 23:36:30 +0000320 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000321 Arg = Arg->IgnoreParenCasts();
322 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000323
Chris Lattner6b6b5372008-06-26 18:38:35 +0000324 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000326 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000327 return;
328 }
Mike Stumpbf916502009-07-24 19:02:52 +0000329
Chris Lattner6b6b5372008-06-26 18:38:35 +0000330 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000331
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000332 d->addAttr(::new (S.Context) AliasAttr(Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000333}
334
Mike Stumpbf916502009-07-24 19:02:52 +0000335static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000336 Sema &S) {
337 // check the attribute arguments.
338 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000340 return;
341 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000342
Chris Lattnerc5197432009-04-14 17:02:11 +0000343 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000344 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000345 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000346 return;
347 }
Mike Stumpbf916502009-07-24 19:02:52 +0000348
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000349 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000350}
351
Ryan Flynn76168e22009-08-09 20:07:29 +0000352static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
353 // check the attribute arguments.
354 if (Attr.getNumArgs() != 0) {
355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
356 return;
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000360 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000361 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
362 d->addAttr(::new (S.Context) MallocAttr());
363 return;
364 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000365 }
366
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000367 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000368}
369
Ted Kremenekb7252322009-04-10 00:01:14 +0000370static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000371 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000372 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000373 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000375 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000376 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000377
Mike Stump19c30c02009-04-29 19:03:13 +0000378 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
379 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000380 if (VD == 0 || (!VD->getType()->isBlockPointerType()
381 && !VD->getType()->isFunctionPointerType())) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000382 S.Diag(Attr.getLoc(),
383 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
384 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000385 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000386 return false;
387 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000388 }
Mike Stumpbf916502009-07-24 19:02:52 +0000389
Ted Kremenekb7252322009-04-10 00:01:14 +0000390 return true;
391}
392
393static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000394 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000395 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000396}
397
398static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
399 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000400 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000401 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000402}
403
Sean Huntbbd37c62009-11-21 08:43:09 +0000404static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
405 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
407 << Attr.getName() << 8; /*function, method, or parameter*/
408 return;
409 }
410 // FIXME: Actually store the attribute on the declaration
411}
412
Ted Kremenek73798892008-07-25 04:39:19 +0000413static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
414 // check the attribute arguments.
415 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000416 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000417 return;
418 }
Mike Stumpbf916502009-07-24 19:02:52 +0000419
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000420 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000421 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000422 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000423 return;
424 }
Mike Stumpbf916502009-07-24 19:02:52 +0000425
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000426 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000427}
428
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000429static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
430 // check the attribute arguments.
431 if (Attr.getNumArgs() != 0) {
432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
433 return;
434 }
Mike Stumpbf916502009-07-24 19:02:52 +0000435
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000436 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000437 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000438 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
439 return;
440 }
441 } else if (!isFunctionOrMethod(d)) {
442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000443 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000444 return;
445 }
Mike Stumpbf916502009-07-24 19:02:52 +0000446
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000447 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000448}
449
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000450static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
451 // check the attribute arguments.
452 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
454 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000455 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000456 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000457
458 int priority = 65535; // FIXME: Do not hardcode such constants.
459 if (Attr.getNumArgs() > 0) {
460 Expr *E = static_cast<Expr *>(Attr.getArg(0));
461 llvm::APSInt Idx(32);
462 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000464 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000465 return;
466 }
467 priority = Idx.getZExtValue();
468 }
Mike Stumpbf916502009-07-24 19:02:52 +0000469
Chris Lattnerc5197432009-04-14 17:02:11 +0000470 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000472 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000473 return;
474 }
475
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000476 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000477}
478
479static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
480 // check the attribute arguments.
481 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000482 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
483 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000484 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000485 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000486
487 int priority = 65535; // FIXME: Do not hardcode such constants.
488 if (Attr.getNumArgs() > 0) {
489 Expr *E = static_cast<Expr *>(Attr.getArg(0));
490 llvm::APSInt Idx(32);
491 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000492 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000493 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000494 return;
495 }
496 priority = Idx.getZExtValue();
497 }
Mike Stumpbf916502009-07-24 19:02:52 +0000498
Anders Carlsson6782fc62008-08-22 22:10:48 +0000499 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000500 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000501 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000502 return;
503 }
504
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000505 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000506}
507
Chris Lattner803d0802008-06-29 00:43:07 +0000508static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000509 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000510 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000512 return;
513 }
Mike Stumpbf916502009-07-24 19:02:52 +0000514
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000515 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000516}
517
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000518static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
519 // check the attribute arguments.
520 if (Attr.getNumArgs() != 0) {
521 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
522 return;
523 }
Mike Stumpbf916502009-07-24 19:02:52 +0000524
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000525 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000526}
527
Chris Lattner803d0802008-06-29 00:43:07 +0000528static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000529 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000530 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000531 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000532 return;
533 }
Mike Stumpbf916502009-07-24 19:02:52 +0000534
Chris Lattner545dd342008-06-28 23:36:30 +0000535 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000536 Arg = Arg->IgnoreParenCasts();
537 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000538
Chris Lattner6b6b5372008-06-26 18:38:35 +0000539 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000541 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000542 return;
543 }
Mike Stumpbf916502009-07-24 19:02:52 +0000544
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000545 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000546 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000547
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000548 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000549 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000550 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000551 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000552 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000553 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000554 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000555 type = VisibilityAttr::ProtectedVisibility;
556 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000557 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000558 return;
559 }
Mike Stumpbf916502009-07-24 19:02:52 +0000560
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000561 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000562}
563
Chris Lattner0db29ec2009-02-14 08:09:34 +0000564static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
565 Sema &S) {
566 if (Attr.getNumArgs() != 0) {
567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
568 return;
569 }
Mike Stumpbf916502009-07-24 19:02:52 +0000570
Chris Lattner0db29ec2009-02-14 08:09:34 +0000571 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
572 if (OCI == 0) {
573 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
574 return;
575 }
Mike Stumpbf916502009-07-24 19:02:52 +0000576
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000577 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000578}
579
580static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000581 if (Attr.getNumArgs() != 0) {
582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
583 return;
584 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000585 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000586 QualType T = TD->getUnderlyingType();
587 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000588 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000589 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
590 return;
591 }
592 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000593 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000594}
595
Mike Stumpbf916502009-07-24 19:02:52 +0000596static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000597HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
598 if (Attr.getNumArgs() != 0) {
599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
600 return;
601 }
602
603 if (!isa<FunctionDecl>(D)) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
605 return;
606 }
607
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000608 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000609}
610
Steve Naroff9eae5762008-09-18 16:44:58 +0000611static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000612 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000613 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000614 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000615 return;
616 }
Mike Stumpbf916502009-07-24 19:02:52 +0000617
Steve Naroff9eae5762008-09-18 16:44:58 +0000618 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000620 return;
621 }
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Steve Naroff9eae5762008-09-18 16:44:58 +0000623 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000624 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000625 type = BlocksAttr::ByRef;
626 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000627 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000629 return;
630 }
Mike Stumpbf916502009-07-24 19:02:52 +0000631
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000632 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000633}
634
Anders Carlsson77091822008-10-05 18:05:59 +0000635static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
636 // check the attribute arguments.
637 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
639 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000640 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000641 }
642
Anders Carlsson77091822008-10-05 18:05:59 +0000643 int sentinel = 0;
644 if (Attr.getNumArgs() > 0) {
645 Expr *E = static_cast<Expr *>(Attr.getArg(0));
646 llvm::APSInt Idx(32);
647 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000650 return;
651 }
652 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000653
Anders Carlsson77091822008-10-05 18:05:59 +0000654 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000655 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
656 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000657 return;
658 }
659 }
660
661 int nullPos = 0;
662 if (Attr.getNumArgs() > 1) {
663 Expr *E = static_cast<Expr *>(Attr.getArg(1));
664 llvm::APSInt Idx(32);
665 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000667 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000668 return;
669 }
670 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000671
Anders Carlsson77091822008-10-05 18:05:59 +0000672 if (nullPos > 1 || nullPos < 0) {
673 // FIXME: This error message could be improved, it would be nice
674 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000675 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
676 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000677 return;
678 }
679 }
680
681 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000682 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000683 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000684
Chris Lattner897cd902009-03-17 23:03:47 +0000685 if (isa<FunctionNoProtoType>(FT)) {
686 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
687 return;
688 }
Mike Stumpbf916502009-07-24 19:02:52 +0000689
Chris Lattner897cd902009-03-17 23:03:47 +0000690 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000691 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000692 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000693 }
Anders Carlsson77091822008-10-05 18:05:59 +0000694 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
695 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000696 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000697 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000698 }
699 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000700 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
701 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000702 ;
703 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000704 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000705 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000706 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000707 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000708 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000709 int m = Ty->isFunctionPointerType() ? 0 : 1;
710 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000711 return;
712 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000713 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000714 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000715 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000716 return;
717 }
Anders Carlsson77091822008-10-05 18:05:59 +0000718 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000720 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000721 return;
722 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000723 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000724}
725
Chris Lattner026dc962009-02-14 07:37:35 +0000726static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
727 // check the attribute arguments.
728 if (Attr.getNumArgs() != 0) {
729 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
730 return;
731 }
732
Nuno Lopesd20254f2009-12-20 23:11:08 +0000733 if (!isFunctionOrMethod(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000735 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000736 return;
737 }
Mike Stumpbf916502009-07-24 19:02:52 +0000738
Nuno Lopesf8577982009-12-22 23:59:52 +0000739 if (getFunctionType(D)->getResultType()->isVoidType()) {
740 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
741 << Attr.getName();
742 return;
743 }
744
Nuno Lopesd20254f2009-12-20 23:11:08 +0000745 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000746}
747
748static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000749 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000750 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000752 return;
753 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000754
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000755 /* weak only applies to non-static declarations */
756 bool isStatic = false;
757 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
758 isStatic = VD->getStorageClass() == VarDecl::Static;
759 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
760 isStatic = FD->getStorageClass() == FunctionDecl::Static;
761 }
762 if (isStatic) {
763 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
764 dyn_cast<NamedDecl>(D)->getNameAsString();
765 return;
766 }
767
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000768 // TODO: could also be applied to methods?
769 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000771 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000772 return;
773 }
Mike Stumpbf916502009-07-24 19:02:52 +0000774
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000775 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000776}
777
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000778static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
779 // check the attribute arguments.
780 if (Attr.getNumArgs() != 0) {
781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
782 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000783 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000784
785 // weak_import only applies to variable & function declarations.
786 bool isDef = false;
787 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
788 isDef = (!VD->hasExternalStorage() || VD->getInit());
789 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000790 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000791 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
792 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000793 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000794 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000795 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000796 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000797 return;
798 }
799
800 // Merge should handle any subsequent violations.
801 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000802 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000803 diag::warn_attribute_weak_import_invalid_on_definition)
804 << "weak_import" << 2 /*variable and function*/;
805 return;
806 }
807
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000808 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000809}
810
Chris Lattner026dc962009-02-14 07:37:35 +0000811static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000812 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000813 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000814 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000815 return;
816 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000817
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000818 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000819 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000820 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000821 return;
822 }
823
Chris Lattner026dc962009-02-14 07:37:35 +0000824 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000825 if (!FD) {
826 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000827 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000828 return;
829 }
830
831 // Currently, the dllimport attribute is ignored for inlined functions.
832 // Warning is emitted.
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000833 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000834 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
835 return;
836 }
837
838 // The attribute is also overridden by a subsequent declaration as dllexport.
839 // Warning is emitted.
840 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
841 nextAttr = nextAttr->getNext()) {
842 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
844 return;
845 }
846 }
847
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000848 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000849 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
850 return;
851 }
852
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000853 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000854}
855
Chris Lattner026dc962009-02-14 07:37:35 +0000856static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000857 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000858 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000860 return;
861 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000862
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000863 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000864 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000865 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000866 return;
867 }
868
Chris Lattner026dc962009-02-14 07:37:35 +0000869 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000870 if (!FD) {
871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000872 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000873 return;
874 }
875
Mike Stumpbf916502009-07-24 19:02:52 +0000876 // Currently, the dllexport attribute is ignored for inlined functions, unless
877 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000878 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000879 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
881 return;
882 }
883
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000884 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000885}
886
Nate Begeman6f3d8382009-06-26 06:32:41 +0000887static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
888 Sema &S) {
889 // Attribute has 3 arguments.
890 if (Attr.getNumArgs() != 3) {
891 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
892 return;
893 }
894
895 unsigned WGSize[3];
896 for (unsigned i = 0; i < 3; ++i) {
897 Expr *E = static_cast<Expr *>(Attr.getArg(i));
898 llvm::APSInt ArgNum(32);
899 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
901 << "reqd_work_group_size" << E->getSourceRange();
902 return;
903 }
904 WGSize[i] = (unsigned) ArgNum.getZExtValue();
905 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000906 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000907 WGSize[2]));
908}
909
Chris Lattner026dc962009-02-14 07:37:35 +0000910static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000911 // Attribute has no arguments.
912 if (Attr.getNumArgs() != 1) {
913 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
914 return;
915 }
916
917 // Make sure that there is a string literal as the sections's single
918 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000919 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
920 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000921 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000922 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000923 return;
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattner797c3c42009-08-10 19:03:04 +0000926 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000927 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000928 if (!Error.empty()) {
929 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
930 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000931 return;
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000934 // This attribute cannot be applied to local variables.
935 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
936 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
937 return;
938 }
939
940 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000941}
942
Eli Friedman8f4c59e2009-11-09 18:38:53 +0000943static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
944 // Attribute has no arguments.
945 if (Attr.getNumArgs() != 0) {
946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
947 return;
948 }
949
950 // Attribute can be applied only to functions.
951 if (!isa<FunctionDecl>(d)) {
952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
953 << Attr.getName() << 0 /*function*/;
954 return;
955 }
956
957 // cdecl and fastcall attributes are mutually incompatible.
958 if (d->getAttr<FastCallAttr>()) {
959 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
960 << "cdecl" << "fastcall";
961 return;
962 }
963
964 // cdecl and stdcall attributes are mutually incompatible.
965 if (d->getAttr<StdCallAttr>()) {
966 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
967 << "cdecl" << "stdcall";
968 return;
969 }
970
971 d->addAttr(::new (S.Context) CDeclAttr());
972}
973
974
Chris Lattner803d0802008-06-29 00:43:07 +0000975static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000976 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000977 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000979 return;
980 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000981
982 // Attribute can be applied only to functions.
983 if (!isa<FunctionDecl>(d)) {
984 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000985 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000986 return;
987 }
988
989 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000990 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000991 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
992 << "stdcall" << "fastcall";
993 return;
994 }
995
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000996 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000997}
998
John McCall9112b932009-11-04 03:36:09 +0000999/// Diagnose the use of a non-standard calling convention on the given
1000/// function.
1001static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1002 SourceLocation Loc, Sema &S) {
1003 if (!D->hasPrototype()) {
1004 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1005 return;
1006 }
1007
1008 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1009 if (T->isVariadic()) {
1010 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1011 return;
1012 }
1013}
1014
Chris Lattner803d0802008-06-29 00:43:07 +00001015static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001016 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001017 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001018 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001019 return;
1020 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001021
1022 if (!isa<FunctionDecl>(d)) {
1023 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001024 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001025 return;
1026 }
1027
John McCall9112b932009-11-04 03:36:09 +00001028 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1029
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001030 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001031 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001032 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1033 << "fastcall" << "stdcall";
1034 return;
1035 }
1036
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001037 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001038}
1039
Chris Lattner803d0802008-06-29 00:43:07 +00001040static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001041 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001042 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001044 return;
1045 }
Mike Stumpbf916502009-07-24 19:02:52 +00001046
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001047 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001048}
1049
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001050static void HandleConstAttr(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) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001058}
1059
1060static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1061 // check the attribute arguments.
1062 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001063 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001064 return;
1065 }
Mike Stumpbf916502009-07-24 19:02:52 +00001066
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001067 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001068}
1069
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001070static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001071 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001072 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1073 return;
1074 }
Mike Stumpbf916502009-07-24 19:02:52 +00001075
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001076 if (Attr.getNumArgs() != 0) {
1077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1078 return;
1079 }
Mike Stumpbf916502009-07-24 19:02:52 +00001080
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001081 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001083 if (!VD || !VD->hasLocalStorage()) {
1084 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1085 return;
1086 }
Mike Stumpbf916502009-07-24 19:02:52 +00001087
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001088 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001089 NamedDecl *CleanupDecl
1090 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1091 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001092 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001094 Attr.getParameterName();
1095 return;
1096 }
Mike Stumpbf916502009-07-24 19:02:52 +00001097
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001098 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1099 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001100 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001101 Attr.getParameterName();
1102 return;
1103 }
1104
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001105 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001106 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001107 Attr.getParameterName();
1108 return;
1109 }
Mike Stumpbf916502009-07-24 19:02:52 +00001110
Anders Carlsson89941c12009-02-07 23:16:50 +00001111 // We're currently more strict than GCC about what function types we accept.
1112 // If this ever proves to be a problem it should be easy to fix.
1113 QualType Ty = S.Context.getPointerType(VD->getType());
1114 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001115 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001116 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001117 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1118 Attr.getParameterName() << ParamTy << Ty;
1119 return;
1120 }
Mike Stumpbf916502009-07-24 19:02:52 +00001121
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001122 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001123}
1124
Mike Stumpbf916502009-07-24 19:02:52 +00001125/// Handle __attribute__((format_arg((idx)))) attribute based on
1126/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1127static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001128 if (Attr.getNumArgs() != 1) {
1129 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1130 return;
1131 }
1132 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1133 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1134 << Attr.getName() << 0 /*function*/;
1135 return;
1136 }
Mike Stumpbf916502009-07-24 19:02:52 +00001137 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1138 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001139 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1140 unsigned FirstIdx = 1;
1141 // checks for the 2nd argument
1142 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1143 llvm::APSInt Idx(32);
1144 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1145 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1146 << "format" << 2 << IdxExpr->getSourceRange();
1147 return;
1148 }
Mike Stumpbf916502009-07-24 19:02:52 +00001149
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001150 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1151 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1152 << "format" << 2 << IdxExpr->getSourceRange();
1153 return;
1154 }
Mike Stumpbf916502009-07-24 19:02:52 +00001155
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001156 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001157
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001158 // make sure the format string is really a string
1159 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001160
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001161 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1162 if (not_nsstring_type &&
1163 !isCFStringType(Ty, S.Context) &&
1164 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001165 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001166 // FIXME: Should highlight the actual expression that has the wrong type.
1167 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001168 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001169 << IdxExpr->getSourceRange();
1170 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001171 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001172 Ty = getFunctionOrMethodResultType(d);
1173 if (!isNSStringType(Ty, S.Context) &&
1174 !isCFStringType(Ty, S.Context) &&
1175 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001176 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001177 // FIXME: Should highlight the actual expression that has the wrong type.
1178 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001179 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001180 << IdxExpr->getSourceRange();
1181 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001182 }
1183
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001184 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001185}
1186
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001187enum FormatAttrKind {
1188 CFStringFormat,
1189 NSStringFormat,
1190 StrftimeFormat,
1191 SupportedFormat,
1192 InvalidFormat
1193};
1194
1195/// getFormatAttrKind - Map from format attribute names to supported format
1196/// types.
1197static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1198 // Check for formats that get handled specially.
1199 if (Format == "NSString")
1200 return NSStringFormat;
1201 if (Format == "CFString")
1202 return CFStringFormat;
1203 if (Format == "strftime")
1204 return StrftimeFormat;
1205
1206 // Otherwise, check for supported formats.
1207 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1208 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1209 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1210 Format == "zcmn_err")
1211 return SupportedFormat;
1212
1213 return InvalidFormat;
1214}
1215
Mike Stumpbf916502009-07-24 19:02:52 +00001216/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1217/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001218static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001219
Chris Lattner545dd342008-06-28 23:36:30 +00001220 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001221 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001222 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001223 return;
1224 }
1225
Chris Lattner545dd342008-06-28 23:36:30 +00001226 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228 return;
1229 }
1230
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001231 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001232 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001233 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001234 return;
1235 }
1236
Daniel Dunbar35682492008-09-26 04:12:28 +00001237 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001238 unsigned FirstIdx = 1;
1239
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001240 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241
1242 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001243 if (Format.startswith("__") && Format.endswith("__"))
1244 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001245
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001246 // Check for supported formats.
1247 FormatAttrKind Kind = getFormatAttrKind(Format);
1248 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001250 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001251 return;
1252 }
1253
1254 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001255 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001256 llvm::APSInt Idx(32);
1257 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001259 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260 return;
1261 }
1262
Anders Carlsson4fb77202009-08-25 14:12:34 +00001263 // FIXME: We should handle the implicit 'this' parameter in a more generic
1264 // way that can be used for other arguments.
1265 bool HasImplicitThisParam = false;
1266 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1267 if (MD->isInstance()) {
1268 HasImplicitThisParam = true;
1269 NumArgs++;
1270 }
1271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Chris Lattner6b6b5372008-06-26 18:38:35 +00001273 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001274 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001275 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001276 return;
1277 }
1278
1279 // FIXME: Do we need to bounds check?
1280 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001281
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001282 if (HasImplicitThisParam) {
1283 if (ArgIdx == 0) {
1284 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1285 << "a string type" << IdxExpr->getSourceRange();
1286 return;
1287 }
1288 ArgIdx--;
1289 }
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Chris Lattner6b6b5372008-06-26 18:38:35 +00001291 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001292 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001293
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001294 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001295 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001296 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1297 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001298 return;
1299 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001300 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001301 // FIXME: do we need to check if the type is NSString*? What are the
1302 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001303 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001304 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001305 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1306 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001307 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001308 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001309 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001310 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001311 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001312 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1313 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001314 return;
1315 }
1316
1317 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001318 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001319 llvm::APSInt FirstArg(32);
1320 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001322 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001323 return;
1324 }
1325
1326 // check if the function is variadic if the 3rd argument non-zero
1327 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001328 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001329 ++NumArgs; // +1 for ...
1330 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001331 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001332 return;
1333 }
1334 }
1335
Chris Lattner3c73c412008-11-19 08:23:25 +00001336 // strftime requires FirstArg to be 0 because it doesn't read from any
1337 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001338 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001339 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001340 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1341 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001342 return;
1343 }
1344 // if 0 it disables parameter checking (to use with e.g. va_list)
1345 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001346 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001347 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001348 return;
1349 }
1350
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001351 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1352 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001353}
1354
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001355static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1356 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001357 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001358 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001359 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001360 return;
1361 }
1362
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001363 // Try to find the underlying union declaration.
1364 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001365 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001366 if (TD && TD->getUnderlyingType()->isUnionType())
1367 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1368 else
1369 RD = dyn_cast<RecordDecl>(d);
1370
1371 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001372 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001373 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001374 return;
1375 }
1376
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001377 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001378 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001379 diag::warn_transparent_union_attribute_not_definition);
1380 return;
1381 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001382
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001383 RecordDecl::field_iterator Field = RD->field_begin(),
1384 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001385 if (Field == FieldEnd) {
1386 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1387 return;
1388 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001389
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001390 FieldDecl *FirstField = *Field;
1391 QualType FirstType = FirstField->getType();
1392 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001393 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001394 diag::warn_transparent_union_attribute_floating);
1395 return;
1396 }
1397
1398 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1399 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1400 for (; Field != FieldEnd; ++Field) {
1401 QualType FieldType = Field->getType();
1402 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1403 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1404 // Warn if we drop the attribute.
1405 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001406 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001407 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001408 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001409 diag::warn_transparent_union_attribute_field_size_align)
1410 << isSize << Field->getDeclName() << FieldBits;
1411 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001412 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001413 diag::note_transparent_union_first_field_size_align)
1414 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001415 return;
1416 }
1417 }
1418
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001419 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001420}
1421
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001422static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001423 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001424 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001426 return;
1427 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001428 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1429 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001430
Chris Lattner6b6b5372008-06-26 18:38:35 +00001431 // Make sure that there is a string literal as the annotation's single
1432 // argument.
1433 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001434 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001435 return;
1436 }
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001437 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001438}
1439
Chris Lattner803d0802008-06-29 00:43:07 +00001440static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001441 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001442 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444 return;
1445 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001446
1447 //FIXME: The C++0x version of this attribute has more limited applicabilty
1448 // than GNU's, and should error out when it is used to specify a
1449 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001450
1451 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001452 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001454 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001455 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001456 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001457 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001458 }
Mike Stumpbf916502009-07-24 19:02:52 +00001459
Chris Lattner49e2d342008-06-28 23:50:44 +00001460 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1461 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001462 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1464 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001465 return;
1466 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001467 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001468 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001469 << alignmentExpr->getSourceRange();
1470 return;
1471 }
1472
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001473 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001474}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001475
Mike Stumpbf916502009-07-24 19:02:52 +00001476/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1477/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001478///
Mike Stumpbf916502009-07-24 19:02:52 +00001479/// Despite what would be logical, the mode attribute is a decl attribute, not a
1480/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1481/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001482static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001483 // This attribute isn't documented, but glibc uses it. It changes
1484 // the width of an int or unsigned int to the specified size.
1485
1486 // Check that there aren't any arguments
1487 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001489 return;
1490 }
1491
1492 IdentifierInfo *Name = Attr.getParameterName();
1493 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001494 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001495 return;
1496 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001497
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001498 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001499
1500 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001501 if (Str.startswith("__") && Str.endswith("__"))
1502 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001503
1504 unsigned DestWidth = 0;
1505 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001506 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001507 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001508 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001509 switch (Str[0]) {
1510 case 'Q': DestWidth = 8; break;
1511 case 'H': DestWidth = 16; break;
1512 case 'S': DestWidth = 32; break;
1513 case 'D': DestWidth = 64; break;
1514 case 'X': DestWidth = 96; break;
1515 case 'T': DestWidth = 128; break;
1516 }
1517 if (Str[1] == 'F') {
1518 IntegerMode = false;
1519 } else if (Str[1] == 'C') {
1520 IntegerMode = false;
1521 ComplexMode = true;
1522 } else if (Str[1] != 'I') {
1523 DestWidth = 0;
1524 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001525 break;
1526 case 4:
1527 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1528 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001529 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001530 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001531 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001532 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001533 break;
1534 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001535 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001536 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001537 break;
1538 }
1539
1540 QualType OldTy;
1541 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1542 OldTy = TD->getUnderlyingType();
1543 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1544 OldTy = VD->getType();
1545 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001546 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1547 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001548 return;
1549 }
Eli Friedman73397492009-03-03 06:41:03 +00001550
John McCall183700f2009-09-21 23:43:11 +00001551 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001552 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1553 else if (IntegerMode) {
1554 if (!OldTy->isIntegralType())
1555 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1556 } else if (ComplexMode) {
1557 if (!OldTy->isComplexType())
1558 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1559 } else {
1560 if (!OldTy->isFloatingType())
1561 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1562 }
1563
Mike Stump390b4cc2009-05-16 07:39:55 +00001564 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1565 // and friends, at least with glibc.
1566 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1567 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001568 // FIXME: Make sure floating-point mappings are accurate
1569 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001570 QualType NewTy;
1571 switch (DestWidth) {
1572 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001573 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001574 return;
1575 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001576 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001577 return;
1578 case 8:
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.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001585 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001586 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587 break;
1588 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001589 if (!IntegerMode) {
1590 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1591 return;
1592 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001593 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001594 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001596 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001597 break;
1598 case 32:
1599 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001600 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001601 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001602 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001603 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001604 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001605 break;
1606 case 64:
1607 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001608 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001609 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001610 if (S.Context.Target.getLongWidth() == 64)
1611 NewTy = S.Context.LongTy;
1612 else
1613 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001614 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001615 if (S.Context.Target.getLongWidth() == 64)
1616 NewTy = S.Context.UnsignedLongTy;
1617 else
1618 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001619 break;
Eli Friedman73397492009-03-03 06:41:03 +00001620 case 96:
1621 NewTy = S.Context.LongDoubleTy;
1622 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001623 case 128:
1624 if (!IntegerMode) {
1625 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1626 return;
1627 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001628 if (OldTy->isSignedIntegerType())
1629 NewTy = S.Context.Int128Ty;
1630 else
1631 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001632 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001633 }
1634
Eli Friedman73397492009-03-03 06:41:03 +00001635 if (ComplexMode) {
1636 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001637 }
1638
1639 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001640 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1641 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001642 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001643 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001644 cast<ValueDecl>(D)->setType(NewTy);
1645}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001646
Mike Stump1feade82009-08-26 22:31:08 +00001647static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001648 // check the attribute arguments.
1649 if (Attr.getNumArgs() > 0) {
1650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1651 return;
1652 }
Anders Carlssone896d982009-02-13 08:11:52 +00001653
Anders Carlsson5bab7882009-02-19 19:16:48 +00001654 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001656 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001657 return;
1658 }
Mike Stumpbf916502009-07-24 19:02:52 +00001659
Mike Stump1feade82009-08-26 22:31:08 +00001660 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001661}
1662
Mike Stump1feade82009-08-26 22:31:08 +00001663static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001664 // check the attribute arguments.
1665 if (Attr.getNumArgs() != 0) {
1666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1667 return;
1668 }
Mike Stumpbf916502009-07-24 19:02:52 +00001669
Chris Lattnerc5197432009-04-14 17:02:11 +00001670 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001671 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001672 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001673 return;
1674 }
Mike Stumpbf916502009-07-24 19:02:52 +00001675
Mike Stump1feade82009-08-26 22:31:08 +00001676 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001677}
1678
Chris Lattnercf2a7212009-04-20 19:12:28 +00001679static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001680 // check the attribute arguments.
1681 if (Attr.getNumArgs() != 0) {
1682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1683 return;
1684 }
Mike Stumpbf916502009-07-24 19:02:52 +00001685
Chris Lattnerc5197432009-04-14 17:02:11 +00001686 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1687 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001689 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001690 return;
1691 }
Mike Stumpbf916502009-07-24 19:02:52 +00001692
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001693 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001694 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001695 return;
1696 }
Mike Stumpbf916502009-07-24 19:02:52 +00001697
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001698 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001699}
1700
Fariborz Jahanianee760332009-03-27 18:38:55 +00001701static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1702 // check the attribute arguments.
1703 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001704 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001705 return;
1706 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001707
Fariborz Jahanianee760332009-03-27 18:38:55 +00001708 if (!isFunctionOrMethod(d)) {
1709 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001710 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001711 return;
1712 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001713
1714 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1715 llvm::APSInt NumParams(32);
1716 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1717 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1718 << "regparm" << NumParamsExpr->getSourceRange();
1719 return;
1720 }
1721
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001722 if (S.Context.Target.getRegParmMax() == 0) {
1723 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001724 << NumParamsExpr->getSourceRange();
1725 return;
1726 }
1727
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001728 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001729 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1730 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001731 return;
1732 }
1733
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001734 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001735}
1736
Sean Huntbbd37c62009-11-21 08:43:09 +00001737static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1738 // check the attribute arguments.
1739 if (Attr.getNumArgs() != 0) {
1740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1741 return;
1742 }
1743
1744 if (!isa<CXXRecordDecl>(d)
1745 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1746 S.Diag(Attr.getLoc(),
1747 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1748 : diag::warn_attribute_wrong_decl_type)
1749 << Attr.getName() << 7 /*virtual method or class*/;
1750 return;
1751 }
Sean Hunt7725e672009-11-25 04:20:27 +00001752
1753 // FIXME: Conform to C++0x redeclaration rules.
1754
1755 if (d->getAttr<FinalAttr>()) {
1756 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1757 return;
1758 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001759
1760 d->addAttr(::new (S.Context) FinalAttr());
1761}
1762
Chris Lattner0744e5f2008-06-29 00:23:49 +00001763//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001764// C++0x member checking attributes
1765//===----------------------------------------------------------------------===//
1766
1767static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1768 if (Attr.getNumArgs() != 0) {
1769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1770 return;
1771 }
1772
1773 if (!isa<CXXRecordDecl>(d)) {
1774 S.Diag(Attr.getLoc(),
1775 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1776 : diag::warn_attribute_wrong_decl_type)
1777 << Attr.getName() << 9 /*class*/;
1778 return;
1779 }
1780
1781 if (d->getAttr<BaseCheckAttr>()) {
1782 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1783 return;
1784 }
1785
1786 d->addAttr(::new (S.Context) BaseCheckAttr());
1787}
1788
1789static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1790 if (Attr.getNumArgs() != 0) {
1791 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1792 return;
1793 }
1794
1795 if (!isa<RecordDecl>(d->getDeclContext())) {
1796 // FIXME: It's not the type that's the problem
1797 S.Diag(Attr.getLoc(),
1798 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1799 : diag::warn_attribute_wrong_decl_type)
1800 << Attr.getName() << 11 /*member*/;
1801 return;
1802 }
1803
1804 // FIXME: Conform to C++0x redeclaration rules.
1805
1806 if (d->getAttr<HidingAttr>()) {
1807 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1808 return;
1809 }
1810
1811 d->addAttr(::new (S.Context) HidingAttr());
1812}
1813
1814static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1815 if (Attr.getNumArgs() != 0) {
1816 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1817 return;
1818 }
1819
1820 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1821 // FIXME: It's not the type that's the problem
1822 S.Diag(Attr.getLoc(),
1823 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1824 : diag::warn_attribute_wrong_decl_type)
1825 << Attr.getName() << 10 /*virtual method*/;
1826 return;
1827 }
1828
1829 // FIXME: Conform to C++0x redeclaration rules.
1830
1831 if (d->getAttr<OverrideAttr>()) {
1832 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1833 return;
1834 }
1835
1836 d->addAttr(::new (S.Context) OverrideAttr());
1837}
1838
1839//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001840// Checker-specific attribute handlers.
1841//===----------------------------------------------------------------------===//
1842
1843static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1844 Sema &S) {
1845
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001846 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001847
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001848 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1849 RetTy = MD->getResultType();
1850 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1851 RetTy = FD->getResultType();
1852 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001853 SourceLocation L = Attr.getLoc();
1854 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1855 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001856 return;
1857 }
Mike Stumpbf916502009-07-24 19:02:52 +00001858
Ted Kremenek6217b802009-07-29 21:53:49 +00001859 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001860 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001861 SourceLocation L = Attr.getLoc();
1862 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1863 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001864 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001865 }
Mike Stumpbf916502009-07-24 19:02:52 +00001866
Ted Kremenekb71368d2009-05-09 02:44:38 +00001867 switch (Attr.getKind()) {
1868 default:
1869 assert(0 && "invalid ownership attribute");
1870 return;
1871 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001872 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001873 return;
1874 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001875 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001876 return;
1877 };
1878}
1879
1880//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001881// Top Level Sema Entry Points
1882//===----------------------------------------------------------------------===//
1883
Sebastian Redla89d82c2008-12-21 19:24:58 +00001884/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001885/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001886/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1887/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001888static void ProcessDeclAttribute(Scope *scope, Decl *D,
1889 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001890 if (Attr.isDeclspecAttribute())
1891 // FIXME: Try to deal with __declspec attributes!
1892 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001893 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001894 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001895 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001896 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001897 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001898 // Ignore these, these are type attributes, handled by
1899 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001900 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001901 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1902 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001903 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001904 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001905 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001906 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001907 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1908 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001909 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001910 HandleDependencyAttr (D, Attr, S); break;
1911 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1912 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1913 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1914 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1915 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1916 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001917 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001918 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001919 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001920 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1921 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1922 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1923 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1924 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1925 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1926 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1927 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1928 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1929 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1930 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1931 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001932
1933 // Checker-specific.
1934 case AttributeList::AT_ns_returns_retained:
1935 case AttributeList::AT_cf_returns_retained:
1936 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1937
Nate Begeman6f3d8382009-06-26 06:32:41 +00001938 case AttributeList::AT_reqd_wg_size:
1939 HandleReqdWorkGroupSize(D, Attr, S); break;
1940
Sean Hunt7725e672009-11-25 04:20:27 +00001941 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1942 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1943 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1944 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1945 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1946 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001947 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001948 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1949 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001950 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1951 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001952 case AttributeList::AT_transparent_union:
1953 HandleTransparentUnionAttr(D, Attr, S);
1954 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001955 case AttributeList::AT_objc_exception:
1956 HandleObjCExceptionAttr(D, Attr, S);
1957 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001958 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001959 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1960 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1961 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1962 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1963 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1964 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1965 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1966 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1967 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001968 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001969 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001970 // Just ignore
1971 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001972 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001973 // Ask target about the attribute.
1974 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1975 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1976 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001977 break;
1978 }
1979}
1980
1981/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1982/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001983void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001984 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001985 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001986 AttrList = AttrList->getNext();
1987 }
1988}
1989
Ryan Flynne25ff832009-07-30 03:15:39 +00001990/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1991/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001992NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001993 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001994 NamedDecl *NewD = 0;
1995 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1996 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1997 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00001998 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001999 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2000 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2001 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002002 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002003 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00002004 }
2005 return NewD;
2006}
2007
2008/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2009/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002010void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002011 if (W.getUsed()) return; // only do this once
2012 W.setUsed(true);
2013 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2014 IdentifierInfo *NDId = ND->getIdentifier();
2015 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
2016 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2017 NewD->addAttr(::new (Context) WeakAttr());
2018 WeakTopLevelDecl.push_back(NewD);
2019 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2020 // to insert Decl at TU scope, sorry.
2021 DeclContext *SavedContext = CurContext;
2022 CurContext = Context.getTranslationUnitDecl();
2023 PushOnScopeChains(NewD, S);
2024 CurContext = SavedContext;
2025 } else { // just add weak to existing
2026 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002027 }
2028}
2029
Chris Lattner0744e5f2008-06-29 00:23:49 +00002030/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2031/// it, apply them to D. This is a bit tricky because PD can have attributes
2032/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002033void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002034 // Handle #pragma weak
2035 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2036 if (ND->hasLinkage()) {
2037 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2038 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002039 // Identifier referenced by #pragma weak before it was declared
2040 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002041 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2042 }
2043 }
2044 }
2045
Chris Lattner0744e5f2008-06-29 00:23:49 +00002046 // Apply decl attributes from the DeclSpec if present.
2047 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002048 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002049
Chris Lattner0744e5f2008-06-29 00:23:49 +00002050 // Walk the declarator structure, applying decl attributes that were in a type
2051 // position to the decl itself. This handles cases like:
2052 // int *__attr__(x)** D;
2053 // when X is a decl attribute.
2054 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2055 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002056 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002057
Chris Lattner0744e5f2008-06-29 00:23:49 +00002058 // Finally, apply any attributes on the decl itself.
2059 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002060 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002061}
John McCall54abf7d2009-11-04 02:18:39 +00002062
2063/// PushParsingDeclaration - Enter a new "scope" of deprecation
2064/// warnings.
2065///
2066/// The state token we use is the start index of this scope
2067/// on the warning stack.
2068Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2069 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002070 return (ParsingDeclStackState) DelayedDiagnostics.size();
2071}
2072
2073void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2074 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2075 ParsingDeclDepth--;
2076
2077 if (DelayedDiagnostics.empty())
2078 return;
2079
2080 unsigned SavedIndex = (unsigned) S;
2081 assert(SavedIndex <= DelayedDiagnostics.size() &&
2082 "saved index is out of bounds");
2083
2084 // We only want to actually emit delayed diagnostics when we
2085 // successfully parsed a decl.
2086 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2087 if (D) {
2088 // We really do want to start with 0 here. We get one push for a
2089 // decl spec and another for each declarator; in a decl group like:
2090 // deprecated_typedef foo, *bar, baz();
2091 // only the declarator pops will be passed decls. This is correct;
2092 // we really do need to consider delayed diagnostics from the decl spec
2093 // for each of the different declarations.
2094 for (unsigned I = 0, E = DelayedDiagnostics.size(); I != E; ++I) {
2095 if (DelayedDiagnostics[I].Triggered)
2096 continue;
2097
2098 switch (DelayedDiagnostics[I].Kind) {
2099 case DelayedDiagnostic::Deprecation:
2100 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2101 break;
2102
2103 case DelayedDiagnostic::Access:
2104 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2105 break;
2106 }
2107 }
2108 }
2109
2110 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002111}
2112
2113static bool isDeclDeprecated(Decl *D) {
2114 do {
2115 if (D->hasAttr<DeprecatedAttr>())
2116 return true;
2117 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2118 return false;
2119}
2120
John McCall2f514482010-01-27 03:50:35 +00002121void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2122 Decl *Ctx) {
2123 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002124 return;
2125
John McCall2f514482010-01-27 03:50:35 +00002126 DD.Triggered = true;
2127 Diag(DD.Loc, diag::warn_deprecated)
2128 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002129}
2130
2131void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2132 // Delay if we're currently parsing a declaration.
2133 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002134 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002135 return;
2136 }
2137
2138 // Otherwise, don't warn if our current context is deprecated.
2139 if (isDeclDeprecated(cast<Decl>(CurContext)))
2140 return;
2141
2142 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2143}