blob: ceab525db128c2f0819b6dd538989ea44a060bb3 [file] [log] [blame]
Chris Lattner2c6fcf52008-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 Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattner58418ff2008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremenek527042b2009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000044
John McCall9dd450b2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000046}
47
Daniel Dunbarc136e0c2008-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 Lopes518e3702009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-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 Dunbar70e3eba2008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000062}
63
Fariborz Jahanian4447e172009-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 Kremenek527042b2009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-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 Jahanian960910a2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000076}
77
Daniel Dunbar70e3eba2008-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 Jahanian4447e172009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000084 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-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 Kremenek527042b2009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099}
100
Ted Kremenek527042b2009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000106
Chris Lattnera4997152009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000108}
109
Ted Kremenek527042b2009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-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 Kremenek527042b2009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
121 return BD->IsVariadic();
122 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000130 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000131
John McCall9dd450b2009-09-21 23:43:11 +0000132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000133 if (!ClsT)
134 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000135
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000136 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Chris Lattner2c6fcf52008-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 Dunbar980c6692008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
Daniel Dunbar980c6692008-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 Lattner58418ff2008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar032db472008-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 Stumpd3bb5572009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000172 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000173 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-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 McCalle66edc12009-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 Gregor758a8692009-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 Lattner2c6fcf52008-06-26 18:38:35 +0000192 }
Douglas Gregor758a8692009-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 McCall703a3f82009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000200
Douglas Gregor758a8692009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000203 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000204}
205
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000212
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlsson68e0b682009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-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 Lattnerb632a6e2008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000222 else
Anders Carlsson68e0b682009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000224 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000226}
227
Ted Kremenek8e3704d2008-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 Lattner4bd8dd82008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000234
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000235 // The IBOutlet attribute only applies to instance variables of Objective-C
236 // classes.
Ted Kremenek2620a062009-02-17 22:20:20 +0000237 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000238 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000239 else
Ted Kremenek2620a062009-02-17 22:20:20 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000241}
242
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000243static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-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 Dunbar70e3eba2008-10-19 02:04:16 +0000246 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000248 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000249 return;
250 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000251
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000252 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000253
254 // The nonnull attribute only applies to pointers.
255 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000256
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000257 for (AttributeList::arg_iterator I=Attr.arg_begin(),
258 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000259
260
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000261 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000262 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000263 llvm::APSInt ArgNum(32);
264 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
266 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000267 return;
268 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000269
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000270 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000271
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000272 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000274 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000275 return;
276 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000277
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000278 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000279
280 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000281 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000282 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000283 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000284 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
285 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000286 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000287 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000288
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000289 NonNullArgs.push_back(x);
290 }
Mike Stumpd3bb5572009-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 Kremenekc4f6d902008-09-01 19:57:52 +0000294 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000295 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
296 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000297 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000298 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000299 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000300
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000301 if (NonNullArgs.empty()) {
302 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
303 return;
304 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000305 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000306
307 unsigned* start = &NonNullArgs[0];
308 unsigned size = NonNullArgs.size();
309 std::sort(start, start + size);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000310 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000311}
312
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000313static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000314 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000315 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000317 return;
318 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000319
Chris Lattner4a927cb2008-06-28 23:36:30 +0000320 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000321 Arg = Arg->IgnoreParenCasts();
322 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000323
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000324 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000326 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000327 return;
328 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000329
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000330 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000331
Benjamin Kramer5f089122009-11-30 17:08:26 +0000332 d->addAttr(::new (S.Context) AliasAttr(Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000333}
334
Mike Stumpd3bb5572009-07-24 19:02:52 +0000335static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000336 Sema &S) {
337 // check the attribute arguments.
338 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000340 return;
341 }
Anders Carlsson88097122009-02-19 19:16:48 +0000342
Chris Lattner4225e232009-04-14 17:02:11 +0000343 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000344 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000345 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000346 return;
347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000348
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000349 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000350}
351
Ryan Flynn1f1fdc02009-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 Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenek08479ae2009-08-15 00:51:46 +0000359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000360 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000361 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
362 d->addAttr(::new (S.Context) MallocAttr());
363 return;
364 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000365 }
366
Ted Kremenek08479ae2009-08-15 00:51:46 +0000367 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000368}
369
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000370static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000371 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000372 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000373 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000375 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000376 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000377
Mike Stump88788fe2009-04-29 19:03:13 +0000378 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
379 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stumpfeb19452009-12-15 03:11:10 +0000380 if (VD == 0 || (!VD->getType()->isBlockPointerType()
381 && !VD->getType()->isFunctionPointerType())) {
Alexis Hunt96d5c762009-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 Kremenek3b204e42009-05-13 21:07:32 +0000385 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000386 return false;
387 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000388 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000389
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000390 return true;
391}
392
393static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000394 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000395 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000396}
397
398static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
399 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000400 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000401 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000402}
403
Alexis Hunt96d5c762009-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 Kremenek39c59a82008-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 Lattner4bd8dd82008-11-19 08:23:25 +0000416 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000417 return;
418 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000419
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000420 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000421 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000422 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000423 return;
424 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000425
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000426 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000427}
428
Daniel Dunbarfee07a02009-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 Stumpd3bb5572009-07-24 19:02:52 +0000435
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000436 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000437 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-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 Kremenek3b204e42009-05-13 21:07:32 +0000443 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000444 return;
445 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000446
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000447 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000448}
449
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
454 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000455 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000456 }
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000464 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000465 return;
466 }
467 priority = Idx.getZExtValue();
468 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000469
Chris Lattner4225e232009-04-14 17:02:11 +0000470 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000472 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000473 return;
474 }
475
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000476 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000482 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
483 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000484 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000485 }
Daniel Dunbar032db472008-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 Lattner3b054132008-11-19 05:08:23 +0000492 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000493 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000494 return;
495 }
496 priority = Idx.getZExtValue();
497 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000498
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000499 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000500 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000501 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000502 return;
503 }
504
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000505 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000506}
507
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000508static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000509 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000510 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000512 return;
513 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000514
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000515 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000516}
517
Fariborz Jahanian1470e932008-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 Stumpd3bb5572009-07-24 19:02:52 +0000524
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000525 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000526}
527
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000528static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000529 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000530 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000531 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000532 return;
533 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000534
Chris Lattner4a927cb2008-06-28 23:36:30 +0000535 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000536 Arg = Arg->IgnoreParenCasts();
537 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000538
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000539 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000541 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000542 return;
543 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000544
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000545 const char *TypeStr = Str->getStrData();
546 unsigned TypeLen = Str->getByteLength();
547 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000548
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000549 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
550 type = VisibilityAttr::DefaultVisibility;
551 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
552 type = VisibilityAttr::HiddenVisibility;
553 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
554 type = VisibilityAttr::HiddenVisibility; // FIXME
555 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
556 type = VisibilityAttr::ProtectedVisibility;
557 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000558 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000559 return;
560 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000561
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000562 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000563}
564
Chris Lattner677a3582009-02-14 08:09:34 +0000565static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
566 Sema &S) {
567 if (Attr.getNumArgs() != 0) {
568 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
569 return;
570 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000571
Chris Lattner677a3582009-02-14 08:09:34 +0000572 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
573 if (OCI == 0) {
574 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
575 return;
576 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000577
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000578 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000579}
580
581static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000582 if (Attr.getNumArgs() != 0) {
583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
584 return;
585 }
Chris Lattner677a3582009-02-14 08:09:34 +0000586 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000587 QualType T = TD->getUnderlyingType();
588 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000589 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000590 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
591 return;
592 }
593 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000594 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000595}
596
Mike Stumpd3bb5572009-07-24 19:02:52 +0000597static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000598HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
599 if (Attr.getNumArgs() != 0) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
601 return;
602 }
603
604 if (!isa<FunctionDecl>(D)) {
605 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
606 return;
607 }
608
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000609 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000610}
611
Steve Naroff3405a732008-09-18 16:44:58 +0000612static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000613 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000614 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000615 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000616 return;
617 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000618
Steve Naroff3405a732008-09-18 16:44:58 +0000619 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000621 return;
622 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000623
Steve Naroff3405a732008-09-18 16:44:58 +0000624 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000625 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000626 type = BlocksAttr::ByRef;
627 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000628 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000629 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000630 return;
631 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000632
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000633 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000634}
635
Anders Carlssonc181b012008-10-05 18:05:59 +0000636static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
637 // check the attribute arguments.
638 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
640 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000641 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000642 }
643
Anders Carlssonc181b012008-10-05 18:05:59 +0000644 int sentinel = 0;
645 if (Attr.getNumArgs() > 0) {
646 Expr *E = static_cast<Expr *>(Attr.getArg(0));
647 llvm::APSInt Idx(32);
648 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000649 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000650 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000651 return;
652 }
653 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000654
Anders Carlssonc181b012008-10-05 18:05:59 +0000655 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
657 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000658 return;
659 }
660 }
661
662 int nullPos = 0;
663 if (Attr.getNumArgs() > 1) {
664 Expr *E = static_cast<Expr *>(Attr.getArg(1));
665 llvm::APSInt Idx(32);
666 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000667 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000668 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000669 return;
670 }
671 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000672
Anders Carlssonc181b012008-10-05 18:05:59 +0000673 if (nullPos > 1 || nullPos < 0) {
674 // FIXME: This error message could be improved, it would be nice
675 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000676 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
677 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000678 return;
679 }
680 }
681
682 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000683 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000684 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000685
Chris Lattner9363e312009-03-17 23:03:47 +0000686 if (isa<FunctionNoProtoType>(FT)) {
687 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
688 return;
689 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000690
Chris Lattner9363e312009-03-17 23:03:47 +0000691 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000692 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000693 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000694 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000695 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
696 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000697 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000698 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000699 }
700 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000701 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
702 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000703 ;
704 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000705 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000706 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000707 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000708 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000709 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000710 int m = Ty->isFunctionPointerType() ? 0 : 1;
711 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000712 return;
713 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000714 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000716 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000717 return;
718 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000719 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000720 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000721 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000722 return;
723 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000724 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000725}
726
Chris Lattner237f2752009-02-14 07:37:35 +0000727static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
728 // check the attribute arguments.
729 if (Attr.getNumArgs() != 0) {
730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
731 return;
732 }
733
Nuno Lopes518e3702009-12-20 23:11:08 +0000734 if (!isFunctionOrMethod(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +0000735 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000736 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000737 return;
738 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000739
Nuno Lopes56abcbd2009-12-22 23:59:52 +0000740 if (getFunctionType(D)->getResultType()->isVoidType()) {
741 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
742 << Attr.getName();
743 return;
744 }
745
Nuno Lopes518e3702009-12-20 23:11:08 +0000746 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000747}
748
749static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000750 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000751 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000753 return;
754 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000755
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000756 /* weak only applies to non-static declarations */
757 bool isStatic = false;
758 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
759 isStatic = VD->getStorageClass() == VarDecl::Static;
760 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
761 isStatic = FD->getStorageClass() == FunctionDecl::Static;
762 }
763 if (isStatic) {
764 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
765 dyn_cast<NamedDecl>(D)->getNameAsString();
766 return;
767 }
768
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000769 // TODO: could also be applied to methods?
770 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
771 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000772 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000773 return;
774 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000775
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000776 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000777}
778
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000779static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
780 // check the attribute arguments.
781 if (Attr.getNumArgs() != 0) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
783 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000784 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000785
786 // weak_import only applies to variable & function declarations.
787 bool isDef = false;
788 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
789 isDef = (!VD->hasExternalStorage() || VD->getInit());
790 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000791 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000792 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
793 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000794 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000795 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000797 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000798 return;
799 }
800
801 // Merge should handle any subsequent violations.
802 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000803 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000804 diag::warn_attribute_weak_import_invalid_on_definition)
805 << "weak_import" << 2 /*variable and function*/;
806 return;
807 }
808
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000809 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000810}
811
Chris Lattner237f2752009-02-14 07:37:35 +0000812static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000813 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000814 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000816 return;
817 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000818
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000819 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000820 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000821 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000822 return;
823 }
824
Chris Lattner237f2752009-02-14 07:37:35 +0000825 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000826 if (!FD) {
827 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000828 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000829 return;
830 }
831
832 // Currently, the dllimport attribute is ignored for inlined functions.
833 // Warning is emitted.
Douglas Gregor35b57532009-10-27 21:01:01 +0000834 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000835 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
836 return;
837 }
838
839 // The attribute is also overridden by a subsequent declaration as dllexport.
840 // Warning is emitted.
841 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
842 nextAttr = nextAttr->getNext()) {
843 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
844 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
845 return;
846 }
847 }
848
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000849 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000850 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
851 return;
852 }
853
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000854 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000855}
856
Chris Lattner237f2752009-02-14 07:37:35 +0000857static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000858 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000859 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000861 return;
862 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000863
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000864 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000865 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000866 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000867 return;
868 }
869
Chris Lattner237f2752009-02-14 07:37:35 +0000870 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000871 if (!FD) {
872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000873 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000874 return;
875 }
876
Mike Stumpd3bb5572009-07-24 19:02:52 +0000877 // Currently, the dllexport attribute is ignored for inlined functions, unless
878 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor35b57532009-10-27 21:01:01 +0000879 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000880 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
881 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
882 return;
883 }
884
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000885 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000886}
887
Nate Begemanf2758702009-06-26 06:32:41 +0000888static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
889 Sema &S) {
890 // Attribute has 3 arguments.
891 if (Attr.getNumArgs() != 3) {
892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
893 return;
894 }
895
896 unsigned WGSize[3];
897 for (unsigned i = 0; i < 3; ++i) {
898 Expr *E = static_cast<Expr *>(Attr.getArg(i));
899 llvm::APSInt ArgNum(32);
900 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
902 << "reqd_work_group_size" << E->getSourceRange();
903 return;
904 }
905 WGSize[i] = (unsigned) ArgNum.getZExtValue();
906 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000907 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000908 WGSize[2]));
909}
910
Chris Lattner237f2752009-02-14 07:37:35 +0000911static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000912 // Attribute has no arguments.
913 if (Attr.getNumArgs() != 1) {
914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
915 return;
916 }
917
918 // Make sure that there is a string literal as the sections's single
919 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +0000920 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
921 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +0000922 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +0000923 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +0000924 return;
925 }
Mike Stump11289f42009-09-09 15:08:12 +0000926
Chris Lattner30ba6742009-08-10 19:03:04 +0000927 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +0000928 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner30ba6742009-08-10 19:03:04 +0000929 if (Error.empty()) {
Benjamin Kramer5f089122009-11-30 17:08:26 +0000930 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Chris Lattner30ba6742009-08-10 19:03:04 +0000931 return;
932 }
Mike Stump11289f42009-09-09 15:08:12 +0000933
Chris Lattner30ba6742009-08-10 19:03:04 +0000934 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
935 << Error;
Mike Stump11289f42009-09-09 15:08:12 +0000936
Daniel Dunbar648bf782009-02-12 17:28:23 +0000937}
938
Eli Friedmane4310c82009-11-09 18:38:53 +0000939static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
940 // Attribute has no arguments.
941 if (Attr.getNumArgs() != 0) {
942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
943 return;
944 }
945
946 // Attribute can be applied only to functions.
947 if (!isa<FunctionDecl>(d)) {
948 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
949 << Attr.getName() << 0 /*function*/;
950 return;
951 }
952
953 // cdecl and fastcall attributes are mutually incompatible.
954 if (d->getAttr<FastCallAttr>()) {
955 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
956 << "cdecl" << "fastcall";
957 return;
958 }
959
960 // cdecl and stdcall attributes are mutually incompatible.
961 if (d->getAttr<StdCallAttr>()) {
962 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
963 << "cdecl" << "stdcall";
964 return;
965 }
966
967 d->addAttr(::new (S.Context) CDeclAttr());
968}
969
970
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000971static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000972 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000973 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000974 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000975 return;
976 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000977
978 // Attribute can be applied only to functions.
979 if (!isa<FunctionDecl>(d)) {
980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000981 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000982 return;
983 }
984
985 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000986 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000987 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
988 << "stdcall" << "fastcall";
989 return;
990 }
991
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000992 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000993}
994
John McCall4976fd42009-11-04 03:36:09 +0000995/// Diagnose the use of a non-standard calling convention on the given
996/// function.
997static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
998 SourceLocation Loc, Sema &S) {
999 if (!D->hasPrototype()) {
1000 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1001 return;
1002 }
1003
1004 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1005 if (T->isVariadic()) {
1006 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1007 return;
1008 }
1009}
1010
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001011static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001012 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001013 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001014 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001015 return;
1016 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001017
1018 if (!isa<FunctionDecl>(d)) {
1019 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001020 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001021 return;
1022 }
1023
John McCall4976fd42009-11-04 03:36:09 +00001024 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1025
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001026 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001027 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001028 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1029 << "fastcall" << "stdcall";
1030 return;
1031 }
1032
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001033 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001034}
1035
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001036static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001037 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001038 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001039 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001040 return;
1041 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001042
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001043 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001044}
1045
Anders Carlssonb8316282008-10-05 23:32:53 +00001046static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1047 // check the attribute arguments.
1048 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001049 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001050 return;
1051 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001052
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001053 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001054}
1055
1056static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1057 // check the attribute arguments.
1058 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001059 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001060 return;
1061 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001062
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001063 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001064}
1065
Anders Carlssond277d792009-01-31 01:16:18 +00001066static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001067 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001068 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1069 return;
1070 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001071
Anders Carlssond277d792009-01-31 01:16:18 +00001072 if (Attr.getNumArgs() != 0) {
1073 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1074 return;
1075 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001076
Anders Carlssond277d792009-01-31 01:16:18 +00001077 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001078
Anders Carlssond277d792009-01-31 01:16:18 +00001079 if (!VD || !VD->hasLocalStorage()) {
1080 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1081 return;
1082 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001083
Anders Carlssond277d792009-01-31 01:16:18 +00001084 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +00001085 NamedDecl *CleanupDecl
1086 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1087 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001088 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001089 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001090 Attr.getParameterName();
1091 return;
1092 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001093
Anders Carlssond277d792009-01-31 01:16:18 +00001094 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1095 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001096 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001097 Attr.getParameterName();
1098 return;
1099 }
1100
Anders Carlssond277d792009-01-31 01:16:18 +00001101 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001102 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001103 Attr.getParameterName();
1104 return;
1105 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001106
Anders Carlsson723f55d2009-02-07 23:16:50 +00001107 // We're currently more strict than GCC about what function types we accept.
1108 // If this ever proves to be a problem it should be easy to fix.
1109 QualType Ty = S.Context.getPointerType(VD->getType());
1110 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001111 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001112 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001113 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1114 Attr.getParameterName() << ParamTy << Ty;
1115 return;
1116 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001117
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001118 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001119}
1120
Mike Stumpd3bb5572009-07-24 19:02:52 +00001121/// Handle __attribute__((format_arg((idx)))) attribute based on
1122/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1123static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001124 if (Attr.getNumArgs() != 1) {
1125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1126 return;
1127 }
1128 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1129 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1130 << Attr.getName() << 0 /*function*/;
1131 return;
1132 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001133 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1134 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001135 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1136 unsigned FirstIdx = 1;
1137 // checks for the 2nd argument
1138 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1139 llvm::APSInt Idx(32);
1140 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1141 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1142 << "format" << 2 << IdxExpr->getSourceRange();
1143 return;
1144 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001145
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001146 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1147 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1148 << "format" << 2 << IdxExpr->getSourceRange();
1149 return;
1150 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001151
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001152 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001153
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001154 // make sure the format string is really a string
1155 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001156
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001157 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1158 if (not_nsstring_type &&
1159 !isCFStringType(Ty, S.Context) &&
1160 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001161 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001162 // FIXME: Should highlight the actual expression that has the wrong type.
1163 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001164 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001165 << IdxExpr->getSourceRange();
1166 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001167 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001168 Ty = getFunctionOrMethodResultType(d);
1169 if (!isNSStringType(Ty, S.Context) &&
1170 !isCFStringType(Ty, S.Context) &&
1171 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001172 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001173 // FIXME: Should highlight the actual expression that has the wrong type.
1174 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001175 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001176 << IdxExpr->getSourceRange();
1177 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001178 }
1179
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001180 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001181}
1182
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001183enum FormatAttrKind {
1184 CFStringFormat,
1185 NSStringFormat,
1186 StrftimeFormat,
1187 SupportedFormat,
1188 InvalidFormat
1189};
1190
1191/// getFormatAttrKind - Map from format attribute names to supported format
1192/// types.
1193static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1194 // Check for formats that get handled specially.
1195 if (Format == "NSString")
1196 return NSStringFormat;
1197 if (Format == "CFString")
1198 return CFStringFormat;
1199 if (Format == "strftime")
1200 return StrftimeFormat;
1201
1202 // Otherwise, check for supported formats.
1203 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1204 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1205 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1206 Format == "zcmn_err")
1207 return SupportedFormat;
1208
1209 return InvalidFormat;
1210}
1211
Mike Stumpd3bb5572009-07-24 19:02:52 +00001212/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1213/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001214static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001215
Chris Lattner4a927cb2008-06-28 23:36:30 +00001216 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001217 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001218 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001219 return;
1220 }
1221
Chris Lattner4a927cb2008-06-28 23:36:30 +00001222 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001223 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001224 return;
1225 }
1226
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001227 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001228 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001229 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001230 return;
1231 }
1232
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001233 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001234 unsigned FirstIdx = 1;
1235
Daniel Dunbar07d07852009-10-18 21:17:35 +00001236 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001237
1238 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001239 if (Format.startswith("__") && Format.endswith("__"))
1240 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001241
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001242 // Check for supported formats.
1243 FormatAttrKind Kind = getFormatAttrKind(Format);
1244 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001245 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001246 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001247 return;
1248 }
1249
1250 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001251 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001252 llvm::APSInt Idx(32);
1253 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001254 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001255 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001256 return;
1257 }
1258
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001259 // FIXME: We should handle the implicit 'this' parameter in a more generic
1260 // way that can be used for other arguments.
1261 bool HasImplicitThisParam = false;
1262 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1263 if (MD->isInstance()) {
1264 HasImplicitThisParam = true;
1265 NumArgs++;
1266 }
1267 }
Mike Stump11289f42009-09-09 15:08:12 +00001268
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001269 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001271 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001272 return;
1273 }
1274
1275 // FIXME: Do we need to bounds check?
1276 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001277
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001278 if (HasImplicitThisParam) {
1279 if (ArgIdx == 0) {
1280 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1281 << "a string type" << IdxExpr->getSourceRange();
1282 return;
1283 }
1284 ArgIdx--;
1285 }
Mike Stump11289f42009-09-09 15:08:12 +00001286
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001287 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001288 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001289
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001290 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001291 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001292 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1293 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001294 return;
1295 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001296 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001297 // FIXME: do we need to check if the type is NSString*? What are the
1298 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001299 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001300 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001301 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1302 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001303 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001304 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001305 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001306 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001307 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001308 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1309 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001310 return;
1311 }
1312
1313 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001314 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001315 llvm::APSInt FirstArg(32);
1316 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001317 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001318 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001319 return;
1320 }
1321
1322 // check if the function is variadic if the 3rd argument non-zero
1323 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001324 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001325 ++NumArgs; // +1 for ...
1326 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001327 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001328 return;
1329 }
1330 }
1331
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001332 // strftime requires FirstArg to be 0 because it doesn't read from any
1333 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001334 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001335 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001336 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1337 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001338 return;
1339 }
1340 // if 0 it disables parameter checking (to use with e.g. va_list)
1341 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001342 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001343 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001344 return;
1345 }
1346
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001347 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1348 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001349}
1350
Chris Lattnera663a0a2008-06-29 00:28:59 +00001351static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1352 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001353 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001354 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001356 return;
1357 }
1358
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001359 // Try to find the underlying union declaration.
1360 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001361 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001362 if (TD && TD->getUnderlyingType()->isUnionType())
1363 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1364 else
1365 RD = dyn_cast<RecordDecl>(d);
1366
1367 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001368 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001369 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001370 return;
1371 }
1372
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001373 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001374 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001375 diag::warn_transparent_union_attribute_not_definition);
1376 return;
1377 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001378
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001379 RecordDecl::field_iterator Field = RD->field_begin(),
1380 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001381 if (Field == FieldEnd) {
1382 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1383 return;
1384 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001385
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001386 FieldDecl *FirstField = *Field;
1387 QualType FirstType = FirstField->getType();
1388 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001389 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001390 diag::warn_transparent_union_attribute_floating);
1391 return;
1392 }
1393
1394 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1395 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1396 for (; Field != FieldEnd; ++Field) {
1397 QualType FieldType = Field->getType();
1398 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1399 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1400 // Warn if we drop the attribute.
1401 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001402 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001403 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001404 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001405 diag::warn_transparent_union_attribute_field_size_align)
1406 << isSize << Field->getDeclName() << FieldBits;
1407 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001408 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001409 diag::note_transparent_union_first_field_size_align)
1410 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001411 return;
1412 }
1413 }
1414
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001415 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001416}
1417
Chris Lattnera663a0a2008-06-29 00:28:59 +00001418static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001419 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001420 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001422 return;
1423 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001424 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1425 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001426
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001427 // Make sure that there is a string literal as the annotation's single
1428 // argument.
1429 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001430 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001431 return;
1432 }
Benjamin Kramer5f089122009-11-30 17:08:26 +00001433 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001434}
1435
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001436static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001437 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001438 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001440 return;
1441 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001442
1443 //FIXME: The C++0x version of this attribute has more limited applicabilty
1444 // than GNU's, and should error out when it is used to specify a
1445 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001446
1447 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001448 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001449 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001450 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001451 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001452 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001453 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001454 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001455
Chris Lattner4627b742008-06-28 23:50:44 +00001456 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1457 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001458 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1460 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001461 return;
1462 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001463 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001464 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001465 << alignmentExpr->getSourceRange();
1466 return;
1467 }
1468
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001469 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001470}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001471
Mike Stumpd3bb5572009-07-24 19:02:52 +00001472/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1473/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001474///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001475/// Despite what would be logical, the mode attribute is a decl attribute, not a
1476/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1477/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001478static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001479 // This attribute isn't documented, but glibc uses it. It changes
1480 // the width of an int or unsigned int to the specified size.
1481
1482 // Check that there aren't any arguments
1483 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001484 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001485 return;
1486 }
1487
1488 IdentifierInfo *Name = Attr.getParameterName();
1489 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001491 return;
1492 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001493
Daniel Dunbar07d07852009-10-18 21:17:35 +00001494 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001495
1496 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001497 if (Str.startswith("__") && Str.endswith("__"))
1498 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001499
1500 unsigned DestWidth = 0;
1501 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001502 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001503 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001504 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001505 switch (Str[0]) {
1506 case 'Q': DestWidth = 8; break;
1507 case 'H': DestWidth = 16; break;
1508 case 'S': DestWidth = 32; break;
1509 case 'D': DestWidth = 64; break;
1510 case 'X': DestWidth = 96; break;
1511 case 'T': DestWidth = 128; break;
1512 }
1513 if (Str[1] == 'F') {
1514 IntegerMode = false;
1515 } else if (Str[1] == 'C') {
1516 IntegerMode = false;
1517 ComplexMode = true;
1518 } else if (Str[1] != 'I') {
1519 DestWidth = 0;
1520 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001521 break;
1522 case 4:
1523 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1524 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001525 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001526 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001527 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001528 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001529 break;
1530 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001531 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001532 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001533 break;
1534 }
1535
1536 QualType OldTy;
1537 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1538 OldTy = TD->getUnderlyingType();
1539 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1540 OldTy = VD->getType();
1541 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001542 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1543 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001544 return;
1545 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001546
John McCall9dd450b2009-09-21 23:43:11 +00001547 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001548 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1549 else if (IntegerMode) {
1550 if (!OldTy->isIntegralType())
1551 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1552 } else if (ComplexMode) {
1553 if (!OldTy->isComplexType())
1554 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1555 } else {
1556 if (!OldTy->isFloatingType())
1557 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1558 }
1559
Mike Stump87c57ac2009-05-16 07:39:55 +00001560 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1561 // and friends, at least with glibc.
1562 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1563 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001564 // FIXME: Make sure floating-point mappings are accurate
1565 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001566 QualType NewTy;
1567 switch (DestWidth) {
1568 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001569 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001570 return;
1571 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001572 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001573 return;
1574 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001575 if (!IntegerMode) {
1576 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1577 return;
1578 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001579 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001580 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001581 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001582 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001583 break;
1584 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001585 if (!IntegerMode) {
1586 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1587 return;
1588 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001589 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001590 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001591 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001592 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001593 break;
1594 case 32:
1595 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001596 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001597 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001598 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001599 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001600 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001601 break;
1602 case 64:
1603 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001604 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001605 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001606 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001607 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001608 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001609 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001610 case 96:
1611 NewTy = S.Context.LongDoubleTy;
1612 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001613 case 128:
1614 if (!IntegerMode) {
1615 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1616 return;
1617 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00001618 if (OldTy->isSignedIntegerType())
1619 NewTy = S.Context.Int128Ty;
1620 else
1621 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001622 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001623 }
1624
Eli Friedman4735374e2009-03-03 06:41:03 +00001625 if (ComplexMode) {
1626 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001627 }
1628
1629 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001630 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1631 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001632 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001633 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001634 cast<ValueDecl>(D)->setType(NewTy);
1635}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001636
Mike Stump3722f582009-08-26 22:31:08 +00001637static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001638 // check the attribute arguments.
1639 if (Attr.getNumArgs() > 0) {
1640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1641 return;
1642 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001643
Anders Carlsson88097122009-02-19 19:16:48 +00001644 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001645 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001646 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001647 return;
1648 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001649
Mike Stump3722f582009-08-26 22:31:08 +00001650 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001651}
1652
Mike Stump3722f582009-08-26 22:31:08 +00001653static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001654 // check the attribute arguments.
1655 if (Attr.getNumArgs() != 0) {
1656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1657 return;
1658 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001659
Chris Lattner4225e232009-04-14 17:02:11 +00001660 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001661 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001662 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001663 return;
1664 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001665
Mike Stump3722f582009-08-26 22:31:08 +00001666 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001667}
1668
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001669static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001670 // check the attribute arguments.
1671 if (Attr.getNumArgs() != 0) {
1672 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1673 return;
1674 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001675
Chris Lattner4225e232009-04-14 17:02:11 +00001676 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1677 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001679 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001680 return;
1681 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001682
Douglas Gregor35b57532009-10-27 21:01:01 +00001683 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001684 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001685 return;
1686 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001687
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001688 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001689}
1690
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001691static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1692 // check the attribute arguments.
1693 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001694 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001695 return;
1696 }
Eli Friedman7044b762009-03-27 21:06:47 +00001697
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001698 if (!isFunctionOrMethod(d)) {
1699 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001700 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001701 return;
1702 }
Eli Friedman7044b762009-03-27 21:06:47 +00001703
1704 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1705 llvm::APSInt NumParams(32);
1706 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1707 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1708 << "regparm" << NumParamsExpr->getSourceRange();
1709 return;
1710 }
1711
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001712 if (S.Context.Target.getRegParmMax() == 0) {
1713 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001714 << NumParamsExpr->getSourceRange();
1715 return;
1716 }
1717
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001718 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001719 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1720 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001721 return;
1722 }
1723
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001724 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001725}
1726
Alexis Hunt96d5c762009-11-21 08:43:09 +00001727static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1728 // check the attribute arguments.
1729 if (Attr.getNumArgs() != 0) {
1730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1731 return;
1732 }
1733
1734 if (!isa<CXXRecordDecl>(d)
1735 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1736 S.Diag(Attr.getLoc(),
1737 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1738 : diag::warn_attribute_wrong_decl_type)
1739 << Attr.getName() << 7 /*virtual method or class*/;
1740 return;
1741 }
Alexis Hunt54a02542009-11-25 04:20:27 +00001742
1743 // FIXME: Conform to C++0x redeclaration rules.
1744
1745 if (d->getAttr<FinalAttr>()) {
1746 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1747 return;
1748 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001749
1750 d->addAttr(::new (S.Context) FinalAttr());
1751}
1752
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001753//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00001754// C++0x member checking attributes
1755//===----------------------------------------------------------------------===//
1756
1757static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1758 if (Attr.getNumArgs() != 0) {
1759 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1760 return;
1761 }
1762
1763 if (!isa<CXXRecordDecl>(d)) {
1764 S.Diag(Attr.getLoc(),
1765 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1766 : diag::warn_attribute_wrong_decl_type)
1767 << Attr.getName() << 9 /*class*/;
1768 return;
1769 }
1770
1771 if (d->getAttr<BaseCheckAttr>()) {
1772 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1773 return;
1774 }
1775
1776 d->addAttr(::new (S.Context) BaseCheckAttr());
1777}
1778
1779static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1780 if (Attr.getNumArgs() != 0) {
1781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1782 return;
1783 }
1784
1785 if (!isa<RecordDecl>(d->getDeclContext())) {
1786 // FIXME: It's not the type that's the problem
1787 S.Diag(Attr.getLoc(),
1788 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1789 : diag::warn_attribute_wrong_decl_type)
1790 << Attr.getName() << 11 /*member*/;
1791 return;
1792 }
1793
1794 // FIXME: Conform to C++0x redeclaration rules.
1795
1796 if (d->getAttr<HidingAttr>()) {
1797 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1798 return;
1799 }
1800
1801 d->addAttr(::new (S.Context) HidingAttr());
1802}
1803
1804static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1805 if (Attr.getNumArgs() != 0) {
1806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1807 return;
1808 }
1809
1810 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1811 // FIXME: It's not the type that's the problem
1812 S.Diag(Attr.getLoc(),
1813 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1814 : diag::warn_attribute_wrong_decl_type)
1815 << Attr.getName() << 10 /*virtual method*/;
1816 return;
1817 }
1818
1819 // FIXME: Conform to C++0x redeclaration rules.
1820
1821 if (d->getAttr<OverrideAttr>()) {
1822 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1823 return;
1824 }
1825
1826 d->addAttr(::new (S.Context) OverrideAttr());
1827}
1828
1829//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001830// Checker-specific attribute handlers.
1831//===----------------------------------------------------------------------===//
1832
1833static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1834 Sema &S) {
1835
Ted Kremenek3b204e42009-05-13 21:07:32 +00001836 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001837
Ted Kremenek3b204e42009-05-13 21:07:32 +00001838 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1839 RetTy = MD->getResultType();
1840 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1841 RetTy = FD->getResultType();
1842 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001843 SourceLocation L = Attr.getLoc();
1844 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1845 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001846 return;
1847 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001848
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001849 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001850 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001851 SourceLocation L = Attr.getLoc();
1852 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1853 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001854 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001855 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001856
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001857 switch (Attr.getKind()) {
1858 default:
1859 assert(0 && "invalid ownership attribute");
1860 return;
1861 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001862 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001863 return;
1864 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001865 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001866 return;
1867 };
1868}
1869
1870//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001871// Top Level Sema Entry Points
1872//===----------------------------------------------------------------------===//
1873
Sebastian Redlfc24b632008-12-21 19:24:58 +00001874/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001875/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001876/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1877/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001878static void ProcessDeclAttribute(Scope *scope, Decl *D,
1879 const AttributeList &Attr, Sema &S) {
Eli Friedman53339e02009-06-08 23:27:34 +00001880 if (Attr.isDeclspecAttribute())
1881 // FIXME: Try to deal with __declspec attributes!
1882 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001883 switch (Attr.getKind()) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001884 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001885 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001886 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00001887 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001888 // Ignore these, these are type attributes, handled by
1889 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001890 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001891 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1892 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001893 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001894 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001895 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001896 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001897 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1898 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001899 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001900 HandleDependencyAttr (D, Attr, S); break;
1901 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1902 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1903 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1904 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1905 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1906 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001907 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001908 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001909 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001910 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1911 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1912 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1913 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1914 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1915 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1916 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1917 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1918 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1919 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1920 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1921 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001922
1923 // Checker-specific.
1924 case AttributeList::AT_ns_returns_retained:
1925 case AttributeList::AT_cf_returns_retained:
1926 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1927
Nate Begemanf2758702009-06-26 06:32:41 +00001928 case AttributeList::AT_reqd_wg_size:
1929 HandleReqdWorkGroupSize(D, Attr, S); break;
1930
Alexis Hunt54a02542009-11-25 04:20:27 +00001931 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1932 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1933 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1934 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1935 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1936 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001937 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00001938 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1939 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001940 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1941 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001942 case AttributeList::AT_transparent_union:
1943 HandleTransparentUnionAttr(D, Attr, S);
1944 break;
Chris Lattner677a3582009-02-14 08:09:34 +00001945 case AttributeList::AT_objc_exception:
1946 HandleObjCExceptionAttr(D, Attr, S);
1947 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001948 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001949 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1950 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1951 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1952 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1953 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1954 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1955 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1956 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1957 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001958 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00001959 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00001960 // Just ignore
1961 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001962 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001963 // Ask target about the attribute.
1964 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1965 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1966 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001967 break;
1968 }
1969}
1970
1971/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1972/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00001973void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001974 while (AttrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +00001975 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001976 AttrList = AttrList->getNext();
1977 }
1978}
1979
Ryan Flynn7d470f32009-07-30 03:15:39 +00001980/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1981/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00001982NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001983 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00001984 NamedDecl *NewD = 0;
1985 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1986 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1987 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00001988 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001989 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1990 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1991 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00001992 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001993 VD->getStorageClass());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001994 }
1995 return NewD;
1996}
1997
1998/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1999/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002000void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002001 if (W.getUsed()) return; // only do this once
2002 W.setUsed(true);
2003 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2004 IdentifierInfo *NDId = ND->getIdentifier();
2005 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
2006 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2007 NewD->addAttr(::new (Context) WeakAttr());
2008 WeakTopLevelDecl.push_back(NewD);
2009 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2010 // to insert Decl at TU scope, sorry.
2011 DeclContext *SavedContext = CurContext;
2012 CurContext = Context.getTranslationUnitDecl();
2013 PushOnScopeChains(NewD, S);
2014 CurContext = SavedContext;
2015 } else { // just add weak to existing
2016 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002017 }
2018}
2019
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002020/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2021/// it, apply them to D. This is a bit tricky because PD can have attributes
2022/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002023void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002024 // Handle #pragma weak
2025 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2026 if (ND->hasLinkage()) {
2027 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2028 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002029 // Identifier referenced by #pragma weak before it was declared
2030 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002031 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2032 }
2033 }
2034 }
2035
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002036 // Apply decl attributes from the DeclSpec if present.
2037 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002038 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002039
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002040 // Walk the declarator structure, applying decl attributes that were in a type
2041 // position to the decl itself. This handles cases like:
2042 // int *__attr__(x)** D;
2043 // when X is a decl attribute.
2044 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2045 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002046 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002047
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002048 // Finally, apply any attributes on the decl itself.
2049 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002050 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002051}
John McCall28a6aea2009-11-04 02:18:39 +00002052
2053/// PushParsingDeclaration - Enter a new "scope" of deprecation
2054/// warnings.
2055///
2056/// The state token we use is the start index of this scope
2057/// on the warning stack.
2058Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2059 ParsingDeclDepth++;
2060 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2061}
2062
2063static bool isDeclDeprecated(Decl *D) {
2064 do {
2065 if (D->hasAttr<DeprecatedAttr>())
2066 return true;
2067 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2068 return false;
2069}
2070
2071void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2072 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2073 ParsingDeclDepth--;
2074
2075 if (DelayedDeprecationWarnings.empty())
2076 return;
2077
2078 unsigned SavedIndex = (unsigned) S;
2079 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2080 "saved index is out of bounds");
2081
2082 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2083 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2084 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2085 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2086 if (ND) {
2087 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2088
2089 // Prevent this from triggering multiple times.
2090 ND = 0;
2091 }
2092 }
2093 }
2094
2095 DelayedDeprecationWarnings.set_size(SavedIndex);
2096}
2097
2098void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2099 // Delay if we're currently parsing a declaration.
2100 if (ParsingDeclDepth) {
2101 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2102 return;
2103 }
2104
2105 // Otherwise, don't warn if our current context is deprecated.
2106 if (isDeclDeprecated(cast<Decl>(CurContext)))
2107 return;
2108
2109 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2110}