blob: b2124fe01459b8b23fb1ab0b71b0018e8da961e3 [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 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000415
Benjamin Kramer5f089122009-11-30 17:08:26 +0000416 d->addAttr(::new (S.Context) AliasAttr(Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000417}
418
Mike Stumpd3bb5572009-07-24 19:02:52 +0000419static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000420 Sema &S) {
421 // check the attribute arguments.
422 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000424 return;
425 }
Anders Carlsson88097122009-02-19 19:16:48 +0000426
Chris Lattner4225e232009-04-14 17:02:11 +0000427 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000428 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000429 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000430 return;
431 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000432
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000433 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000434}
435
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000436static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
437 // check the attribute arguments.
438 if (Attr.getNumArgs() != 0) {
439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
440 return;
441 }
Mike Stump11289f42009-09-09 15:08:12 +0000442
Ted Kremenek08479ae2009-08-15 00:51:46 +0000443 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000444 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000445 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
446 d->addAttr(::new (S.Context) MallocAttr());
447 return;
448 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000449 }
450
Ted Kremenek08479ae2009-08-15 00:51:46 +0000451 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000452}
453
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000454static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000455 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000456 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000457 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000459 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000460 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000461
Mike Stump88788fe2009-04-29 19:03:13 +0000462 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
463 ValueDecl *VD = dyn_cast<ValueDecl>(d);
464 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000465 S.Diag(Attr.getLoc(),
466 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
467 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000468 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000469 return false;
470 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000471 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000472
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000473 return true;
474}
475
476static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000477 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000478 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000479}
480
481static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
482 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000483 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000484 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000485}
486
Alexis Hunt96d5c762009-11-21 08:43:09 +0000487static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
488 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
489 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
490 << Attr.getName() << 8; /*function, method, or parameter*/
491 return;
492 }
493 // FIXME: Actually store the attribute on the declaration
494}
495
Ted Kremenek39c59a82008-07-25 04:39:19 +0000496static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
497 // check the attribute arguments.
498 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000499 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000500 return;
501 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000502
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000503 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000504 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000505 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000506 return;
507 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000508
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000509 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000510}
511
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000512static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
513 // check the attribute arguments.
514 if (Attr.getNumArgs() != 0) {
515 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
516 return;
517 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000518
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000519 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000520 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000521 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
522 return;
523 }
524 } else if (!isFunctionOrMethod(d)) {
525 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000526 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000527 return;
528 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000529
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000530 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000531}
532
Daniel Dunbar032db472008-07-31 22:40:48 +0000533static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
534 // check the attribute arguments.
535 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
537 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000538 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000539 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000540
541 int priority = 65535; // FIXME: Do not hardcode such constants.
542 if (Attr.getNumArgs() > 0) {
543 Expr *E = static_cast<Expr *>(Attr.getArg(0));
544 llvm::APSInt Idx(32);
545 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000547 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000548 return;
549 }
550 priority = Idx.getZExtValue();
551 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000552
Chris Lattner4225e232009-04-14 17:02:11 +0000553 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000554 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000555 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000556 return;
557 }
558
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000559 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000560}
561
562static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
563 // check the attribute arguments.
564 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000565 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
566 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000567 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000568 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000569
570 int priority = 65535; // FIXME: Do not hardcode such constants.
571 if (Attr.getNumArgs() > 0) {
572 Expr *E = static_cast<Expr *>(Attr.getArg(0));
573 llvm::APSInt Idx(32);
574 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000575 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000576 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000577 return;
578 }
579 priority = Idx.getZExtValue();
580 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000581
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000582 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000583 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000584 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000585 return;
586 }
587
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000588 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000589}
590
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000591static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000592 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000593 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000594 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000595 return;
596 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000597
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000598 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000599}
600
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000601static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
602 // check the attribute arguments.
603 if (Attr.getNumArgs() != 0) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
605 return;
606 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000607
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000608 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000609}
610
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000611static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000612 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000613 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000615 return;
616 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000617
Chris Lattner4a927cb2008-06-28 23:36:30 +0000618 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000619 Arg = Arg->IgnoreParenCasts();
620 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000621
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000622 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000623 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000624 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000625 return;
626 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000627
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000628 const char *TypeStr = Str->getStrData();
629 unsigned TypeLen = Str->getByteLength();
630 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000631
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000632 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
633 type = VisibilityAttr::DefaultVisibility;
634 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
635 type = VisibilityAttr::HiddenVisibility;
636 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
637 type = VisibilityAttr::HiddenVisibility; // FIXME
638 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
639 type = VisibilityAttr::ProtectedVisibility;
640 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000641 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000642 return;
643 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000644
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000645 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000646}
647
Chris Lattner677a3582009-02-14 08:09:34 +0000648static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
649 Sema &S) {
650 if (Attr.getNumArgs() != 0) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
652 return;
653 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000654
Chris Lattner677a3582009-02-14 08:09:34 +0000655 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
656 if (OCI == 0) {
657 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
658 return;
659 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000660
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000661 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000662}
663
664static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000665 if (Attr.getNumArgs() != 0) {
666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
667 return;
668 }
Chris Lattner677a3582009-02-14 08:09:34 +0000669 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000670 QualType T = TD->getUnderlyingType();
671 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000672 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000673 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
674 return;
675 }
676 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000677 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000678}
679
Mike Stumpd3bb5572009-07-24 19:02:52 +0000680static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000681HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
682 if (Attr.getNumArgs() != 0) {
683 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
684 return;
685 }
686
687 if (!isa<FunctionDecl>(D)) {
688 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
689 return;
690 }
691
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000692 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000693}
694
Steve Naroff3405a732008-09-18 16:44:58 +0000695static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000696 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000697 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000698 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000699 return;
700 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000701
Steve Naroff3405a732008-09-18 16:44:58 +0000702 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000703 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000704 return;
705 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000706
Steve Naroff3405a732008-09-18 16:44:58 +0000707 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000708 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000709 type = BlocksAttr::ByRef;
710 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000711 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000712 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000713 return;
714 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000715
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000716 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000717}
718
Anders Carlssonc181b012008-10-05 18:05:59 +0000719static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
720 // check the attribute arguments.
721 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
723 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000724 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000725 }
726
Anders Carlssonc181b012008-10-05 18:05:59 +0000727 int sentinel = 0;
728 if (Attr.getNumArgs() > 0) {
729 Expr *E = static_cast<Expr *>(Attr.getArg(0));
730 llvm::APSInt Idx(32);
731 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000732 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000733 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000734 return;
735 }
736 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000737
Anders Carlssonc181b012008-10-05 18:05:59 +0000738 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000739 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
740 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000741 return;
742 }
743 }
744
745 int nullPos = 0;
746 if (Attr.getNumArgs() > 1) {
747 Expr *E = static_cast<Expr *>(Attr.getArg(1));
748 llvm::APSInt Idx(32);
749 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000750 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000751 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000752 return;
753 }
754 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000755
Anders Carlssonc181b012008-10-05 18:05:59 +0000756 if (nullPos > 1 || nullPos < 0) {
757 // FIXME: This error message could be improved, it would be nice
758 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000759 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
760 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000761 return;
762 }
763 }
764
765 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000766 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000767 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000768
Chris Lattner9363e312009-03-17 23:03:47 +0000769 if (isa<FunctionNoProtoType>(FT)) {
770 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
771 return;
772 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000773
Chris Lattner9363e312009-03-17 23:03:47 +0000774 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000775 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000776 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000777 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000778 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
779 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000780 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000781 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000782 }
783 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000784 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
785 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000786 ;
787 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000788 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000789 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000790 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000791 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000792 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000793 int m = Ty->isFunctionPointerType() ? 0 : 1;
794 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000795 return;
796 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000797 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000798 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000799 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000800 return;
801 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000802 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000803 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000804 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000805 return;
806 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000807 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000808}
809
Chris Lattner237f2752009-02-14 07:37:35 +0000810static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
811 // check the attribute arguments.
812 if (Attr.getNumArgs() != 0) {
813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
814 return;
815 }
816
817 // TODO: could also be applied to methods?
818 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
819 if (!Fn) {
820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000821 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000822 return;
823 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000824
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000825 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000826}
827
828static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000829 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000830 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000831 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000832 return;
833 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000834
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000835 /* weak only applies to non-static declarations */
836 bool isStatic = false;
837 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
838 isStatic = VD->getStorageClass() == VarDecl::Static;
839 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
840 isStatic = FD->getStorageClass() == FunctionDecl::Static;
841 }
842 if (isStatic) {
843 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
844 dyn_cast<NamedDecl>(D)->getNameAsString();
845 return;
846 }
847
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000848 // TODO: could also be applied to methods?
849 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
850 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000851 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000852 return;
853 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000854
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000855 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000856}
857
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000858static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
859 // check the attribute arguments.
860 if (Attr.getNumArgs() != 0) {
861 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
862 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000863 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000864
865 // weak_import only applies to variable & function declarations.
866 bool isDef = false;
867 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
868 isDef = (!VD->hasExternalStorage() || VD->getInit());
869 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000870 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000871 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
872 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000873 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000874 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000875 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000876 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000877 return;
878 }
879
880 // Merge should handle any subsequent violations.
881 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000882 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000883 diag::warn_attribute_weak_import_invalid_on_definition)
884 << "weak_import" << 2 /*variable and function*/;
885 return;
886 }
887
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000888 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000889}
890
Chris Lattner237f2752009-02-14 07:37:35 +0000891static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000892 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000893 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000894 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000895 return;
896 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000897
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000898 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000899 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000900 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000901 return;
902 }
903
Chris Lattner237f2752009-02-14 07:37:35 +0000904 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000905 if (!FD) {
906 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000907 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000908 return;
909 }
910
911 // Currently, the dllimport attribute is ignored for inlined functions.
912 // Warning is emitted.
Douglas Gregor35b57532009-10-27 21:01:01 +0000913 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000914 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
915 return;
916 }
917
918 // The attribute is also overridden by a subsequent declaration as dllexport.
919 // Warning is emitted.
920 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
921 nextAttr = nextAttr->getNext()) {
922 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
923 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
924 return;
925 }
926 }
927
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000928 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000929 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
930 return;
931 }
932
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000933 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000934}
935
Chris Lattner237f2752009-02-14 07:37:35 +0000936static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000937 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000938 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000939 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000940 return;
941 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000942
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000943 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000944 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000945 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000946 return;
947 }
948
Chris Lattner237f2752009-02-14 07:37:35 +0000949 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000950 if (!FD) {
951 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000952 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000953 return;
954 }
955
Mike Stumpd3bb5572009-07-24 19:02:52 +0000956 // Currently, the dllexport attribute is ignored for inlined functions, unless
957 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor35b57532009-10-27 21:01:01 +0000958 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000959 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
960 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
961 return;
962 }
963
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000964 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000965}
966
Nate Begemanf2758702009-06-26 06:32:41 +0000967static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
968 Sema &S) {
969 // Attribute has 3 arguments.
970 if (Attr.getNumArgs() != 3) {
971 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
972 return;
973 }
974
975 unsigned WGSize[3];
976 for (unsigned i = 0; i < 3; ++i) {
977 Expr *E = static_cast<Expr *>(Attr.getArg(i));
978 llvm::APSInt ArgNum(32);
979 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
980 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
981 << "reqd_work_group_size" << E->getSourceRange();
982 return;
983 }
984 WGSize[i] = (unsigned) ArgNum.getZExtValue();
985 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000986 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000987 WGSize[2]));
988}
989
Chris Lattner237f2752009-02-14 07:37:35 +0000990static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000991 // Attribute has no arguments.
992 if (Attr.getNumArgs() != 1) {
993 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
994 return;
995 }
996
997 // Make sure that there is a string literal as the sections's single
998 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +0000999 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1000 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001001 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001002 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001003 return;
1004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Chris Lattner30ba6742009-08-10 19:03:04 +00001006 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001007 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner30ba6742009-08-10 19:03:04 +00001008 if (Error.empty()) {
Benjamin Kramer5f089122009-11-30 17:08:26 +00001009 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Chris Lattner30ba6742009-08-10 19:03:04 +00001010 return;
1011 }
Mike Stump11289f42009-09-09 15:08:12 +00001012
Chris Lattner30ba6742009-08-10 19:03:04 +00001013 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1014 << Error;
Mike Stump11289f42009-09-09 15:08:12 +00001015
Daniel Dunbar648bf782009-02-12 17:28:23 +00001016}
1017
Eli Friedmane4310c82009-11-09 18:38:53 +00001018static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1019 // Attribute has no arguments.
1020 if (Attr.getNumArgs() != 0) {
1021 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1022 return;
1023 }
1024
1025 // Attribute can be applied only to functions.
1026 if (!isa<FunctionDecl>(d)) {
1027 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1028 << Attr.getName() << 0 /*function*/;
1029 return;
1030 }
1031
1032 // cdecl and fastcall attributes are mutually incompatible.
1033 if (d->getAttr<FastCallAttr>()) {
1034 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1035 << "cdecl" << "fastcall";
1036 return;
1037 }
1038
1039 // cdecl and stdcall attributes are mutually incompatible.
1040 if (d->getAttr<StdCallAttr>()) {
1041 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1042 << "cdecl" << "stdcall";
1043 return;
1044 }
1045
1046 d->addAttr(::new (S.Context) CDeclAttr());
1047}
1048
1049
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001050static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001051 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001052 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001053 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001054 return;
1055 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001056
1057 // Attribute can be applied only to functions.
1058 if (!isa<FunctionDecl>(d)) {
1059 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001060 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001061 return;
1062 }
1063
1064 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001065 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001066 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1067 << "stdcall" << "fastcall";
1068 return;
1069 }
1070
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001071 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001072}
1073
John McCall4976fd42009-11-04 03:36:09 +00001074/// Diagnose the use of a non-standard calling convention on the given
1075/// function.
1076static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1077 SourceLocation Loc, Sema &S) {
1078 if (!D->hasPrototype()) {
1079 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1080 return;
1081 }
1082
1083 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1084 if (T->isVariadic()) {
1085 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1086 return;
1087 }
1088}
1089
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001090static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001091 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001092 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001094 return;
1095 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001096
1097 if (!isa<FunctionDecl>(d)) {
1098 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001099 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001100 return;
1101 }
1102
John McCall4976fd42009-11-04 03:36:09 +00001103 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1104
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001105 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001106 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001107 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1108 << "fastcall" << "stdcall";
1109 return;
1110 }
1111
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001112 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001113}
1114
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001115static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001116 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001117 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001118 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001119 return;
1120 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001121
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001122 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001123}
1124
Anders Carlssonb8316282008-10-05 23:32:53 +00001125static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1126 // check the attribute arguments.
1127 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001128 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001129 return;
1130 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001131
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001132 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001133}
1134
1135static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1136 // check the attribute arguments.
1137 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001138 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001139 return;
1140 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001141
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001142 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001143}
1144
Anders Carlssond277d792009-01-31 01:16:18 +00001145static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001146 // Match gcc which ignores cleanup attrs when compiling C++.
1147 if (S.getLangOptions().CPlusPlus)
1148 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001149
1150 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001151 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1152 return;
1153 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001154
Anders Carlssond277d792009-01-31 01:16:18 +00001155 if (Attr.getNumArgs() != 0) {
1156 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 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001161
Anders Carlssond277d792009-01-31 01:16:18 +00001162 if (!VD || !VD->hasLocalStorage()) {
1163 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1164 return;
1165 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001166
Anders Carlssond277d792009-01-31 01:16:18 +00001167 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +00001168 NamedDecl *CleanupDecl
1169 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1170 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001171 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001172 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001173 Attr.getParameterName();
1174 return;
1175 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001176
Anders Carlssond277d792009-01-31 01:16:18 +00001177 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1178 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001179 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001180 Attr.getParameterName();
1181 return;
1182 }
1183
Anders Carlssond277d792009-01-31 01:16:18 +00001184 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001185 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001186 Attr.getParameterName();
1187 return;
1188 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001189
Anders Carlsson723f55d2009-02-07 23:16:50 +00001190 // We're currently more strict than GCC about what function types we accept.
1191 // If this ever proves to be a problem it should be easy to fix.
1192 QualType Ty = S.Context.getPointerType(VD->getType());
1193 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001194 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001195 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001196 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1197 Attr.getParameterName() << ParamTy << Ty;
1198 return;
1199 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001200
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001201 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001202}
1203
Mike Stumpd3bb5572009-07-24 19:02:52 +00001204/// Handle __attribute__((format_arg((idx)))) attribute based on
1205/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1206static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001207 if (Attr.getNumArgs() != 1) {
1208 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1209 return;
1210 }
1211 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1212 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1213 << Attr.getName() << 0 /*function*/;
1214 return;
1215 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001216 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1217 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001218 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1219 unsigned FirstIdx = 1;
1220 // checks for the 2nd argument
1221 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1222 llvm::APSInt Idx(32);
1223 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1225 << "format" << 2 << IdxExpr->getSourceRange();
1226 return;
1227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001228
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001229 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1231 << "format" << 2 << IdxExpr->getSourceRange();
1232 return;
1233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001234
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001235 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001236
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001237 // make sure the format string is really a string
1238 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001239
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001240 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1241 if (not_nsstring_type &&
1242 !isCFStringType(Ty, S.Context) &&
1243 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001244 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001245 // FIXME: Should highlight the actual expression that has the wrong type.
1246 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001247 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001248 << IdxExpr->getSourceRange();
1249 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001250 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001251 Ty = getFunctionOrMethodResultType(d);
1252 if (!isNSStringType(Ty, S.Context) &&
1253 !isCFStringType(Ty, S.Context) &&
1254 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001255 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001256 // FIXME: Should highlight the actual expression that has the wrong type.
1257 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001258 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001259 << IdxExpr->getSourceRange();
1260 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261 }
1262
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001263 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001264}
1265
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001266enum FormatAttrKind {
1267 CFStringFormat,
1268 NSStringFormat,
1269 StrftimeFormat,
1270 SupportedFormat,
1271 InvalidFormat
1272};
1273
1274/// getFormatAttrKind - Map from format attribute names to supported format
1275/// types.
1276static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1277 // Check for formats that get handled specially.
1278 if (Format == "NSString")
1279 return NSStringFormat;
1280 if (Format == "CFString")
1281 return CFStringFormat;
1282 if (Format == "strftime")
1283 return StrftimeFormat;
1284
1285 // Otherwise, check for supported formats.
1286 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1287 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1288 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1289 Format == "zcmn_err")
1290 return SupportedFormat;
1291
1292 return InvalidFormat;
1293}
1294
Mike Stumpd3bb5572009-07-24 19:02:52 +00001295/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1296/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001297static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001298
Chris Lattner4a927cb2008-06-28 23:36:30 +00001299 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001300 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001301 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001302 return;
1303 }
1304
Chris Lattner4a927cb2008-06-28 23:36:30 +00001305 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001307 return;
1308 }
1309
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001310 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001311 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001312 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001313 return;
1314 }
1315
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001316 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001317 unsigned FirstIdx = 1;
1318
Daniel Dunbar07d07852009-10-18 21:17:35 +00001319 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001320
1321 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001322 if (Format.startswith("__") && Format.endswith("__"))
1323 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001324
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001325 // Check for supported formats.
1326 FormatAttrKind Kind = getFormatAttrKind(Format);
1327 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001328 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001329 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001330 return;
1331 }
1332
1333 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001334 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001335 llvm::APSInt Idx(32);
1336 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001338 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001339 return;
1340 }
1341
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001342 // FIXME: We should handle the implicit 'this' parameter in a more generic
1343 // way that can be used for other arguments.
1344 bool HasImplicitThisParam = false;
1345 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1346 if (MD->isInstance()) {
1347 HasImplicitThisParam = true;
1348 NumArgs++;
1349 }
1350 }
Mike Stump11289f42009-09-09 15:08:12 +00001351
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001352 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001353 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001354 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001355 return;
1356 }
1357
1358 // FIXME: Do we need to bounds check?
1359 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001360
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001361 if (HasImplicitThisParam) {
1362 if (ArgIdx == 0) {
1363 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1364 << "a string type" << IdxExpr->getSourceRange();
1365 return;
1366 }
1367 ArgIdx--;
1368 }
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001370 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001371 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001372
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001373 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001374 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001375 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1376 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001377 return;
1378 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001379 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001380 // FIXME: do we need to check if the type is NSString*? What are the
1381 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001382 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001383 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001384 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1385 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001386 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001387 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001388 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001389 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001390 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001391 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1392 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001393 return;
1394 }
1395
1396 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001397 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001398 llvm::APSInt FirstArg(32);
1399 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001400 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001401 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001402 return;
1403 }
1404
1405 // check if the function is variadic if the 3rd argument non-zero
1406 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001407 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001408 ++NumArgs; // +1 for ...
1409 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001410 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001411 return;
1412 }
1413 }
1414
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001415 // strftime requires FirstArg to be 0 because it doesn't read from any
1416 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001417 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001418 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001419 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1420 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001421 return;
1422 }
1423 // if 0 it disables parameter checking (to use with e.g. va_list)
1424 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001425 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001426 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001427 return;
1428 }
1429
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001430 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1431 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001432}
1433
Chris Lattnera663a0a2008-06-29 00:28:59 +00001434static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1435 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001436 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001437 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001439 return;
1440 }
1441
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001442 // Try to find the underlying union declaration.
1443 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001444 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001445 if (TD && TD->getUnderlyingType()->isUnionType())
1446 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1447 else
1448 RD = dyn_cast<RecordDecl>(d);
1449
1450 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001451 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001452 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001453 return;
1454 }
1455
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001456 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001457 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001458 diag::warn_transparent_union_attribute_not_definition);
1459 return;
1460 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001461
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001462 RecordDecl::field_iterator Field = RD->field_begin(),
1463 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001464 if (Field == FieldEnd) {
1465 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1466 return;
1467 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001468
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001469 FieldDecl *FirstField = *Field;
1470 QualType FirstType = FirstField->getType();
1471 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001472 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001473 diag::warn_transparent_union_attribute_floating);
1474 return;
1475 }
1476
1477 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1478 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1479 for (; Field != FieldEnd; ++Field) {
1480 QualType FieldType = Field->getType();
1481 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1482 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1483 // Warn if we drop the attribute.
1484 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001485 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001486 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001487 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001488 diag::warn_transparent_union_attribute_field_size_align)
1489 << isSize << Field->getDeclName() << FieldBits;
1490 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001491 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001492 diag::note_transparent_union_first_field_size_align)
1493 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001494 return;
1495 }
1496 }
1497
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001498 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001499}
1500
Chris Lattnera663a0a2008-06-29 00:28:59 +00001501static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001502 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001503 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001505 return;
1506 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001507 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1508 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001509
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001510 // Make sure that there is a string literal as the annotation's single
1511 // argument.
1512 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001513 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001514 return;
1515 }
Benjamin Kramer5f089122009-11-30 17:08:26 +00001516 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001517}
1518
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001519static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001520 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001521 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001522 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001523 return;
1524 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001525
1526 //FIXME: The C++0x version of this attribute has more limited applicabilty
1527 // than GNU's, and should error out when it is used to specify a
1528 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001529
1530 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001531 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001532 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001533 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001534 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001535 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001536 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001537 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001538
Chris Lattner4627b742008-06-28 23:50:44 +00001539 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1540 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001541 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001542 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1543 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001544 return;
1545 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001546 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001547 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001548 << alignmentExpr->getSourceRange();
1549 return;
1550 }
1551
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001552 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001553}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001554
Mike Stumpd3bb5572009-07-24 19:02:52 +00001555/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1556/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001557///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001558/// Despite what would be logical, the mode attribute is a decl attribute, not a
1559/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1560/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001561static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001562 // This attribute isn't documented, but glibc uses it. It changes
1563 // the width of an int or unsigned int to the specified size.
1564
1565 // Check that there aren't any arguments
1566 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001568 return;
1569 }
1570
1571 IdentifierInfo *Name = Attr.getParameterName();
1572 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001573 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001574 return;
1575 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001576
Daniel Dunbar07d07852009-10-18 21:17:35 +00001577 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001578
1579 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001580 if (Str.startswith("__") && Str.endswith("__"))
1581 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001582
1583 unsigned DestWidth = 0;
1584 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001585 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001586 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001587 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001588 switch (Str[0]) {
1589 case 'Q': DestWidth = 8; break;
1590 case 'H': DestWidth = 16; break;
1591 case 'S': DestWidth = 32; break;
1592 case 'D': DestWidth = 64; break;
1593 case 'X': DestWidth = 96; break;
1594 case 'T': DestWidth = 128; break;
1595 }
1596 if (Str[1] == 'F') {
1597 IntegerMode = false;
1598 } else if (Str[1] == 'C') {
1599 IntegerMode = false;
1600 ComplexMode = true;
1601 } else if (Str[1] != 'I') {
1602 DestWidth = 0;
1603 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001604 break;
1605 case 4:
1606 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1607 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001608 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001609 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001610 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001611 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001612 break;
1613 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001614 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001615 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001616 break;
1617 }
1618
1619 QualType OldTy;
1620 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1621 OldTy = TD->getUnderlyingType();
1622 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1623 OldTy = VD->getType();
1624 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001625 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1626 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001627 return;
1628 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001629
John McCall9dd450b2009-09-21 23:43:11 +00001630 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001631 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1632 else if (IntegerMode) {
1633 if (!OldTy->isIntegralType())
1634 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1635 } else if (ComplexMode) {
1636 if (!OldTy->isComplexType())
1637 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1638 } else {
1639 if (!OldTy->isFloatingType())
1640 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1641 }
1642
Mike Stump87c57ac2009-05-16 07:39:55 +00001643 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1644 // and friends, at least with glibc.
1645 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1646 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001647 // FIXME: Make sure floating-point mappings are accurate
1648 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001649 QualType NewTy;
1650 switch (DestWidth) {
1651 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001652 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001653 return;
1654 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001655 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001656 return;
1657 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001658 if (!IntegerMode) {
1659 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1660 return;
1661 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001662 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001663 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001664 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001665 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001666 break;
1667 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001668 if (!IntegerMode) {
1669 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1670 return;
1671 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001672 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001673 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001674 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001675 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001676 break;
1677 case 32:
1678 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001679 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001680 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001681 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001682 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001683 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001684 break;
1685 case 64:
1686 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001687 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001688 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001689 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001690 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001691 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001692 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001693 case 96:
1694 NewTy = S.Context.LongDoubleTy;
1695 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001696 case 128:
1697 if (!IntegerMode) {
1698 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1699 return;
1700 }
1701 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman4735374e2009-03-03 06:41:03 +00001702 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001703 }
1704
Eli Friedman4735374e2009-03-03 06:41:03 +00001705 if (ComplexMode) {
1706 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001707 }
1708
1709 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001710 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1711 // FIXME: preserve existing source info.
1712 TD->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(NewTy));
1713 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001714 cast<ValueDecl>(D)->setType(NewTy);
1715}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001716
Mike Stump3722f582009-08-26 22:31:08 +00001717static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001718 // check the attribute arguments.
1719 if (Attr.getNumArgs() > 0) {
1720 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1721 return;
1722 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001723
Anders Carlsson88097122009-02-19 19:16:48 +00001724 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001725 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001726 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001727 return;
1728 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001729
Mike Stump3722f582009-08-26 22:31:08 +00001730 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001731}
1732
Mike Stump3722f582009-08-26 22:31:08 +00001733static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001734 // check the attribute arguments.
1735 if (Attr.getNumArgs() != 0) {
1736 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1737 return;
1738 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001739
Chris Lattner4225e232009-04-14 17:02:11 +00001740 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001742 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001743 return;
1744 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001745
Mike Stump3722f582009-08-26 22:31:08 +00001746 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001747}
1748
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001749static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001750 // check the attribute arguments.
1751 if (Attr.getNumArgs() != 0) {
1752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1753 return;
1754 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001755
Chris Lattner4225e232009-04-14 17:02:11 +00001756 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1757 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001759 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001760 return;
1761 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001762
Douglas Gregor35b57532009-10-27 21:01:01 +00001763 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001764 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001765 return;
1766 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001767
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001768 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001769}
1770
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001771static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1772 // check the attribute arguments.
1773 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001774 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001775 return;
1776 }
Eli Friedman7044b762009-03-27 21:06:47 +00001777
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001778 if (!isFunctionOrMethod(d)) {
1779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001780 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001781 return;
1782 }
Eli Friedman7044b762009-03-27 21:06:47 +00001783
1784 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1785 llvm::APSInt NumParams(32);
1786 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1787 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1788 << "regparm" << NumParamsExpr->getSourceRange();
1789 return;
1790 }
1791
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001792 if (S.Context.Target.getRegParmMax() == 0) {
1793 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001794 << NumParamsExpr->getSourceRange();
1795 return;
1796 }
1797
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001798 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001799 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1800 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001801 return;
1802 }
1803
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001804 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001805}
1806
Alexis Hunt96d5c762009-11-21 08:43:09 +00001807static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1808 // check the attribute arguments.
1809 if (Attr.getNumArgs() != 0) {
1810 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1811 return;
1812 }
1813
1814 if (!isa<CXXRecordDecl>(d)
1815 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1816 S.Diag(Attr.getLoc(),
1817 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1818 : diag::warn_attribute_wrong_decl_type)
1819 << Attr.getName() << 7 /*virtual method or class*/;
1820 return;
1821 }
Alexis Hunt54a02542009-11-25 04:20:27 +00001822
1823 // FIXME: Conform to C++0x redeclaration rules.
1824
1825 if (d->getAttr<FinalAttr>()) {
1826 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1827 return;
1828 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001829
1830 d->addAttr(::new (S.Context) FinalAttr());
1831}
1832
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001833//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00001834// C++0x member checking attributes
1835//===----------------------------------------------------------------------===//
1836
1837static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1838 if (Attr.getNumArgs() != 0) {
1839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1840 return;
1841 }
1842
1843 if (!isa<CXXRecordDecl>(d)) {
1844 S.Diag(Attr.getLoc(),
1845 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1846 : diag::warn_attribute_wrong_decl_type)
1847 << Attr.getName() << 9 /*class*/;
1848 return;
1849 }
1850
1851 if (d->getAttr<BaseCheckAttr>()) {
1852 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1853 return;
1854 }
1855
1856 d->addAttr(::new (S.Context) BaseCheckAttr());
1857}
1858
1859static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1860 if (Attr.getNumArgs() != 0) {
1861 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1862 return;
1863 }
1864
1865 if (!isa<RecordDecl>(d->getDeclContext())) {
1866 // FIXME: It's not the type that's the problem
1867 S.Diag(Attr.getLoc(),
1868 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1869 : diag::warn_attribute_wrong_decl_type)
1870 << Attr.getName() << 11 /*member*/;
1871 return;
1872 }
1873
1874 // FIXME: Conform to C++0x redeclaration rules.
1875
1876 if (d->getAttr<HidingAttr>()) {
1877 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1878 return;
1879 }
1880
1881 d->addAttr(::new (S.Context) HidingAttr());
1882}
1883
1884static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1885 if (Attr.getNumArgs() != 0) {
1886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1887 return;
1888 }
1889
1890 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1891 // FIXME: It's not the type that's the problem
1892 S.Diag(Attr.getLoc(),
1893 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1894 : diag::warn_attribute_wrong_decl_type)
1895 << Attr.getName() << 10 /*virtual method*/;
1896 return;
1897 }
1898
1899 // FIXME: Conform to C++0x redeclaration rules.
1900
1901 if (d->getAttr<OverrideAttr>()) {
1902 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1903 return;
1904 }
1905
1906 d->addAttr(::new (S.Context) OverrideAttr());
1907}
1908
1909//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001910// Checker-specific attribute handlers.
1911//===----------------------------------------------------------------------===//
1912
1913static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1914 Sema &S) {
1915
Ted Kremenek3b204e42009-05-13 21:07:32 +00001916 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001917
Ted Kremenek3b204e42009-05-13 21:07:32 +00001918 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1919 RetTy = MD->getResultType();
1920 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1921 RetTy = FD->getResultType();
1922 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001923 SourceLocation L = Attr.getLoc();
1924 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1925 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001926 return;
1927 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001928
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001929 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001930 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001931 SourceLocation L = Attr.getLoc();
1932 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1933 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001934 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001935 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001936
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001937 switch (Attr.getKind()) {
1938 default:
1939 assert(0 && "invalid ownership attribute");
1940 return;
1941 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001942 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001943 return;
1944 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001945 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001946 return;
1947 };
1948}
1949
1950//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001951// Top Level Sema Entry Points
1952//===----------------------------------------------------------------------===//
1953
Sebastian Redlfc24b632008-12-21 19:24:58 +00001954/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001955/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001956/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1957/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001958static void ProcessDeclAttribute(Scope *scope, Decl *D,
1959 const AttributeList &Attr, Sema &S) {
Eli Friedman53339e02009-06-08 23:27:34 +00001960 if (Attr.isDeclspecAttribute())
1961 // FIXME: Try to deal with __declspec attributes!
1962 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001963 switch (Attr.getKind()) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001964 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001965 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001966 case AttributeList::AT_objc_gc:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001967 // Ignore these, these are type attributes, handled by
1968 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001969 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001970 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1971 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001972 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001973 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001974 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001975 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001976 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1977 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001978 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001979 HandleDependencyAttr (D, Attr, S); break;
1980 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1981 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1982 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1983 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1984 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1985 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001986 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001987 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001988 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001989 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1990 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1991 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1992 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1993 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1994 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1995 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1996 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1997 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1998 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1999 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2000 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002001
2002 // Checker-specific.
2003 case AttributeList::AT_ns_returns_retained:
2004 case AttributeList::AT_cf_returns_retained:
2005 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2006
Nate Begemanf2758702009-06-26 06:32:41 +00002007 case AttributeList::AT_reqd_wg_size:
2008 HandleReqdWorkGroupSize(D, Attr, S); break;
2009
Alexis Hunt54a02542009-11-25 04:20:27 +00002010 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2011 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
2012 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
2013 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2014 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2015 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
2016 case AttributeList::AT_vector_size: HandleVectorSizeAttr (D, Attr, S); break;
2017 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002018 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2019 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002020 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
2021 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002022 case AttributeList::AT_transparent_union:
2023 HandleTransparentUnionAttr(D, Attr, S);
2024 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002025 case AttributeList::AT_objc_exception:
2026 HandleObjCExceptionAttr(D, Attr, S);
2027 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002028 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002029 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2030 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2031 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2032 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2033 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2034 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2035 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2036 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2037 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002038 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00002039 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00002040 // Just ignore
2041 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002042 default:
Anders Carlsson76187b42009-02-13 06:46:13 +00002043 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002044 break;
2045 }
2046}
2047
2048/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2049/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00002050void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002051 while (AttrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +00002052 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002053 AttrList = AttrList->getNext();
2054 }
2055}
2056
Ryan Flynn7d470f32009-07-30 03:15:39 +00002057/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2058/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002059NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002060 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002061 NamedDecl *NewD = 0;
2062 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2063 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2064 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002065 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002066 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2067 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2068 VD->getLocation(), II,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002069 VD->getType(), VD->getDeclaratorInfo(),
2070 VD->getStorageClass());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002071 }
2072 return NewD;
2073}
2074
2075/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2076/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002077void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002078 if (W.getUsed()) return; // only do this once
2079 W.setUsed(true);
2080 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2081 IdentifierInfo *NDId = ND->getIdentifier();
2082 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
2083 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2084 NewD->addAttr(::new (Context) WeakAttr());
2085 WeakTopLevelDecl.push_back(NewD);
2086 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2087 // to insert Decl at TU scope, sorry.
2088 DeclContext *SavedContext = CurContext;
2089 CurContext = Context.getTranslationUnitDecl();
2090 PushOnScopeChains(NewD, S);
2091 CurContext = SavedContext;
2092 } else { // just add weak to existing
2093 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002094 }
2095}
2096
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002097/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2098/// it, apply them to D. This is a bit tricky because PD can have attributes
2099/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002100void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002101 // Handle #pragma weak
2102 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2103 if (ND->hasLinkage()) {
2104 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2105 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002106 // Identifier referenced by #pragma weak before it was declared
2107 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002108 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2109 }
2110 }
2111 }
2112
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002113 // Apply decl attributes from the DeclSpec if present.
2114 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002115 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002116
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002117 // Walk the declarator structure, applying decl attributes that were in a type
2118 // position to the decl itself. This handles cases like:
2119 // int *__attr__(x)** D;
2120 // when X is a decl attribute.
2121 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2122 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002123 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002124
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002125 // Finally, apply any attributes on the decl itself.
2126 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002127 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002128}
John McCall28a6aea2009-11-04 02:18:39 +00002129
2130/// PushParsingDeclaration - Enter a new "scope" of deprecation
2131/// warnings.
2132///
2133/// The state token we use is the start index of this scope
2134/// on the warning stack.
2135Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2136 ParsingDeclDepth++;
2137 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2138}
2139
2140static bool isDeclDeprecated(Decl *D) {
2141 do {
2142 if (D->hasAttr<DeprecatedAttr>())
2143 return true;
2144 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2145 return false;
2146}
2147
2148void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2149 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2150 ParsingDeclDepth--;
2151
2152 if (DelayedDeprecationWarnings.empty())
2153 return;
2154
2155 unsigned SavedIndex = (unsigned) S;
2156 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2157 "saved index is out of bounds");
2158
2159 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2160 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2161 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2162 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2163 if (ND) {
2164 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2165
2166 // Prevent this from triggering multiple times.
2167 ND = 0;
2168 }
2169 }
2170 }
2171
2172 DelayedDeprecationWarnings.set_size(SavedIndex);
2173}
2174
2175void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2176 // Delay if we're currently parsing a declaration.
2177 if (ParsingDeclDepth) {
2178 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2179 return;
2180 }
2181
2182 // Otherwise, don't warn if our current context is deprecated.
2183 if (isDeclDeprecated(cast<Decl>(CurContext)))
2184 return;
2185
2186 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2187}