blob: f40f125683b7de9f167da7f01d1adc2a12b9cc9b [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"
15#include "clang/AST/ASTContext.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattner58418ff2008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Ted Kremenek527042b2009-08-14 20:49:40 +000027static const FunctionType *getFunctionType(const Decl *d,
28 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000029 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000030 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000032 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000034 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035 Ty = decl->getUnderlyingType();
36 else
37 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000038
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000040 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000041 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000042 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000043
John McCall9dd450b2009-09-21 23:43:11 +000044 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000045}
46
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000047// FIXME: We should provide an abstraction around a method or function
48// to provide the following bits of information.
49
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000050/// isFunctionOrMethod - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000051/// type (function or function-typed variable).
52static bool isFunction(const Decl *d) {
53 return getFunctionType(d, false) != NULL;
54}
55
56/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000057/// type (function or function-typed variable) or an Objective-C
58/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000059static bool isFunctionOrMethod(const Decl *d) {
60 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000061}
62
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063/// isFunctionOrMethodOrBlock - Return true if the given decl has function
64/// type (function or function-typed variable) or an Objective-C
65/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000066static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067 if (isFunctionOrMethod(d))
68 return true;
69 // check for block is more involved.
70 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
71 QualType Ty = V->getType();
72 return Ty->isBlockPointerType();
73 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000074 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000075}
76
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000077/// hasFunctionProto - Return true if the given decl has a argument
78/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000079/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000080static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000081 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000082 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000083 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000084 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000085 return true;
86 }
87}
88
89/// getFunctionOrMethodNumArgs - Return number of function or method
90/// arguments. It is an error to call this on a K&R function (use
91/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000092static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000093 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000094 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
96 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +000097 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000098}
99
Ted Kremenek527042b2009-08-14 20:49:40 +0000100static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000101 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000102 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000103 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
104 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000105
Chris Lattnera4997152009-02-20 18:43:26 +0000106 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000107}
108
Ted Kremenek527042b2009-08-14 20:49:40 +0000109static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000110 if (const FunctionType *FnTy = getFunctionType(d))
111 return cast<FunctionProtoType>(FnTy)->getResultType();
112 return cast<ObjCMethodDecl>(d)->getResultType();
113}
114
Ted Kremenek527042b2009-08-14 20:49:40 +0000115static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000116 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000118 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000119 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
120 return BD->IsVariadic();
121 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000122 return cast<ObjCMethodDecl>(d)->isVariadic();
123 }
124}
125
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000126static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000127 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000128 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000129 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000130
John McCall9dd450b2009-09-21 23:43:11 +0000131 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000132 if (!ClsT)
133 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000134
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000135 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000136
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000137 // FIXME: Should we walk the chain of classes?
138 return ClsName == &Ctx.Idents.get("NSString") ||
139 ClsName == &Ctx.Idents.get("NSMutableString");
140}
141
Daniel Dunbar980c6692008-09-26 03:32:58 +0000142static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000143 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000144 if (!PT)
145 return false;
146
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000147 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000148 if (!RT)
149 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000150
Daniel Dunbar980c6692008-09-26 03:32:58 +0000151 const RecordDecl *RD = RT->getDecl();
152 if (RD->getTagKind() != TagDecl::TK_struct)
153 return false;
154
155 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
156}
157
Chris Lattner58418ff2008-06-29 00:16:31 +0000158//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000159// Attribute Implementations
160//===----------------------------------------------------------------------===//
161
Daniel Dunbar032db472008-07-31 22:40:48 +0000162// FIXME: All this manual attribute parsing code is gross. At the
163// least add some helper functions to check most argument patterns (#
164// and types of args).
165
Mike Stumpd3bb5572009-07-24 19:02:52 +0000166static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000167 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000168 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
169 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000171 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000174 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000175
176 Expr *sizeExpr;
177
178 // Special case where the argument is a template id.
179 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000180 CXXScopeSpec SS;
181 UnqualifiedId id;
182 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
183 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000184 } else {
185 // check the attribute arguments.
186 if (Attr.getNumArgs() != 1) {
187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
188 return;
189 }
190 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000191 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000192
193 // Instantiate/Install the vector type, and let Sema build the type for us.
194 // This will run the reguired checks.
195 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
196 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000197 // FIXME: preserve the old source info.
198 tDecl->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000199
Douglas Gregor758a8692009-06-17 21:51:59 +0000200 // Remember this typedef decl, we will need it later for diagnostics.
201 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000202 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000203}
204
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000205
Mike Stumpd3bb5572009-07-24 19:02:52 +0000206/// HandleVectorSizeAttribute - this attribute is only applicable to integral
207/// and float scalars, although arrays, pointers, and function return values are
208/// allowed in conjunction with this construct. Aggregates with this attribute
209/// are invalid, even if they are of the same size as a corresponding scalar.
210/// The raw attribute should contain precisely 1 argument, the vector size for
211/// the variable, measured in bytes. If curType and rawAttr are well formed,
212/// this routine will return a new vector type.
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000213static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000214 QualType CurType;
215 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
216 CurType = VD->getType();
217 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
218 CurType = TD->getUnderlyingType();
219 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000220 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
221 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000222 return;
223 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000224
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000225 // Check the attribute arugments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000226 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000228 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000229 }
Chris Lattner4a927cb2008-06-28 23:36:30 +0000230 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000231 llvm::APSInt vecSize(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000232 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
234 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000235 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000236 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000237 // navigate to the base type - we need to provide for vector pointers, vector
238 // arrays, and functions returning vectors.
Chris Lattner574dee62008-07-26 22:17:49 +0000239 if (CurType->isPointerType() || CurType->isArrayType() ||
240 CurType->isFunctionType()) {
Chris Lattner851eb922009-05-13 04:00:12 +0000241 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
242 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000243 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
244 do {
245 if (PointerType *PT = dyn_cast<PointerType>(canonType))
246 canonType = PT->getPointeeType().getTypePtr();
247 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
248 canonType = AT->getElementType().getTypePtr();
249 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
250 canonType = FT->getResultType().getTypePtr();
251 } while (canonType->isPointerType() || canonType->isArrayType() ||
252 canonType->isFunctionType());
253 */
254 }
Chris Lattnerb70f3de2009-05-13 05:13:44 +0000255 // the base type must be integer or float, and can't already be a vector.
256 if (CurType->isVectorType() ||
257 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattner1e5665e2008-11-24 06:25:27 +0000258 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000259 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000260 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000261 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000262 // vecSize is specified in bytes - convert to bits.
Mike Stumpd3bb5572009-07-24 19:02:52 +0000263 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
264
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000265 // the vector size needs to be an integral multiple of the type size.
266 if (vectorSize % typeSize) {
Chris Lattner3b054132008-11-19 05:08:23 +0000267 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
268 << sizeExpr->getSourceRange();
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000269 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000270 }
271 if (vectorSize == 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000272 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
273 << sizeExpr->getSourceRange();
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000274 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000275 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000276
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000277 // Success! Instantiate the vector type, the number of elements is > 0, and
278 // not required to be a power of 2, unlike GCC.
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000279 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000280
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000281 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
282 VD->setType(CurType);
John McCall703a3f82009-10-24 08:00:42 +0000283 else {
284 // FIXME: preserve existing source info.
285 DeclaratorInfo *DInfo = S.Context.getTrivialDeclaratorInfo(CurType);
286 cast<TypedefDecl>(D)->setTypeDeclaratorInfo(DInfo);
287 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000288}
289
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000290static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000291 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000292 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000293 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000294 return;
295 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000296
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000297 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlsson68e0b682009-08-08 18:23:56 +0000298 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000299 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
300 // If the alignment is less than or equal to 8 bits, the packed attribute
301 // has no effect.
302 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000303 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000304 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000305 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000306 else
Anders Carlsson68e0b682009-08-08 18:23:56 +0000307 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000308 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000309 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000310}
311
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000312static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
313 // check the attribute arguments.
314 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000316 return;
317 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000318
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000319 // The IBOutlet attribute only applies to instance variables of Objective-C
320 // classes.
Ted Kremenek2620a062009-02-17 22:20:20 +0000321 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000322 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000323 else
Ted Kremenek2620a062009-02-17 22:20:20 +0000324 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000325}
326
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000327static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000328 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
329 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000330 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000331 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000332 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000333 return;
334 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000335
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000336 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000337
338 // The nonnull attribute only applies to pointers.
339 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000340
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000341 for (AttributeList::arg_iterator I=Attr.arg_begin(),
342 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000343
344
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000345 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000346 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000347 llvm::APSInt ArgNum(32);
348 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000349 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
350 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000351 return;
352 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000353
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000354 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000355
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000356 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000358 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000359 return;
360 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000361
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000362 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000363
364 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000365 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000366 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000367 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000368 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
369 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000370 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000371 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000372
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000373 NonNullArgs.push_back(x);
374 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000375
376 // If no arguments were specified to __attribute__((nonnull)) then all pointer
377 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000378 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000379 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
380 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000381 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000382 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000383 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000384
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000385 if (NonNullArgs.empty()) {
386 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
387 return;
388 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000389 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000390
391 unsigned* start = &NonNullArgs[0];
392 unsigned size = NonNullArgs.size();
393 std::sort(start, start + size);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000394 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000395}
396
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000397static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000398 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000399 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000401 return;
402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000403
Chris Lattner4a927cb2008-06-28 23:36:30 +0000404 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000405 Arg = Arg->IgnoreParenCasts();
406 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000407
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000408 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000409 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000410 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000411 return;
412 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000413
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000414 const char *Alias = Str->getStrData();
415 unsigned AliasLen = Str->getByteLength();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000416
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000417 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000418
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000419 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000420}
421
Mike Stumpd3bb5572009-07-24 19:02:52 +0000422static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000423 Sema &S) {
424 // check the attribute arguments.
425 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000427 return;
428 }
Anders Carlsson88097122009-02-19 19:16:48 +0000429
Chris Lattner4225e232009-04-14 17:02:11 +0000430 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000431 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000432 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000433 return;
434 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000435
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000436 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000437}
438
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000439static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
440 // check the attribute arguments.
441 if (Attr.getNumArgs() != 0) {
442 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
443 return;
444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenek08479ae2009-08-15 00:51:46 +0000446 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000447 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000448 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
449 d->addAttr(::new (S.Context) MallocAttr());
450 return;
451 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000452 }
453
Ted Kremenek08479ae2009-08-15 00:51:46 +0000454 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000455}
456
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000457static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000458 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000459 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000460 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000462 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000463 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000464
Mike Stump88788fe2009-04-29 19:03:13 +0000465 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
466 ValueDecl *VD = dyn_cast<ValueDecl>(d);
467 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000468 S.Diag(Attr.getLoc(),
469 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
470 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000471 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000472 return false;
473 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000474 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000475
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000476 return true;
477}
478
479static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000480 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000481 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000482}
483
484static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
485 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000486 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000487 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000488}
489
Alexis Hunt96d5c762009-11-21 08:43:09 +0000490static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
491 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
493 << Attr.getName() << 8; /*function, method, or parameter*/
494 return;
495 }
496 // FIXME: Actually store the attribute on the declaration
497}
498
Ted Kremenek39c59a82008-07-25 04:39:19 +0000499static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
500 // check the attribute arguments.
501 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000503 return;
504 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000505
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000506 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000507 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000508 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000509 return;
510 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000511
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000512 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000513}
514
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000515static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
516 // check the attribute arguments.
517 if (Attr.getNumArgs() != 0) {
518 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
519 return;
520 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000521
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000522 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000523 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000524 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
525 return;
526 }
527 } else if (!isFunctionOrMethod(d)) {
528 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000529 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000530 return;
531 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000532
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000533 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000534}
535
Daniel Dunbar032db472008-07-31 22:40:48 +0000536static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
537 // check the attribute arguments.
538 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000539 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
540 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000541 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000542 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000543
544 int priority = 65535; // FIXME: Do not hardcode such constants.
545 if (Attr.getNumArgs() > 0) {
546 Expr *E = static_cast<Expr *>(Attr.getArg(0));
547 llvm::APSInt Idx(32);
548 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000549 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000550 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000551 return;
552 }
553 priority = Idx.getZExtValue();
554 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000555
Chris Lattner4225e232009-04-14 17:02:11 +0000556 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000557 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000558 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000559 return;
560 }
561
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000562 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000563}
564
565static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
566 // check the attribute arguments.
567 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000568 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
569 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000570 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000571 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000572
573 int priority = 65535; // FIXME: Do not hardcode such constants.
574 if (Attr.getNumArgs() > 0) {
575 Expr *E = static_cast<Expr *>(Attr.getArg(0));
576 llvm::APSInt Idx(32);
577 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000578 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000579 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000580 return;
581 }
582 priority = Idx.getZExtValue();
583 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000584
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000585 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000586 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000587 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000588 return;
589 }
590
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000591 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000592}
593
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000594static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000595 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000596 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000598 return;
599 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000600
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000601 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000602}
603
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000604static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
605 // check the attribute arguments.
606 if (Attr.getNumArgs() != 0) {
607 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
608 return;
609 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000610
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000611 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000612}
613
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000614static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000615 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000616 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000617 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000618 return;
619 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000620
Chris Lattner4a927cb2008-06-28 23:36:30 +0000621 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000622 Arg = Arg->IgnoreParenCasts();
623 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000624
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000625 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000626 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000627 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000628 return;
629 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000630
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000631 const char *TypeStr = Str->getStrData();
632 unsigned TypeLen = Str->getByteLength();
633 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000634
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000635 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
636 type = VisibilityAttr::DefaultVisibility;
637 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
638 type = VisibilityAttr::HiddenVisibility;
639 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
640 type = VisibilityAttr::HiddenVisibility; // FIXME
641 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
642 type = VisibilityAttr::ProtectedVisibility;
643 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000644 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000645 return;
646 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000647
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000648 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000649}
650
Chris Lattner677a3582009-02-14 08:09:34 +0000651static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
652 Sema &S) {
653 if (Attr.getNumArgs() != 0) {
654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
655 return;
656 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000657
Chris Lattner677a3582009-02-14 08:09:34 +0000658 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
659 if (OCI == 0) {
660 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
661 return;
662 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000663
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000664 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000665}
666
667static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000668 if (Attr.getNumArgs() != 0) {
669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
670 return;
671 }
Chris Lattner677a3582009-02-14 08:09:34 +0000672 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000673 QualType T = TD->getUnderlyingType();
674 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000675 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000676 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
677 return;
678 }
679 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000680 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000681}
682
Mike Stumpd3bb5572009-07-24 19:02:52 +0000683static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000684HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
685 if (Attr.getNumArgs() != 0) {
686 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
687 return;
688 }
689
690 if (!isa<FunctionDecl>(D)) {
691 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
692 return;
693 }
694
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000695 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000696}
697
Steve Naroff3405a732008-09-18 16:44:58 +0000698static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000699 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000700 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000701 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000702 return;
703 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000704
Steve Naroff3405a732008-09-18 16:44:58 +0000705 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000707 return;
708 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000709
Steve Naroff3405a732008-09-18 16:44:58 +0000710 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000711 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000712 type = BlocksAttr::ByRef;
713 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000714 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000715 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000716 return;
717 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000718
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000719 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000720}
721
Anders Carlssonc181b012008-10-05 18:05:59 +0000722static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
723 // check the attribute arguments.
724 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000725 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
726 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000727 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000728 }
729
Anders Carlssonc181b012008-10-05 18:05:59 +0000730 int sentinel = 0;
731 if (Attr.getNumArgs() > 0) {
732 Expr *E = static_cast<Expr *>(Attr.getArg(0));
733 llvm::APSInt Idx(32);
734 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000736 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000737 return;
738 }
739 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000740
Anders Carlssonc181b012008-10-05 18:05:59 +0000741 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000742 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
743 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000744 return;
745 }
746 }
747
748 int nullPos = 0;
749 if (Attr.getNumArgs() > 1) {
750 Expr *E = static_cast<Expr *>(Attr.getArg(1));
751 llvm::APSInt Idx(32);
752 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000753 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000754 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000755 return;
756 }
757 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000758
Anders Carlssonc181b012008-10-05 18:05:59 +0000759 if (nullPos > 1 || nullPos < 0) {
760 // FIXME: This error message could be improved, it would be nice
761 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000762 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
763 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000764 return;
765 }
766 }
767
768 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000769 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000770 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000771
Chris Lattner9363e312009-03-17 23:03:47 +0000772 if (isa<FunctionNoProtoType>(FT)) {
773 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
774 return;
775 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000776
Chris Lattner9363e312009-03-17 23:03:47 +0000777 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000778 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000779 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000780 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000781 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
782 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000783 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000784 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000785 }
786 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000787 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
788 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000789 ;
790 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000791 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000792 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000793 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000794 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000795 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000796 int m = Ty->isFunctionPointerType() ? 0 : 1;
797 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000798 return;
799 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000800 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000802 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000803 return;
804 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000805 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000806 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000807 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000808 return;
809 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000810 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000811}
812
Chris Lattner237f2752009-02-14 07:37:35 +0000813static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
814 // check the attribute arguments.
815 if (Attr.getNumArgs() != 0) {
816 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
817 return;
818 }
819
820 // TODO: could also be applied to methods?
821 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
822 if (!Fn) {
823 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000824 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000825 return;
826 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000827
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000828 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000829}
830
831static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000832 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000833 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000835 return;
836 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000837
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000838 /* weak only applies to non-static declarations */
839 bool isStatic = false;
840 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
841 isStatic = VD->getStorageClass() == VarDecl::Static;
842 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
843 isStatic = FD->getStorageClass() == FunctionDecl::Static;
844 }
845 if (isStatic) {
846 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
847 dyn_cast<NamedDecl>(D)->getNameAsString();
848 return;
849 }
850
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000851 // TODO: could also be applied to methods?
852 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000854 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000855 return;
856 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000857
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000858 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000859}
860
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000861static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
862 // check the attribute arguments.
863 if (Attr.getNumArgs() != 0) {
864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
865 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000866 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000867
868 // weak_import only applies to variable & function declarations.
869 bool isDef = false;
870 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
871 isDef = (!VD->hasExternalStorage() || VD->getInit());
872 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000873 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000874 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
875 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000876 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000877 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000878 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000879 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000880 return;
881 }
882
883 // Merge should handle any subsequent violations.
884 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000885 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000886 diag::warn_attribute_weak_import_invalid_on_definition)
887 << "weak_import" << 2 /*variable and function*/;
888 return;
889 }
890
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000891 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000892}
893
Chris Lattner237f2752009-02-14 07:37:35 +0000894static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000895 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000896 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000897 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000898 return;
899 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000900
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000901 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000902 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000903 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000904 return;
905 }
906
Chris Lattner237f2752009-02-14 07:37:35 +0000907 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000908 if (!FD) {
909 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000910 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000911 return;
912 }
913
914 // Currently, the dllimport attribute is ignored for inlined functions.
915 // Warning is emitted.
Douglas Gregor35b57532009-10-27 21:01:01 +0000916 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000917 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
918 return;
919 }
920
921 // The attribute is also overridden by a subsequent declaration as dllexport.
922 // Warning is emitted.
923 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
924 nextAttr = nextAttr->getNext()) {
925 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
926 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
927 return;
928 }
929 }
930
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000931 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000932 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
933 return;
934 }
935
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000936 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000937}
938
Chris Lattner237f2752009-02-14 07:37:35 +0000939static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000940 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000941 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000943 return;
944 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000945
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000946 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000947 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000948 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000949 return;
950 }
951
Chris Lattner237f2752009-02-14 07:37:35 +0000952 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000953 if (!FD) {
954 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000955 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000956 return;
957 }
958
Mike Stumpd3bb5572009-07-24 19:02:52 +0000959 // Currently, the dllexport attribute is ignored for inlined functions, unless
960 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor35b57532009-10-27 21:01:01 +0000961 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000962 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
963 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
964 return;
965 }
966
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000967 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000968}
969
Nate Begemanf2758702009-06-26 06:32:41 +0000970static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
971 Sema &S) {
972 // Attribute has 3 arguments.
973 if (Attr.getNumArgs() != 3) {
974 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
975 return;
976 }
977
978 unsigned WGSize[3];
979 for (unsigned i = 0; i < 3; ++i) {
980 Expr *E = static_cast<Expr *>(Attr.getArg(i));
981 llvm::APSInt ArgNum(32);
982 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
983 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
984 << "reqd_work_group_size" << E->getSourceRange();
985 return;
986 }
987 WGSize[i] = (unsigned) ArgNum.getZExtValue();
988 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000989 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000990 WGSize[2]));
991}
992
Chris Lattner237f2752009-02-14 07:37:35 +0000993static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000994 // Attribute has no arguments.
995 if (Attr.getNumArgs() != 1) {
996 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
997 return;
998 }
999
1000 // Make sure that there is a string literal as the sections's single
1001 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +00001002 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1003 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001004 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001005 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001006 return;
1007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Chris Lattner30ba6742009-08-10 19:03:04 +00001009 std::string SectionStr(SE->getStrData(), SE->getByteLength());
1010
1011 // If the target wants to validate the section specifier, make it happen.
1012 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
1013 if (Error.empty()) {
1014 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
1015 return;
1016 }
Mike Stump11289f42009-09-09 15:08:12 +00001017
Chris Lattner30ba6742009-08-10 19:03:04 +00001018 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1019 << Error;
Mike Stump11289f42009-09-09 15:08:12 +00001020
Daniel Dunbar648bf782009-02-12 17:28:23 +00001021}
1022
Eli Friedmane4310c82009-11-09 18:38:53 +00001023static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1024 // Attribute has no arguments.
1025 if (Attr.getNumArgs() != 0) {
1026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1027 return;
1028 }
1029
1030 // Attribute can be applied only to functions.
1031 if (!isa<FunctionDecl>(d)) {
1032 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1033 << Attr.getName() << 0 /*function*/;
1034 return;
1035 }
1036
1037 // cdecl and fastcall attributes are mutually incompatible.
1038 if (d->getAttr<FastCallAttr>()) {
1039 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1040 << "cdecl" << "fastcall";
1041 return;
1042 }
1043
1044 // cdecl and stdcall attributes are mutually incompatible.
1045 if (d->getAttr<StdCallAttr>()) {
1046 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1047 << "cdecl" << "stdcall";
1048 return;
1049 }
1050
1051 d->addAttr(::new (S.Context) CDeclAttr());
1052}
1053
1054
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001055static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001056 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001057 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001058 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001059 return;
1060 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001061
1062 // Attribute can be applied only to functions.
1063 if (!isa<FunctionDecl>(d)) {
1064 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001065 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001066 return;
1067 }
1068
1069 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001070 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001071 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1072 << "stdcall" << "fastcall";
1073 return;
1074 }
1075
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001076 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001077}
1078
John McCall4976fd42009-11-04 03:36:09 +00001079/// Diagnose the use of a non-standard calling convention on the given
1080/// function.
1081static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1082 SourceLocation Loc, Sema &S) {
1083 if (!D->hasPrototype()) {
1084 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1085 return;
1086 }
1087
1088 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1089 if (T->isVariadic()) {
1090 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1091 return;
1092 }
1093}
1094
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001095static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001096 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001097 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001098 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001099 return;
1100 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001101
1102 if (!isa<FunctionDecl>(d)) {
1103 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001104 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001105 return;
1106 }
1107
John McCall4976fd42009-11-04 03:36:09 +00001108 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1109
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001110 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001111 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001112 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1113 << "fastcall" << "stdcall";
1114 return;
1115 }
1116
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001117 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001118}
1119
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001120static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001121 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001122 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001123 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001124 return;
1125 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001126
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001127 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001128}
1129
Anders Carlssonb8316282008-10-05 23:32:53 +00001130static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1131 // check the attribute arguments.
1132 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001134 return;
1135 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001136
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001137 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001138}
1139
1140static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1141 // check the attribute arguments.
1142 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001143 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001144 return;
1145 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001146
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001147 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001148}
1149
Anders Carlssond277d792009-01-31 01:16:18 +00001150static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001151 // Match gcc which ignores cleanup attrs when compiling C++.
1152 if (S.getLangOptions().CPlusPlus)
1153 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001154
1155 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1157 return;
1158 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001159
Anders Carlssond277d792009-01-31 01:16:18 +00001160 if (Attr.getNumArgs() != 0) {
1161 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1162 return;
1163 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001164
Anders Carlssond277d792009-01-31 01:16:18 +00001165 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001166
Anders Carlssond277d792009-01-31 01:16:18 +00001167 if (!VD || !VD->hasLocalStorage()) {
1168 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1169 return;
1170 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001171
Anders Carlssond277d792009-01-31 01:16:18 +00001172 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +00001173 NamedDecl *CleanupDecl
1174 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1175 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001176 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001178 Attr.getParameterName();
1179 return;
1180 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001181
Anders Carlssond277d792009-01-31 01:16:18 +00001182 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1183 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001184 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001185 Attr.getParameterName();
1186 return;
1187 }
1188
Anders Carlssond277d792009-01-31 01:16:18 +00001189 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001190 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001191 Attr.getParameterName();
1192 return;
1193 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001194
Anders Carlsson723f55d2009-02-07 23:16:50 +00001195 // We're currently more strict than GCC about what function types we accept.
1196 // If this ever proves to be a problem it should be easy to fix.
1197 QualType Ty = S.Context.getPointerType(VD->getType());
1198 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001199 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001200 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001201 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1202 Attr.getParameterName() << ParamTy << Ty;
1203 return;
1204 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001205
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001206 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001207}
1208
Mike Stumpd3bb5572009-07-24 19:02:52 +00001209/// Handle __attribute__((format_arg((idx)))) attribute based on
1210/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1211static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001212 if (Attr.getNumArgs() != 1) {
1213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1214 return;
1215 }
1216 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1217 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1218 << Attr.getName() << 0 /*function*/;
1219 return;
1220 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001221 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1222 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001223 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1224 unsigned FirstIdx = 1;
1225 // checks for the 2nd argument
1226 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1227 llvm::APSInt Idx(32);
1228 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1230 << "format" << 2 << IdxExpr->getSourceRange();
1231 return;
1232 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001233
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001234 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1235 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1236 << "format" << 2 << IdxExpr->getSourceRange();
1237 return;
1238 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001239
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001240 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001241
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001242 // make sure the format string is really a string
1243 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001244
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001245 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1246 if (not_nsstring_type &&
1247 !isCFStringType(Ty, S.Context) &&
1248 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001249 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001250 // FIXME: Should highlight the actual expression that has the wrong type.
1251 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001252 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001253 << IdxExpr->getSourceRange();
1254 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001255 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001256 Ty = getFunctionOrMethodResultType(d);
1257 if (!isNSStringType(Ty, S.Context) &&
1258 !isCFStringType(Ty, S.Context) &&
1259 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001260 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001261 // FIXME: Should highlight the actual expression that has the wrong type.
1262 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001263 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001264 << IdxExpr->getSourceRange();
1265 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001266 }
1267
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001268 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001269}
1270
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001271enum FormatAttrKind {
1272 CFStringFormat,
1273 NSStringFormat,
1274 StrftimeFormat,
1275 SupportedFormat,
1276 InvalidFormat
1277};
1278
1279/// getFormatAttrKind - Map from format attribute names to supported format
1280/// types.
1281static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1282 // Check for formats that get handled specially.
1283 if (Format == "NSString")
1284 return NSStringFormat;
1285 if (Format == "CFString")
1286 return CFStringFormat;
1287 if (Format == "strftime")
1288 return StrftimeFormat;
1289
1290 // Otherwise, check for supported formats.
1291 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1292 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1293 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1294 Format == "zcmn_err")
1295 return SupportedFormat;
1296
1297 return InvalidFormat;
1298}
1299
Mike Stumpd3bb5572009-07-24 19:02:52 +00001300/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1301/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001302static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001303
Chris Lattner4a927cb2008-06-28 23:36:30 +00001304 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001305 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001306 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001307 return;
1308 }
1309
Chris Lattner4a927cb2008-06-28 23:36:30 +00001310 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001312 return;
1313 }
1314
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001315 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001316 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001317 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001318 return;
1319 }
1320
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001321 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001322 unsigned FirstIdx = 1;
1323
Daniel Dunbar07d07852009-10-18 21:17:35 +00001324 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001325
1326 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001327 if (Format.startswith("__") && Format.endswith("__"))
1328 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001329
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001330 // Check for supported formats.
1331 FormatAttrKind Kind = getFormatAttrKind(Format);
1332 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001333 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001334 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001335 return;
1336 }
1337
1338 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001339 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001340 llvm::APSInt Idx(32);
1341 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001342 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001343 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001344 return;
1345 }
1346
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001347 // FIXME: We should handle the implicit 'this' parameter in a more generic
1348 // way that can be used for other arguments.
1349 bool HasImplicitThisParam = false;
1350 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1351 if (MD->isInstance()) {
1352 HasImplicitThisParam = true;
1353 NumArgs++;
1354 }
1355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001357 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001358 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001359 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001360 return;
1361 }
1362
1363 // FIXME: Do we need to bounds check?
1364 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001365
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001366 if (HasImplicitThisParam) {
1367 if (ArgIdx == 0) {
1368 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1369 << "a string type" << IdxExpr->getSourceRange();
1370 return;
1371 }
1372 ArgIdx--;
1373 }
Mike Stump11289f42009-09-09 15:08:12 +00001374
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001375 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001376 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001377
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001378 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001379 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001380 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1381 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001382 return;
1383 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001384 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001385 // FIXME: do we need to check if the type is NSString*? What are the
1386 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001387 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001388 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001389 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1390 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001391 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001392 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001393 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001394 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001395 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001396 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1397 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001398 return;
1399 }
1400
1401 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001402 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001403 llvm::APSInt FirstArg(32);
1404 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001406 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001407 return;
1408 }
1409
1410 // check if the function is variadic if the 3rd argument non-zero
1411 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001412 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001413 ++NumArgs; // +1 for ...
1414 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001415 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001416 return;
1417 }
1418 }
1419
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001420 // strftime requires FirstArg to be 0 because it doesn't read from any
1421 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001422 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001423 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001424 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1425 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001426 return;
1427 }
1428 // if 0 it disables parameter checking (to use with e.g. va_list)
1429 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001430 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001431 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001432 return;
1433 }
1434
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001435 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1436 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001437}
1438
Chris Lattnera663a0a2008-06-29 00:28:59 +00001439static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1440 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001441 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001442 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001444 return;
1445 }
1446
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001447 // Try to find the underlying union declaration.
1448 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001449 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001450 if (TD && TD->getUnderlyingType()->isUnionType())
1451 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1452 else
1453 RD = dyn_cast<RecordDecl>(d);
1454
1455 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001456 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001457 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001458 return;
1459 }
1460
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001461 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001462 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001463 diag::warn_transparent_union_attribute_not_definition);
1464 return;
1465 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001466
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001467 RecordDecl::field_iterator Field = RD->field_begin(),
1468 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001469 if (Field == FieldEnd) {
1470 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1471 return;
1472 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001473
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001474 FieldDecl *FirstField = *Field;
1475 QualType FirstType = FirstField->getType();
1476 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001477 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001478 diag::warn_transparent_union_attribute_floating);
1479 return;
1480 }
1481
1482 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1483 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1484 for (; Field != FieldEnd; ++Field) {
1485 QualType FieldType = Field->getType();
1486 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1487 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1488 // Warn if we drop the attribute.
1489 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001490 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001491 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001492 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001493 diag::warn_transparent_union_attribute_field_size_align)
1494 << isSize << Field->getDeclName() << FieldBits;
1495 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001497 diag::note_transparent_union_first_field_size_align)
1498 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001499 return;
1500 }
1501 }
1502
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001503 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001504}
1505
Chris Lattnera663a0a2008-06-29 00:28:59 +00001506static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001507 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001508 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001510 return;
1511 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001512 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1513 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001514
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001515 // Make sure that there is a string literal as the annotation's single
1516 // argument.
1517 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001518 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001519 return;
1520 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001521 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner9631e182009-03-04 06:34:08 +00001522 SE->getByteLength())));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001523}
1524
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001525static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001526 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001527 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001528 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001529 return;
1530 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001531
1532 //FIXME: The C++0x version of this attribute has more limited applicabilty
1533 // than GNU's, and should error out when it is used to specify a
1534 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001535
1536 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001537 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001538 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001539 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001540 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001541 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001542 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001543 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001544
Chris Lattner4627b742008-06-28 23:50:44 +00001545 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1546 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001547 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1549 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001550 return;
1551 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001552 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001553 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001554 << alignmentExpr->getSourceRange();
1555 return;
1556 }
1557
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001558 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001559}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001560
Mike Stumpd3bb5572009-07-24 19:02:52 +00001561/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1562/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001563///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001564/// Despite what would be logical, the mode attribute is a decl attribute, not a
1565/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1566/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001567static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001568 // This attribute isn't documented, but glibc uses it. It changes
1569 // the width of an int or unsigned int to the specified size.
1570
1571 // Check that there aren't any arguments
1572 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001573 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001574 return;
1575 }
1576
1577 IdentifierInfo *Name = Attr.getParameterName();
1578 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001579 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001580 return;
1581 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001582
Daniel Dunbar07d07852009-10-18 21:17:35 +00001583 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001584
1585 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001586 if (Str.startswith("__") && Str.endswith("__"))
1587 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001588
1589 unsigned DestWidth = 0;
1590 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001591 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001592 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001593 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001594 switch (Str[0]) {
1595 case 'Q': DestWidth = 8; break;
1596 case 'H': DestWidth = 16; break;
1597 case 'S': DestWidth = 32; break;
1598 case 'D': DestWidth = 64; break;
1599 case 'X': DestWidth = 96; break;
1600 case 'T': DestWidth = 128; break;
1601 }
1602 if (Str[1] == 'F') {
1603 IntegerMode = false;
1604 } else if (Str[1] == 'C') {
1605 IntegerMode = false;
1606 ComplexMode = true;
1607 } else if (Str[1] != 'I') {
1608 DestWidth = 0;
1609 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001610 break;
1611 case 4:
1612 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1613 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001614 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001615 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001616 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001617 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001618 break;
1619 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001620 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001621 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001622 break;
1623 }
1624
1625 QualType OldTy;
1626 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1627 OldTy = TD->getUnderlyingType();
1628 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1629 OldTy = VD->getType();
1630 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001631 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1632 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001633 return;
1634 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001635
John McCall9dd450b2009-09-21 23:43:11 +00001636 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001637 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1638 else if (IntegerMode) {
1639 if (!OldTy->isIntegralType())
1640 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1641 } else if (ComplexMode) {
1642 if (!OldTy->isComplexType())
1643 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1644 } else {
1645 if (!OldTy->isFloatingType())
1646 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1647 }
1648
Mike Stump87c57ac2009-05-16 07:39:55 +00001649 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1650 // and friends, at least with glibc.
1651 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1652 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001653 // FIXME: Make sure floating-point mappings are accurate
1654 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001655 QualType NewTy;
1656 switch (DestWidth) {
1657 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001658 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001659 return;
1660 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001661 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001662 return;
1663 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001664 if (!IntegerMode) {
1665 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1666 return;
1667 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001668 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001669 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001670 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001671 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001672 break;
1673 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001674 if (!IntegerMode) {
1675 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1676 return;
1677 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001678 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001679 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001680 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001681 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001682 break;
1683 case 32:
1684 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001685 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001686 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001687 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001688 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001689 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001690 break;
1691 case 64:
1692 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001693 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001694 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001695 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001696 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001697 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001698 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001699 case 96:
1700 NewTy = S.Context.LongDoubleTy;
1701 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001702 case 128:
1703 if (!IntegerMode) {
1704 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1705 return;
1706 }
1707 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman4735374e2009-03-03 06:41:03 +00001708 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001709 }
1710
Eli Friedman4735374e2009-03-03 06:41:03 +00001711 if (ComplexMode) {
1712 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001713 }
1714
1715 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001716 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1717 // FIXME: preserve existing source info.
1718 TD->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(NewTy));
1719 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001720 cast<ValueDecl>(D)->setType(NewTy);
1721}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001722
Mike Stump3722f582009-08-26 22:31:08 +00001723static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001724 // check the attribute arguments.
1725 if (Attr.getNumArgs() > 0) {
1726 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1727 return;
1728 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001729
Anders Carlsson88097122009-02-19 19:16:48 +00001730 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001732 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001733 return;
1734 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001735
Mike Stump3722f582009-08-26 22:31:08 +00001736 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001737}
1738
Mike Stump3722f582009-08-26 22:31:08 +00001739static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001740 // check the attribute arguments.
1741 if (Attr.getNumArgs() != 0) {
1742 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1743 return;
1744 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001745
Chris Lattner4225e232009-04-14 17:02:11 +00001746 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001747 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001748 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001749 return;
1750 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001751
Mike Stump3722f582009-08-26 22:31:08 +00001752 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001753}
1754
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001755static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001756 // check the attribute arguments.
1757 if (Attr.getNumArgs() != 0) {
1758 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1759 return;
1760 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001761
Chris Lattner4225e232009-04-14 17:02:11 +00001762 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1763 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001764 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001765 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001766 return;
1767 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001768
Douglas Gregor35b57532009-10-27 21:01:01 +00001769 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001770 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001771 return;
1772 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001773
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001774 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001775}
1776
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001777static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1778 // check the attribute arguments.
1779 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001780 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001781 return;
1782 }
Eli Friedman7044b762009-03-27 21:06:47 +00001783
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001784 if (!isFunctionOrMethod(d)) {
1785 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001786 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001787 return;
1788 }
Eli Friedman7044b762009-03-27 21:06:47 +00001789
1790 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1791 llvm::APSInt NumParams(32);
1792 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1794 << "regparm" << NumParamsExpr->getSourceRange();
1795 return;
1796 }
1797
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001798 if (S.Context.Target.getRegParmMax() == 0) {
1799 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001800 << NumParamsExpr->getSourceRange();
1801 return;
1802 }
1803
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001804 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001805 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1806 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001807 return;
1808 }
1809
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001810 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001811}
1812
Alexis Hunt96d5c762009-11-21 08:43:09 +00001813static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1814 // check the attribute arguments.
1815 if (Attr.getNumArgs() != 0) {
1816 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1817 return;
1818 }
1819
1820 if (!isa<CXXRecordDecl>(d)
1821 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1822 S.Diag(Attr.getLoc(),
1823 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1824 : diag::warn_attribute_wrong_decl_type)
1825 << Attr.getName() << 7 /*virtual method or class*/;
1826 return;
1827 }
Alexis Hunt54a02542009-11-25 04:20:27 +00001828
1829 // FIXME: Conform to C++0x redeclaration rules.
1830
1831 if (d->getAttr<FinalAttr>()) {
1832 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1833 return;
1834 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001835
1836 d->addAttr(::new (S.Context) FinalAttr());
1837}
1838
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001839//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00001840// C++0x member checking attributes
1841//===----------------------------------------------------------------------===//
1842
1843static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1844 if (Attr.getNumArgs() != 0) {
1845 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1846 return;
1847 }
1848
1849 if (!isa<CXXRecordDecl>(d)) {
1850 S.Diag(Attr.getLoc(),
1851 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1852 : diag::warn_attribute_wrong_decl_type)
1853 << Attr.getName() << 9 /*class*/;
1854 return;
1855 }
1856
1857 if (d->getAttr<BaseCheckAttr>()) {
1858 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1859 return;
1860 }
1861
1862 d->addAttr(::new (S.Context) BaseCheckAttr());
1863}
1864
1865static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1866 if (Attr.getNumArgs() != 0) {
1867 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1868 return;
1869 }
1870
1871 if (!isa<RecordDecl>(d->getDeclContext())) {
1872 // FIXME: It's not the type that's the problem
1873 S.Diag(Attr.getLoc(),
1874 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1875 : diag::warn_attribute_wrong_decl_type)
1876 << Attr.getName() << 11 /*member*/;
1877 return;
1878 }
1879
1880 // FIXME: Conform to C++0x redeclaration rules.
1881
1882 if (d->getAttr<HidingAttr>()) {
1883 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1884 return;
1885 }
1886
1887 d->addAttr(::new (S.Context) HidingAttr());
1888}
1889
1890static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1891 if (Attr.getNumArgs() != 0) {
1892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1893 return;
1894 }
1895
1896 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1897 // FIXME: It's not the type that's the problem
1898 S.Diag(Attr.getLoc(),
1899 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1900 : diag::warn_attribute_wrong_decl_type)
1901 << Attr.getName() << 10 /*virtual method*/;
1902 return;
1903 }
1904
1905 // FIXME: Conform to C++0x redeclaration rules.
1906
1907 if (d->getAttr<OverrideAttr>()) {
1908 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1909 return;
1910 }
1911
1912 d->addAttr(::new (S.Context) OverrideAttr());
1913}
1914
1915//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001916// Checker-specific attribute handlers.
1917//===----------------------------------------------------------------------===//
1918
1919static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1920 Sema &S) {
1921
Ted Kremenek3b204e42009-05-13 21:07:32 +00001922 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001923
Ted Kremenek3b204e42009-05-13 21:07:32 +00001924 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1925 RetTy = MD->getResultType();
1926 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1927 RetTy = FD->getResultType();
1928 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001929 SourceLocation L = Attr.getLoc();
1930 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1931 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001932 return;
1933 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001934
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001935 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001936 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001937 SourceLocation L = Attr.getLoc();
1938 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1939 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001940 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001941 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001942
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001943 switch (Attr.getKind()) {
1944 default:
1945 assert(0 && "invalid ownership attribute");
1946 return;
1947 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001948 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001949 return;
1950 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001951 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001952 return;
1953 };
1954}
1955
1956//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001957// Top Level Sema Entry Points
1958//===----------------------------------------------------------------------===//
1959
Sebastian Redlfc24b632008-12-21 19:24:58 +00001960/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001961/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001962/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1963/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001964static void ProcessDeclAttribute(Scope *scope, Decl *D,
1965 const AttributeList &Attr, Sema &S) {
Eli Friedman53339e02009-06-08 23:27:34 +00001966 if (Attr.isDeclspecAttribute())
1967 // FIXME: Try to deal with __declspec attributes!
1968 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001969 switch (Attr.getKind()) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001970 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001971 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001972 case AttributeList::AT_objc_gc:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001973 // Ignore these, these are type attributes, handled by
1974 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001975 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001976 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1977 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001978 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001979 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001980 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001981 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001982 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1983 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001984 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001985 HandleDependencyAttr (D, Attr, S); break;
1986 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1987 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1988 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1989 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1990 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1991 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001992 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001993 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001994 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001995 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1996 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1997 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1998 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1999 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2000 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2001 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2002 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2003 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
2004 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2005 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2006 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002007
2008 // Checker-specific.
2009 case AttributeList::AT_ns_returns_retained:
2010 case AttributeList::AT_cf_returns_retained:
2011 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2012
Nate Begemanf2758702009-06-26 06:32:41 +00002013 case AttributeList::AT_reqd_wg_size:
2014 HandleReqdWorkGroupSize(D, Attr, S); break;
2015
Alexis Hunt54a02542009-11-25 04:20:27 +00002016 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2017 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
2018 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
2019 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2020 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2021 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
2022 case AttributeList::AT_vector_size: HandleVectorSizeAttr (D, Attr, S); break;
2023 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002024 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2025 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002026 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
2027 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002028 case AttributeList::AT_transparent_union:
2029 HandleTransparentUnionAttr(D, Attr, S);
2030 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002031 case AttributeList::AT_objc_exception:
2032 HandleObjCExceptionAttr(D, Attr, S);
2033 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002034 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002035 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2036 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2037 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2038 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2039 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2040 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2041 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2042 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2043 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002044 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00002045 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00002046 // Just ignore
2047 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002048 default:
Anders Carlsson76187b42009-02-13 06:46:13 +00002049 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002050 break;
2051 }
2052}
2053
2054/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2055/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00002056void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002057 while (AttrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +00002058 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002059 AttrList = AttrList->getNext();
2060 }
2061}
2062
Ryan Flynn7d470f32009-07-30 03:15:39 +00002063/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2064/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002065NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002066 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002067 NamedDecl *NewD = 0;
2068 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2069 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2070 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002071 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002072 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2073 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2074 VD->getLocation(), II,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002075 VD->getType(), VD->getDeclaratorInfo(),
2076 VD->getStorageClass());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002077 }
2078 return NewD;
2079}
2080
2081/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2082/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002083void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002084 if (W.getUsed()) return; // only do this once
2085 W.setUsed(true);
2086 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2087 IdentifierInfo *NDId = ND->getIdentifier();
2088 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
2089 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2090 NewD->addAttr(::new (Context) WeakAttr());
2091 WeakTopLevelDecl.push_back(NewD);
2092 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2093 // to insert Decl at TU scope, sorry.
2094 DeclContext *SavedContext = CurContext;
2095 CurContext = Context.getTranslationUnitDecl();
2096 PushOnScopeChains(NewD, S);
2097 CurContext = SavedContext;
2098 } else { // just add weak to existing
2099 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002100 }
2101}
2102
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002103/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2104/// it, apply them to D. This is a bit tricky because PD can have attributes
2105/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002106void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002107 // Handle #pragma weak
2108 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2109 if (ND->hasLinkage()) {
2110 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2111 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002112 // Identifier referenced by #pragma weak before it was declared
2113 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002114 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2115 }
2116 }
2117 }
2118
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002119 // Apply decl attributes from the DeclSpec if present.
2120 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002121 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002122
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002123 // Walk the declarator structure, applying decl attributes that were in a type
2124 // position to the decl itself. This handles cases like:
2125 // int *__attr__(x)** D;
2126 // when X is a decl attribute.
2127 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2128 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002129 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002130
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002131 // Finally, apply any attributes on the decl itself.
2132 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002133 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002134}
John McCall28a6aea2009-11-04 02:18:39 +00002135
2136/// PushParsingDeclaration - Enter a new "scope" of deprecation
2137/// warnings.
2138///
2139/// The state token we use is the start index of this scope
2140/// on the warning stack.
2141Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2142 ParsingDeclDepth++;
2143 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2144}
2145
2146static bool isDeclDeprecated(Decl *D) {
2147 do {
2148 if (D->hasAttr<DeprecatedAttr>())
2149 return true;
2150 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2151 return false;
2152}
2153
2154void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2155 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2156 ParsingDeclDepth--;
2157
2158 if (DelayedDeprecationWarnings.empty())
2159 return;
2160
2161 unsigned SavedIndex = (unsigned) S;
2162 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2163 "saved index is out of bounds");
2164
2165 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2166 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2167 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2168 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2169 if (ND) {
2170 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2171
2172 // Prevent this from triggering multiple times.
2173 ND = 0;
2174 }
2175 }
2176 }
2177
2178 DelayedDeprecationWarnings.set_size(SavedIndex);
2179}
2180
2181void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2182 // Delay if we're currently parsing a declaration.
2183 if (ParsingDeclDepth) {
2184 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2185 return;
2186 }
2187
2188 // Otherwise, don't warn if our current context is deprecated.
2189 if (isDeclDeprecated(cast<Decl>(CurContext)))
2190 return;
2191
2192 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2193}