blob: 3fa7d9cddebdcd54890a4e71bc73b2856ad620f7 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Ted Kremeneka18d7d82009-08-14 20:49:40 +000027static const FunctionType *getFunctionType(const Decl *d,
28 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000030 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000031 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000032 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000033 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000034 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000035 Ty = decl->getUnderlyingType();
36 else
37 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000038
Chris Lattner6b6b5372008-06-26 18:38:35 +000039 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000040 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000041 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000042 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000043
John McCall183700f2009-09-21 23:43:11 +000044 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000045}
46
Daniel Dunbar35682492008-09-26 04:12:28 +000047// FIXME: We should provide an abstraction around a method or function
48// to provide the following bits of information.
49
Daniel Dunbard3f2c102008-10-19 02:04:16 +000050/// isFunctionOrMethod - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000051/// type (function or function-typed variable).
52static bool isFunction(const Decl *d) {
53 return getFunctionType(d, false) != NULL;
54}
55
56/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000057/// type (function or function-typed variable) or an Objective-C
58/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000059static bool isFunctionOrMethod(const Decl *d) {
60 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000061}
62
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000063/// isFunctionOrMethodOrBlock - Return true if the given decl has function
64/// type (function or function-typed variable) or an Objective-C
65/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000066static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000067 if (isFunctionOrMethod(d))
68 return true;
69 // check for block is more involved.
70 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
71 QualType Ty = V->getType();
72 return Ty->isBlockPointerType();
73 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000074 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000075}
76
Daniel Dunbard3f2c102008-10-19 02:04:16 +000077/// hasFunctionProto - Return true if the given decl has a argument
78/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000079/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000080static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000081 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000082 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000083 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000084 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000085 return true;
86 }
87}
88
89/// getFunctionOrMethodNumArgs - Return number of function or method
90/// arguments. It is an error to call this on a K&R function (use
91/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000092static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000093 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000094 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
96 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000097 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000098}
99
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000100static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000101 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000102 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000103 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
104 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000105
Chris Lattner89951a82009-02-20 18:43:26 +0000106 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000107}
108
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000109static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000110 if (const FunctionType *FnTy = getFunctionType(d))
111 return cast<FunctionProtoType>(FnTy)->getResultType();
112 return cast<ObjCMethodDecl>(d)->getResultType();
113}
114
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000115static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000116 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000118 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000119 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
120 return BD->IsVariadic();
121 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000122 return cast<ObjCMethodDecl>(d)->isVariadic();
123 }
124}
125
Chris Lattner6b6b5372008-06-26 18:38:35 +0000126static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000127 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000128 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000129 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000130
John McCall183700f2009-09-21 23:43:11 +0000131 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000132 if (!ClsT)
133 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000134
Chris Lattner6b6b5372008-06-26 18:38:35 +0000135 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000136
Chris Lattner6b6b5372008-06-26 18:38:35 +0000137 // FIXME: Should we walk the chain of classes?
138 return ClsName == &Ctx.Idents.get("NSString") ||
139 ClsName == &Ctx.Idents.get("NSMutableString");
140}
141
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000142static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000143 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000144 if (!PT)
145 return false;
146
Ted Kremenek6217b802009-07-29 21:53:49 +0000147 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000148 if (!RT)
149 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000150
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000151 const RecordDecl *RD = RT->getDecl();
152 if (RD->getTagKind() != TagDecl::TK_struct)
153 return false;
154
155 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
156}
157
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000158//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159// Attribute Implementations
160//===----------------------------------------------------------------------===//
161
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000162// FIXME: All this manual attribute parsing code is gross. At the
163// least add some helper functions to check most argument patterns (#
164// and types of args).
165
Mike Stumpbf916502009-07-24 19:02:52 +0000166static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000167 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000168 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
169 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000171 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 }
Mike Stumpbf916502009-07-24 19:02:52 +0000173
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000175
176 Expr *sizeExpr;
177
178 // Special case where the argument is a template id.
179 if (Attr.getParameterName()) {
180 sizeExpr = S.ActOnDeclarationNameExpr(scope, Attr.getLoc(),
181 Attr.getParameterName(),
182 false, 0, false).takeAs<Expr>();
183 } else {
184 // check the attribute arguments.
185 if (Attr.getNumArgs() != 1) {
186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
187 return;
188 }
189 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000190 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000191
192 // Instantiate/Install the vector type, and let Sema build the type for us.
193 // This will run the reguired checks.
194 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
195 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000196 // FIXME: preserve the old source info.
197 tDecl->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000198
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000199 // Remember this typedef decl, we will need it later for diagnostics.
200 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000201 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000202}
203
Chris Lattner065c5a82008-06-28 23:48:25 +0000204
Mike Stumpbf916502009-07-24 19:02:52 +0000205/// HandleVectorSizeAttribute - this attribute is only applicable to integral
206/// and float scalars, although arrays, pointers, and function return values are
207/// allowed in conjunction with this construct. Aggregates with this attribute
208/// are invalid, even if they are of the same size as a corresponding scalar.
209/// The raw attribute should contain precisely 1 argument, the vector size for
210/// the variable, measured in bytes. If curType and rawAttr are well formed,
211/// this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000212static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000213 QualType CurType;
214 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
215 CurType = VD->getType();
216 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
217 CurType = TD->getUnderlyingType();
218 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000219 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
220 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000221 return;
222 }
Mike Stumpbf916502009-07-24 19:02:52 +0000223
Chris Lattner065c5a82008-06-28 23:48:25 +0000224 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000225 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000226 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000227 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000228 }
Chris Lattner545dd342008-06-28 23:36:30 +0000229 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000230 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000231 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000232 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
233 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000234 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000235 }
Mike Stumpbf916502009-07-24 19:02:52 +0000236 // navigate to the base type - we need to provide for vector pointers, vector
237 // arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000238 if (CurType->isPointerType() || CurType->isArrayType() ||
239 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000240 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
241 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000242 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
243 do {
244 if (PointerType *PT = dyn_cast<PointerType>(canonType))
245 canonType = PT->getPointeeType().getTypePtr();
246 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
247 canonType = AT->getElementType().getTypePtr();
248 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
249 canonType = FT->getResultType().getTypePtr();
250 } while (canonType->isPointerType() || canonType->isArrayType() ||
251 canonType->isFunctionType());
252 */
253 }
Chris Lattner82afa2d2009-05-13 05:13:44 +0000254 // the base type must be integer or float, and can't already be a vector.
255 if (CurType->isVectorType() ||
256 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000257 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000258 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000259 }
Chris Lattner803d0802008-06-29 00:43:07 +0000260 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000261 // vecSize is specified in bytes - convert to bits.
Mike Stumpbf916502009-07-24 19:02:52 +0000262 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
263
Chris Lattner6b6b5372008-06-26 18:38:35 +0000264 // the vector size needs to be an integral multiple of the type size.
265 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000266 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
267 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000268 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000269 }
270 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000271 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
272 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000273 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000274 }
Mike Stumpbf916502009-07-24 19:02:52 +0000275
Chris Lattner065c5a82008-06-28 23:48:25 +0000276 // Success! Instantiate the vector type, the number of elements is > 0, and
277 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000278 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Mike Stumpbf916502009-07-24 19:02:52 +0000279
Chris Lattner065c5a82008-06-28 23:48:25 +0000280 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
281 VD->setType(CurType);
John McCallba6a9bd2009-10-24 08:00:42 +0000282 else {
283 // FIXME: preserve existing source info.
284 DeclaratorInfo *DInfo = S.Context.getTrivialDeclaratorInfo(CurType);
285 cast<TypedefDecl>(D)->setTypeDeclaratorInfo(DInfo);
286 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000287}
288
Chris Lattner803d0802008-06-29 00:43:07 +0000289static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000290 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000291 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000292 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000293 return;
294 }
Mike Stumpbf916502009-07-24 19:02:52 +0000295
Chris Lattner6b6b5372008-06-26 18:38:35 +0000296 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000297 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000298 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
299 // If the alignment is less than or equal to 8 bits, the packed attribute
300 // has no effect.
301 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000302 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000303 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000304 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000305 else
Anders Carlssona860e752009-08-08 18:23:56 +0000306 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000307 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000308 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000309}
310
Ted Kremenek96329d42008-07-15 22:26:48 +0000311static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
312 // check the attribute arguments.
313 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000314 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000315 return;
316 }
Mike Stumpbf916502009-07-24 19:02:52 +0000317
Ted Kremenek96329d42008-07-15 22:26:48 +0000318 // The IBOutlet attribute only applies to instance variables of Objective-C
319 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000320 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000321 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000322 else
Ted Kremenek32742602009-02-17 22:20:20 +0000323 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000324}
325
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000326static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000327 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
328 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000329 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000330 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000331 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332 return;
333 }
Mike Stumpbf916502009-07-24 19:02:52 +0000334
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000335 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336
337 // The nonnull attribute only applies to pointers.
338 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000339
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 for (AttributeList::arg_iterator I=Attr.arg_begin(),
341 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000342
343
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000344 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000345 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000346 llvm::APSInt ArgNum(32);
347 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000348 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
349 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000350 return;
351 }
Mike Stumpbf916502009-07-24 19:02:52 +0000352
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000353 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000354
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000355 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000356 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000357 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000358 return;
359 }
Mike Stumpbf916502009-07-24 19:02:52 +0000360
Ted Kremenek465172f2008-07-21 22:09:15 +0000361 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000362
363 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000364 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000365 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000366 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000367 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
368 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000369 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000370 }
Mike Stumpbf916502009-07-24 19:02:52 +0000371
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000372 NonNullArgs.push_back(x);
373 }
Mike Stumpbf916502009-07-24 19:02:52 +0000374
375 // If no arguments were specified to __attribute__((nonnull)) then all pointer
376 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000377 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000378 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
379 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000380 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000381 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000382 }
Mike Stumpbf916502009-07-24 19:02:52 +0000383
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000384 if (NonNullArgs.empty()) {
385 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
386 return;
387 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000388 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000389
390 unsigned* start = &NonNullArgs[0];
391 unsigned size = NonNullArgs.size();
392 std::sort(start, start + size);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000393 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000394}
395
Chris Lattner803d0802008-06-29 00:43:07 +0000396static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000397 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000398 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000399 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000400 return;
401 }
Mike Stumpbf916502009-07-24 19:02:52 +0000402
Chris Lattner545dd342008-06-28 23:36:30 +0000403 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000404 Arg = Arg->IgnoreParenCasts();
405 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000406
Chris Lattner6b6b5372008-06-26 18:38:35 +0000407 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000408 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000409 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000410 return;
411 }
Mike Stumpbf916502009-07-24 19:02:52 +0000412
Chris Lattner6b6b5372008-06-26 18:38:35 +0000413 const char *Alias = Str->getStrData();
414 unsigned AliasLen = Str->getByteLength();
Mike Stumpbf916502009-07-24 19:02:52 +0000415
Chris Lattner6b6b5372008-06-26 18:38:35 +0000416 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000417
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000418 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000419}
420
Mike Stumpbf916502009-07-24 19:02:52 +0000421static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000422 Sema &S) {
423 // check the attribute arguments.
424 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000426 return;
427 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000428
Chris Lattnerc5197432009-04-14 17:02:11 +0000429 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000431 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000432 return;
433 }
Mike Stumpbf916502009-07-24 19:02:52 +0000434
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000435 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000436}
437
Ryan Flynn76168e22009-08-09 20:07:29 +0000438static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
439 // check the attribute arguments.
440 if (Attr.getNumArgs() != 0) {
441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
442 return;
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000445 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000446 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000447 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
448 d->addAttr(::new (S.Context) MallocAttr());
449 return;
450 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000451 }
452
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000453 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000454}
455
Ted Kremenekb7252322009-04-10 00:01:14 +0000456static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000457 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000458 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000459 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000460 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000461 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000462 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000463
Mike Stump19c30c02009-04-29 19:03:13 +0000464 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
465 ValueDecl *VD = dyn_cast<ValueDecl>(d);
466 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000467 S.Diag(Attr.getLoc(),
468 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
469 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000470 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000471 return false;
472 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000473 }
Mike Stumpbf916502009-07-24 19:02:52 +0000474
Ted Kremenekb7252322009-04-10 00:01:14 +0000475 return true;
476}
477
478static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000479 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000480 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000481}
482
483static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
484 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000485 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000486 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000487}
488
Sean Huntbbd37c62009-11-21 08:43:09 +0000489static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
490 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
492 << Attr.getName() << 8; /*function, method, or parameter*/
493 return;
494 }
495 // FIXME: Actually store the attribute on the declaration
496}
497
Ted Kremenek73798892008-07-25 04:39:19 +0000498static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
499 // check the attribute arguments.
500 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000501 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000502 return;
503 }
Mike Stumpbf916502009-07-24 19:02:52 +0000504
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000505 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000506 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000507 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000508 return;
509 }
Mike Stumpbf916502009-07-24 19:02:52 +0000510
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000511 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000512}
513
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000514static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
515 // check the attribute arguments.
516 if (Attr.getNumArgs() != 0) {
517 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
518 return;
519 }
Mike Stumpbf916502009-07-24 19:02:52 +0000520
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000521 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000522 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000523 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
524 return;
525 }
526 } else if (!isFunctionOrMethod(d)) {
527 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000528 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000529 return;
530 }
Mike Stumpbf916502009-07-24 19:02:52 +0000531
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000532 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000533}
534
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000535static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
536 // check the attribute arguments.
537 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000538 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
539 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000540 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000541 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000542
543 int priority = 65535; // FIXME: Do not hardcode such constants.
544 if (Attr.getNumArgs() > 0) {
545 Expr *E = static_cast<Expr *>(Attr.getArg(0));
546 llvm::APSInt Idx(32);
547 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000549 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000550 return;
551 }
552 priority = Idx.getZExtValue();
553 }
Mike Stumpbf916502009-07-24 19:02:52 +0000554
Chris Lattnerc5197432009-04-14 17:02:11 +0000555 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000556 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000557 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000558 return;
559 }
560
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000561 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000562}
563
564static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
565 // check the attribute arguments.
566 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
568 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000569 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000570 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000571
572 int priority = 65535; // FIXME: Do not hardcode such constants.
573 if (Attr.getNumArgs() > 0) {
574 Expr *E = static_cast<Expr *>(Attr.getArg(0));
575 llvm::APSInt Idx(32);
576 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000577 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000578 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000579 return;
580 }
581 priority = Idx.getZExtValue();
582 }
Mike Stumpbf916502009-07-24 19:02:52 +0000583
Anders Carlsson6782fc62008-08-22 22:10:48 +0000584 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000585 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000586 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000587 return;
588 }
589
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000590 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000591}
592
Chris Lattner803d0802008-06-29 00:43:07 +0000593static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000594 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000595 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000597 return;
598 }
Mike Stumpbf916502009-07-24 19:02:52 +0000599
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000600 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000601}
602
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000603static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
604 // check the attribute arguments.
605 if (Attr.getNumArgs() != 0) {
606 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
607 return;
608 }
Mike Stumpbf916502009-07-24 19:02:52 +0000609
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000610 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000611}
612
Chris Lattner803d0802008-06-29 00:43:07 +0000613static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000614 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000615 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000617 return;
618 }
Mike Stumpbf916502009-07-24 19:02:52 +0000619
Chris Lattner545dd342008-06-28 23:36:30 +0000620 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000621 Arg = Arg->IgnoreParenCasts();
622 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000623
Chris Lattner6b6b5372008-06-26 18:38:35 +0000624 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000625 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000626 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000627 return;
628 }
Mike Stumpbf916502009-07-24 19:02:52 +0000629
Chris Lattner6b6b5372008-06-26 18:38:35 +0000630 const char *TypeStr = Str->getStrData();
631 unsigned TypeLen = Str->getByteLength();
632 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000633
Chris Lattner6b6b5372008-06-26 18:38:35 +0000634 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
635 type = VisibilityAttr::DefaultVisibility;
636 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
637 type = VisibilityAttr::HiddenVisibility;
638 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
639 type = VisibilityAttr::HiddenVisibility; // FIXME
640 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
641 type = VisibilityAttr::ProtectedVisibility;
642 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000643 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000644 return;
645 }
Mike Stumpbf916502009-07-24 19:02:52 +0000646
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000647 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000648}
649
Chris Lattner0db29ec2009-02-14 08:09:34 +0000650static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
651 Sema &S) {
652 if (Attr.getNumArgs() != 0) {
653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
654 return;
655 }
Mike Stumpbf916502009-07-24 19:02:52 +0000656
Chris Lattner0db29ec2009-02-14 08:09:34 +0000657 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
658 if (OCI == 0) {
659 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
660 return;
661 }
Mike Stumpbf916502009-07-24 19:02:52 +0000662
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000663 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000664}
665
666static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000667 if (Attr.getNumArgs() != 0) {
668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
669 return;
670 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000671 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000672 QualType T = TD->getUnderlyingType();
673 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000674 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000675 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
676 return;
677 }
678 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000679 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000680}
681
Mike Stumpbf916502009-07-24 19:02:52 +0000682static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000683HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
684 if (Attr.getNumArgs() != 0) {
685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
686 return;
687 }
688
689 if (!isa<FunctionDecl>(D)) {
690 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
691 return;
692 }
693
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000694 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000695}
696
Steve Naroff9eae5762008-09-18 16:44:58 +0000697static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000698 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000699 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000700 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000701 return;
702 }
Mike Stumpbf916502009-07-24 19:02:52 +0000703
Steve Naroff9eae5762008-09-18 16:44:58 +0000704 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000705 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000706 return;
707 }
Mike Stumpbf916502009-07-24 19:02:52 +0000708
Steve Naroff9eae5762008-09-18 16:44:58 +0000709 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000710 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000711 type = BlocksAttr::ByRef;
712 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000713 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000714 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000715 return;
716 }
Mike Stumpbf916502009-07-24 19:02:52 +0000717
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000718 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000719}
720
Anders Carlsson77091822008-10-05 18:05:59 +0000721static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
722 // check the attribute arguments.
723 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000724 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
725 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000726 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000727 }
728
Anders Carlsson77091822008-10-05 18:05:59 +0000729 int sentinel = 0;
730 if (Attr.getNumArgs() > 0) {
731 Expr *E = static_cast<Expr *>(Attr.getArg(0));
732 llvm::APSInt Idx(32);
733 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000734 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000735 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000736 return;
737 }
738 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000739
Anders Carlsson77091822008-10-05 18:05:59 +0000740 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000741 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
742 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000743 return;
744 }
745 }
746
747 int nullPos = 0;
748 if (Attr.getNumArgs() > 1) {
749 Expr *E = static_cast<Expr *>(Attr.getArg(1));
750 llvm::APSInt Idx(32);
751 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000753 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000754 return;
755 }
756 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000757
Anders Carlsson77091822008-10-05 18:05:59 +0000758 if (nullPos > 1 || nullPos < 0) {
759 // FIXME: This error message could be improved, it would be nice
760 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000761 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
762 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000763 return;
764 }
765 }
766
767 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000768 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000769 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000770
Chris Lattner897cd902009-03-17 23:03:47 +0000771 if (isa<FunctionNoProtoType>(FT)) {
772 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
773 return;
774 }
Mike Stumpbf916502009-07-24 19:02:52 +0000775
Chris Lattner897cd902009-03-17 23:03:47 +0000776 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000777 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000778 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000779 }
Anders Carlsson77091822008-10-05 18:05:59 +0000780 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
781 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000782 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000783 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000784 }
785 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000786 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
787 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000788 ;
789 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000790 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000791 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000792 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000793 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000794 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000795 int m = Ty->isFunctionPointerType() ? 0 : 1;
796 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000797 return;
798 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000799 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000800 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000801 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000802 return;
803 }
Anders Carlsson77091822008-10-05 18:05:59 +0000804 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000805 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000806 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000807 return;
808 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000809 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000810}
811
Chris Lattner026dc962009-02-14 07:37:35 +0000812static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
813 // check the attribute arguments.
814 if (Attr.getNumArgs() != 0) {
815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
816 return;
817 }
818
819 // TODO: could also be applied to methods?
820 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
821 if (!Fn) {
822 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000823 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000824 return;
825 }
Mike Stumpbf916502009-07-24 19:02:52 +0000826
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000827 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000828}
829
830static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000831 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000832 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000834 return;
835 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000836
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000837 /* weak only applies to non-static declarations */
838 bool isStatic = false;
839 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
840 isStatic = VD->getStorageClass() == VarDecl::Static;
841 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
842 isStatic = FD->getStorageClass() == FunctionDecl::Static;
843 }
844 if (isStatic) {
845 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
846 dyn_cast<NamedDecl>(D)->getNameAsString();
847 return;
848 }
849
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000850 // TODO: could also be applied to methods?
851 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
852 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000853 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000854 return;
855 }
Mike Stumpbf916502009-07-24 19:02:52 +0000856
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000857 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000858}
859
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000860static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
861 // check the attribute arguments.
862 if (Attr.getNumArgs() != 0) {
863 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
864 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000865 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000866
867 // weak_import only applies to variable & function declarations.
868 bool isDef = false;
869 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
870 isDef = (!VD->hasExternalStorage() || VD->getInit());
871 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000872 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000873 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
874 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000875 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000876 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000877 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000878 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000879 return;
880 }
881
882 // Merge should handle any subsequent violations.
883 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000884 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000885 diag::warn_attribute_weak_import_invalid_on_definition)
886 << "weak_import" << 2 /*variable and function*/;
887 return;
888 }
889
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000890 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000891}
892
Chris Lattner026dc962009-02-14 07:37:35 +0000893static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000894 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000895 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000897 return;
898 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000899
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000900 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000901 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000902 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000903 return;
904 }
905
Chris Lattner026dc962009-02-14 07:37:35 +0000906 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000907 if (!FD) {
908 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000909 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000910 return;
911 }
912
913 // Currently, the dllimport attribute is ignored for inlined functions.
914 // Warning is emitted.
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000915 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
917 return;
918 }
919
920 // The attribute is also overridden by a subsequent declaration as dllexport.
921 // Warning is emitted.
922 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
923 nextAttr = nextAttr->getNext()) {
924 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
925 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
926 return;
927 }
928 }
929
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000930 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000931 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
932 return;
933 }
934
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000935 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000936}
937
Chris Lattner026dc962009-02-14 07:37:35 +0000938static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000939 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000940 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000942 return;
943 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000944
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000945 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000946 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000947 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000948 return;
949 }
950
Chris Lattner026dc962009-02-14 07:37:35 +0000951 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000952 if (!FD) {
953 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000954 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000955 return;
956 }
957
Mike Stumpbf916502009-07-24 19:02:52 +0000958 // Currently, the dllexport attribute is ignored for inlined functions, unless
959 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000960 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000961 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
962 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
963 return;
964 }
965
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000966 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000967}
968
Nate Begeman6f3d8382009-06-26 06:32:41 +0000969static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
970 Sema &S) {
971 // Attribute has 3 arguments.
972 if (Attr.getNumArgs() != 3) {
973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
974 return;
975 }
976
977 unsigned WGSize[3];
978 for (unsigned i = 0; i < 3; ++i) {
979 Expr *E = static_cast<Expr *>(Attr.getArg(i));
980 llvm::APSInt ArgNum(32);
981 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
982 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
983 << "reqd_work_group_size" << E->getSourceRange();
984 return;
985 }
986 WGSize[i] = (unsigned) ArgNum.getZExtValue();
987 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000988 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000989 WGSize[2]));
990}
991
Chris Lattner026dc962009-02-14 07:37:35 +0000992static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000993 // Attribute has no arguments.
994 if (Attr.getNumArgs() != 1) {
995 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
996 return;
997 }
998
999 // Make sure that there is a string literal as the sections's single
1000 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001001 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1002 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001003 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001004 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001005 return;
1006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Chris Lattner797c3c42009-08-10 19:03:04 +00001008 std::string SectionStr(SE->getStrData(), SE->getByteLength());
1009
1010 // If the target wants to validate the section specifier, make it happen.
1011 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
1012 if (Error.empty()) {
1013 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
1014 return;
1015 }
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Chris Lattner797c3c42009-08-10 19:03:04 +00001017 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1018 << Error;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001020}
1021
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001022static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1023 // Attribute has no arguments.
1024 if (Attr.getNumArgs() != 0) {
1025 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1026 return;
1027 }
1028
1029 // Attribute can be applied only to functions.
1030 if (!isa<FunctionDecl>(d)) {
1031 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1032 << Attr.getName() << 0 /*function*/;
1033 return;
1034 }
1035
1036 // cdecl and fastcall attributes are mutually incompatible.
1037 if (d->getAttr<FastCallAttr>()) {
1038 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1039 << "cdecl" << "fastcall";
1040 return;
1041 }
1042
1043 // cdecl and stdcall attributes are mutually incompatible.
1044 if (d->getAttr<StdCallAttr>()) {
1045 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1046 << "cdecl" << "stdcall";
1047 return;
1048 }
1049
1050 d->addAttr(::new (S.Context) CDeclAttr());
1051}
1052
1053
Chris Lattner803d0802008-06-29 00:43:07 +00001054static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001055 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001056 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001057 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001058 return;
1059 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001060
1061 // Attribute can be applied only to functions.
1062 if (!isa<FunctionDecl>(d)) {
1063 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001064 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001065 return;
1066 }
1067
1068 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001069 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001070 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1071 << "stdcall" << "fastcall";
1072 return;
1073 }
1074
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001075 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001076}
1077
John McCall9112b932009-11-04 03:36:09 +00001078/// Diagnose the use of a non-standard calling convention on the given
1079/// function.
1080static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1081 SourceLocation Loc, Sema &S) {
1082 if (!D->hasPrototype()) {
1083 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1084 return;
1085 }
1086
1087 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1088 if (T->isVariadic()) {
1089 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1090 return;
1091 }
1092}
1093
Chris Lattner803d0802008-06-29 00:43:07 +00001094static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001095 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001096 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001097 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001098 return;
1099 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001100
1101 if (!isa<FunctionDecl>(d)) {
1102 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001103 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001104 return;
1105 }
1106
John McCall9112b932009-11-04 03:36:09 +00001107 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1108
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001109 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001110 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001111 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1112 << "fastcall" << "stdcall";
1113 return;
1114 }
1115
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001116 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001117}
1118
Chris Lattner803d0802008-06-29 00:43:07 +00001119static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001120 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001121 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001122 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001123 return;
1124 }
Mike Stumpbf916502009-07-24 19:02:52 +00001125
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001126 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001127}
1128
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001129static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1130 // check the attribute arguments.
1131 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001132 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001133 return;
1134 }
Mike Stumpbf916502009-07-24 19:02:52 +00001135
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001136 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001137}
1138
1139static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1140 // check the attribute arguments.
1141 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001142 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001143 return;
1144 }
Mike Stumpbf916502009-07-24 19:02:52 +00001145
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001146 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001147}
1148
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001149static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001150 // Match gcc which ignores cleanup attrs when compiling C++.
1151 if (S.getLangOptions().CPlusPlus)
1152 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001153
1154 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1156 return;
1157 }
Mike Stumpbf916502009-07-24 19:02:52 +00001158
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001159 if (Attr.getNumArgs() != 0) {
1160 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1161 return;
1162 }
Mike Stumpbf916502009-07-24 19:02:52 +00001163
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001164 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001165
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001166 if (!VD || !VD->hasLocalStorage()) {
1167 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1168 return;
1169 }
Mike Stumpbf916502009-07-24 19:02:52 +00001170
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001171 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001172 NamedDecl *CleanupDecl
1173 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1174 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001175 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001176 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001177 Attr.getParameterName();
1178 return;
1179 }
Mike Stumpbf916502009-07-24 19:02:52 +00001180
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001181 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1182 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001183 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001184 Attr.getParameterName();
1185 return;
1186 }
1187
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001188 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001190 Attr.getParameterName();
1191 return;
1192 }
Mike Stumpbf916502009-07-24 19:02:52 +00001193
Anders Carlsson89941c12009-02-07 23:16:50 +00001194 // We're currently more strict than GCC about what function types we accept.
1195 // If this ever proves to be a problem it should be easy to fix.
1196 QualType Ty = S.Context.getPointerType(VD->getType());
1197 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001198 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001199 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001200 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1201 Attr.getParameterName() << ParamTy << Ty;
1202 return;
1203 }
Mike Stumpbf916502009-07-24 19:02:52 +00001204
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001205 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001206}
1207
Mike Stumpbf916502009-07-24 19:02:52 +00001208/// Handle __attribute__((format_arg((idx)))) attribute based on
1209/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1210static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001211 if (Attr.getNumArgs() != 1) {
1212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1213 return;
1214 }
1215 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1216 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1217 << Attr.getName() << 0 /*function*/;
1218 return;
1219 }
Mike Stumpbf916502009-07-24 19:02:52 +00001220 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1221 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001222 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1223 unsigned FirstIdx = 1;
1224 // checks for the 2nd argument
1225 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1226 llvm::APSInt Idx(32);
1227 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1228 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1229 << "format" << 2 << IdxExpr->getSourceRange();
1230 return;
1231 }
Mike Stumpbf916502009-07-24 19:02:52 +00001232
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001233 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1235 << "format" << 2 << IdxExpr->getSourceRange();
1236 return;
1237 }
Mike Stumpbf916502009-07-24 19:02:52 +00001238
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001239 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001240
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001241 // make sure the format string is really a string
1242 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001243
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001244 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1245 if (not_nsstring_type &&
1246 !isCFStringType(Ty, S.Context) &&
1247 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001248 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001249 // FIXME: Should highlight the actual expression that has the wrong type.
1250 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001251 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001252 << IdxExpr->getSourceRange();
1253 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001254 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001255 Ty = getFunctionOrMethodResultType(d);
1256 if (!isNSStringType(Ty, S.Context) &&
1257 !isCFStringType(Ty, S.Context) &&
1258 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001259 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001260 // FIXME: Should highlight the actual expression that has the wrong type.
1261 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001262 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001263 << IdxExpr->getSourceRange();
1264 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001265 }
1266
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001267 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001268}
1269
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001270enum FormatAttrKind {
1271 CFStringFormat,
1272 NSStringFormat,
1273 StrftimeFormat,
1274 SupportedFormat,
1275 InvalidFormat
1276};
1277
1278/// getFormatAttrKind - Map from format attribute names to supported format
1279/// types.
1280static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1281 // Check for formats that get handled specially.
1282 if (Format == "NSString")
1283 return NSStringFormat;
1284 if (Format == "CFString")
1285 return CFStringFormat;
1286 if (Format == "strftime")
1287 return StrftimeFormat;
1288
1289 // Otherwise, check for supported formats.
1290 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1291 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1292 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1293 Format == "zcmn_err")
1294 return SupportedFormat;
1295
1296 return InvalidFormat;
1297}
1298
Mike Stumpbf916502009-07-24 19:02:52 +00001299/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1300/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001301static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001302
Chris Lattner545dd342008-06-28 23:36:30 +00001303 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001305 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001306 return;
1307 }
1308
Chris Lattner545dd342008-06-28 23:36:30 +00001309 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001311 return;
1312 }
1313
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001314 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001315 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001316 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001317 return;
1318 }
1319
Daniel Dunbar35682492008-09-26 04:12:28 +00001320 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001321 unsigned FirstIdx = 1;
1322
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001323 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001324
1325 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001326 if (Format.startswith("__") && Format.endswith("__"))
1327 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001328
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001329 // Check for supported formats.
1330 FormatAttrKind Kind = getFormatAttrKind(Format);
1331 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001332 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001333 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001334 return;
1335 }
1336
1337 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001338 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001339 llvm::APSInt Idx(32);
1340 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001341 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001342 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001343 return;
1344 }
1345
Anders Carlsson4fb77202009-08-25 14:12:34 +00001346 // FIXME: We should handle the implicit 'this' parameter in a more generic
1347 // way that can be used for other arguments.
1348 bool HasImplicitThisParam = false;
1349 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1350 if (MD->isInstance()) {
1351 HasImplicitThisParam = true;
1352 NumArgs++;
1353 }
1354 }
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Chris Lattner6b6b5372008-06-26 18:38:35 +00001356 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001358 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001359 return;
1360 }
1361
1362 // FIXME: Do we need to bounds check?
1363 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001364
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001365 if (HasImplicitThisParam) {
1366 if (ArgIdx == 0) {
1367 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1368 << "a string type" << IdxExpr->getSourceRange();
1369 return;
1370 }
1371 ArgIdx--;
1372 }
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Chris Lattner6b6b5372008-06-26 18:38:35 +00001374 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001375 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001376
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001377 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001378 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001379 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1380 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001381 return;
1382 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001383 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001384 // FIXME: do we need to check if the type is NSString*? What are the
1385 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001386 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001387 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001388 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1389 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001390 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001391 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001392 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001393 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001394 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001395 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1396 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001397 return;
1398 }
1399
1400 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001401 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001402 llvm::APSInt FirstArg(32);
1403 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001404 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001405 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001406 return;
1407 }
1408
1409 // check if the function is variadic if the 3rd argument non-zero
1410 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001411 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001412 ++NumArgs; // +1 for ...
1413 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001414 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001415 return;
1416 }
1417 }
1418
Chris Lattner3c73c412008-11-19 08:23:25 +00001419 // strftime requires FirstArg to be 0 because it doesn't read from any
1420 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001421 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001422 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001423 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1424 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425 return;
1426 }
1427 // if 0 it disables parameter checking (to use with e.g. va_list)
1428 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001430 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001431 return;
1432 }
1433
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001434 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1435 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001436}
1437
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001438static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1439 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001440 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001441 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001442 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001443 return;
1444 }
1445
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001446 // Try to find the underlying union declaration.
1447 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001448 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001449 if (TD && TD->getUnderlyingType()->isUnionType())
1450 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1451 else
1452 RD = dyn_cast<RecordDecl>(d);
1453
1454 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001455 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001456 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001457 return;
1458 }
1459
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001460 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001461 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001462 diag::warn_transparent_union_attribute_not_definition);
1463 return;
1464 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001465
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001466 RecordDecl::field_iterator Field = RD->field_begin(),
1467 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001468 if (Field == FieldEnd) {
1469 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1470 return;
1471 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001472
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001473 FieldDecl *FirstField = *Field;
1474 QualType FirstType = FirstField->getType();
1475 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001476 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001477 diag::warn_transparent_union_attribute_floating);
1478 return;
1479 }
1480
1481 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1482 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1483 for (; Field != FieldEnd; ++Field) {
1484 QualType FieldType = Field->getType();
1485 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1486 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1487 // Warn if we drop the attribute.
1488 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001489 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001490 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001491 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001492 diag::warn_transparent_union_attribute_field_size_align)
1493 << isSize << Field->getDeclName() << FieldBits;
1494 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001495 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001496 diag::note_transparent_union_first_field_size_align)
1497 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001498 return;
1499 }
1500 }
1501
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001502 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001503}
1504
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001505static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001506 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001507 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001508 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001509 return;
1510 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001511 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1512 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001513
Chris Lattner6b6b5372008-06-26 18:38:35 +00001514 // Make sure that there is a string literal as the annotation's single
1515 // argument.
1516 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001517 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001518 return;
1519 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001520 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001521 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001522}
1523
Chris Lattner803d0802008-06-29 00:43:07 +00001524static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001525 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001526 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001528 return;
1529 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001530
1531 //FIXME: The C++0x version of this attribute has more limited applicabilty
1532 // than GNU's, and should error out when it is used to specify a
1533 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001534
1535 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001536 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001537 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001538 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001539 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001540 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001541 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001542 }
Mike Stumpbf916502009-07-24 19:02:52 +00001543
Chris Lattner49e2d342008-06-28 23:50:44 +00001544 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1545 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001546 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001547 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1548 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001549 return;
1550 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001551 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001552 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001553 << alignmentExpr->getSourceRange();
1554 return;
1555 }
1556
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001557 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001558}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001559
Mike Stumpbf916502009-07-24 19:02:52 +00001560/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1561/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001562///
Mike Stumpbf916502009-07-24 19:02:52 +00001563/// Despite what would be logical, the mode attribute is a decl attribute, not a
1564/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1565/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001566static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001567 // This attribute isn't documented, but glibc uses it. It changes
1568 // the width of an int or unsigned int to the specified size.
1569
1570 // Check that there aren't any arguments
1571 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001573 return;
1574 }
1575
1576 IdentifierInfo *Name = Attr.getParameterName();
1577 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001578 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001579 return;
1580 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001581
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001582 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001583
1584 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001585 if (Str.startswith("__") && Str.endswith("__"))
1586 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587
1588 unsigned DestWidth = 0;
1589 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001590 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001591 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001592 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001593 switch (Str[0]) {
1594 case 'Q': DestWidth = 8; break;
1595 case 'H': DestWidth = 16; break;
1596 case 'S': DestWidth = 32; break;
1597 case 'D': DestWidth = 64; break;
1598 case 'X': DestWidth = 96; break;
1599 case 'T': DestWidth = 128; break;
1600 }
1601 if (Str[1] == 'F') {
1602 IntegerMode = false;
1603 } else if (Str[1] == 'C') {
1604 IntegerMode = false;
1605 ComplexMode = true;
1606 } else if (Str[1] != 'I') {
1607 DestWidth = 0;
1608 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001609 break;
1610 case 4:
1611 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1612 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001613 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001614 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001615 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001616 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001617 break;
1618 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001619 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001620 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001621 break;
1622 }
1623
1624 QualType OldTy;
1625 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1626 OldTy = TD->getUnderlyingType();
1627 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1628 OldTy = VD->getType();
1629 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001630 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1631 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001632 return;
1633 }
Eli Friedman73397492009-03-03 06:41:03 +00001634
John McCall183700f2009-09-21 23:43:11 +00001635 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001636 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1637 else if (IntegerMode) {
1638 if (!OldTy->isIntegralType())
1639 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1640 } else if (ComplexMode) {
1641 if (!OldTy->isComplexType())
1642 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1643 } else {
1644 if (!OldTy->isFloatingType())
1645 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1646 }
1647
Mike Stump390b4cc2009-05-16 07:39:55 +00001648 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1649 // and friends, at least with glibc.
1650 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1651 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001652 // FIXME: Make sure floating-point mappings are accurate
1653 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001654 QualType NewTy;
1655 switch (DestWidth) {
1656 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001657 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001658 return;
1659 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001660 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001661 return;
1662 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001663 if (!IntegerMode) {
1664 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1665 return;
1666 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001667 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001668 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001669 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001670 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001671 break;
1672 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001673 if (!IntegerMode) {
1674 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1675 return;
1676 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001677 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001678 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001679 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001680 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001681 break;
1682 case 32:
1683 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001684 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001685 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001686 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001687 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001688 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001689 break;
1690 case 64:
1691 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001692 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001693 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001694 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001695 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001696 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001697 break;
Eli Friedman73397492009-03-03 06:41:03 +00001698 case 96:
1699 NewTy = S.Context.LongDoubleTy;
1700 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001701 case 128:
1702 if (!IntegerMode) {
1703 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1704 return;
1705 }
1706 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001707 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001708 }
1709
Eli Friedman73397492009-03-03 06:41:03 +00001710 if (ComplexMode) {
1711 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001712 }
1713
1714 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001715 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1716 // FIXME: preserve existing source info.
1717 TD->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(NewTy));
1718 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001719 cast<ValueDecl>(D)->setType(NewTy);
1720}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001721
Mike Stump1feade82009-08-26 22:31:08 +00001722static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001723 // check the attribute arguments.
1724 if (Attr.getNumArgs() > 0) {
1725 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1726 return;
1727 }
Anders Carlssone896d982009-02-13 08:11:52 +00001728
Anders Carlsson5bab7882009-02-19 19:16:48 +00001729 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001730 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001731 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001732 return;
1733 }
Mike Stumpbf916502009-07-24 19:02:52 +00001734
Mike Stump1feade82009-08-26 22:31:08 +00001735 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001736}
1737
Mike Stump1feade82009-08-26 22:31:08 +00001738static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001739 // check the attribute arguments.
1740 if (Attr.getNumArgs() != 0) {
1741 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1742 return;
1743 }
Mike Stumpbf916502009-07-24 19:02:52 +00001744
Chris Lattnerc5197432009-04-14 17:02:11 +00001745 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001746 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001747 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001748 return;
1749 }
Mike Stumpbf916502009-07-24 19:02:52 +00001750
Mike Stump1feade82009-08-26 22:31:08 +00001751 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001752}
1753
Chris Lattnercf2a7212009-04-20 19:12:28 +00001754static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001755 // check the attribute arguments.
1756 if (Attr.getNumArgs() != 0) {
1757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1758 return;
1759 }
Mike Stumpbf916502009-07-24 19:02:52 +00001760
Chris Lattnerc5197432009-04-14 17:02:11 +00001761 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1762 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001763 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001764 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001765 return;
1766 }
Mike Stumpbf916502009-07-24 19:02:52 +00001767
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001768 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001769 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001770 return;
1771 }
Mike Stumpbf916502009-07-24 19:02:52 +00001772
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001773 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001774}
1775
Fariborz Jahanianee760332009-03-27 18:38:55 +00001776static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1777 // check the attribute arguments.
1778 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001779 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001780 return;
1781 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001782
Fariborz Jahanianee760332009-03-27 18:38:55 +00001783 if (!isFunctionOrMethod(d)) {
1784 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001785 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001786 return;
1787 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001788
1789 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1790 llvm::APSInt NumParams(32);
1791 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1792 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1793 << "regparm" << NumParamsExpr->getSourceRange();
1794 return;
1795 }
1796
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001797 if (S.Context.Target.getRegParmMax() == 0) {
1798 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001799 << NumParamsExpr->getSourceRange();
1800 return;
1801 }
1802
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001803 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001804 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1805 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001806 return;
1807 }
1808
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001809 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001810}
1811
Sean Huntbbd37c62009-11-21 08:43:09 +00001812static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1813 // check the attribute arguments.
1814 if (Attr.getNumArgs() != 0) {
1815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1816 return;
1817 }
1818
1819 if (!isa<CXXRecordDecl>(d)
1820 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1821 S.Diag(Attr.getLoc(),
1822 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1823 : diag::warn_attribute_wrong_decl_type)
1824 << Attr.getName() << 7 /*virtual method or class*/;
1825 return;
1826 }
1827
1828 // FIXME: Check that it's not specified more than once in an attribute-
1829 // specifier and that it conforms to the C++0x rules for
1830 // redeclarations.
1831
1832 d->addAttr(::new (S.Context) FinalAttr());
1833}
1834
Chris Lattner0744e5f2008-06-29 00:23:49 +00001835//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001836// Checker-specific attribute handlers.
1837//===----------------------------------------------------------------------===//
1838
1839static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1840 Sema &S) {
1841
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001842 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001843
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001844 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1845 RetTy = MD->getResultType();
1846 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1847 RetTy = FD->getResultType();
1848 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001849 SourceLocation L = Attr.getLoc();
1850 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1851 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001852 return;
1853 }
Mike Stumpbf916502009-07-24 19:02:52 +00001854
Ted Kremenek6217b802009-07-29 21:53:49 +00001855 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001856 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001857 SourceLocation L = Attr.getLoc();
1858 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1859 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001860 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001861 }
Mike Stumpbf916502009-07-24 19:02:52 +00001862
Ted Kremenekb71368d2009-05-09 02:44:38 +00001863 switch (Attr.getKind()) {
1864 default:
1865 assert(0 && "invalid ownership attribute");
1866 return;
1867 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001868 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001869 return;
1870 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001871 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001872 return;
1873 };
1874}
1875
1876//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001877// Top Level Sema Entry Points
1878//===----------------------------------------------------------------------===//
1879
Sebastian Redla89d82c2008-12-21 19:24:58 +00001880/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001881/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001882/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1883/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001884static void ProcessDeclAttribute(Scope *scope, Decl *D,
1885 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001886 if (Attr.isDeclspecAttribute())
1887 // FIXME: Try to deal with __declspec attributes!
1888 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001889 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001890 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001891 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001892 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001893 // Ignore these, these are type attributes, handled by
1894 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001895 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001896 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1897 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001898 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001899 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001900 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001901 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001902 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001903 case AttributeList::AT_carries_dependency:
1904 HandleDependencyAttr (D, Attr, S); break;
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001905 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001906 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1907 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1908 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1909 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1910 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001911 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001912 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001913 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001914 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001915 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001916 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001917 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001918 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001919 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001920 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001921 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1922 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1923 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001924
1925 // Checker-specific.
1926 case AttributeList::AT_ns_returns_retained:
1927 case AttributeList::AT_cf_returns_retained:
1928 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1929
Nate Begeman6f3d8382009-06-26 06:32:41 +00001930 case AttributeList::AT_reqd_wg_size:
1931 HandleReqdWorkGroupSize(D, Attr, S); break;
1932
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001933 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001934 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001935 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001936 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001937 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001938 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001939 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001940 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001941 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1942 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001943 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001944 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001945 case AttributeList::AT_transparent_union:
1946 HandleTransparentUnionAttr(D, Attr, S);
1947 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001948 case AttributeList::AT_objc_exception:
1949 HandleObjCExceptionAttr(D, Attr, S);
1950 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001951 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001952 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001953 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001954 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001955 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1956 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001957 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Mike Stump1feade82009-08-26 22:31:08 +00001958 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1959 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001960 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001961 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001962 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001963 // Just ignore
1964 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001965 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001966 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001967 break;
1968 }
1969}
1970
1971/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1972/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001973void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001974 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001975 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001976 AttrList = AttrList->getNext();
1977 }
1978}
1979
Ryan Flynne25ff832009-07-30 03:15:39 +00001980/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1981/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001982NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001983 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001984 NamedDecl *NewD = 0;
1985 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1986 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1987 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001988 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001989 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1990 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1991 VD->getLocation(), II,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001992 VD->getType(), VD->getDeclaratorInfo(),
1993 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001994 }
1995 return NewD;
1996}
1997
1998/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1999/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002000void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002001 if (W.getUsed()) return; // only do this once
2002 W.setUsed(true);
2003 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2004 IdentifierInfo *NDId = ND->getIdentifier();
2005 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
2006 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2007 NewD->addAttr(::new (Context) WeakAttr());
2008 WeakTopLevelDecl.push_back(NewD);
2009 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2010 // to insert Decl at TU scope, sorry.
2011 DeclContext *SavedContext = CurContext;
2012 CurContext = Context.getTranslationUnitDecl();
2013 PushOnScopeChains(NewD, S);
2014 CurContext = SavedContext;
2015 } else { // just add weak to existing
2016 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002017 }
2018}
2019
Chris Lattner0744e5f2008-06-29 00:23:49 +00002020/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2021/// it, apply them to D. This is a bit tricky because PD can have attributes
2022/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002023void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002024 // Handle #pragma weak
2025 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2026 if (ND->hasLinkage()) {
2027 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2028 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002029 // Identifier referenced by #pragma weak before it was declared
2030 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002031 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2032 }
2033 }
2034 }
2035
Chris Lattner0744e5f2008-06-29 00:23:49 +00002036 // Apply decl attributes from the DeclSpec if present.
2037 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002038 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002039
Chris Lattner0744e5f2008-06-29 00:23:49 +00002040 // Walk the declarator structure, applying decl attributes that were in a type
2041 // position to the decl itself. This handles cases like:
2042 // int *__attr__(x)** D;
2043 // when X is a decl attribute.
2044 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2045 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002046 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002047
Chris Lattner0744e5f2008-06-29 00:23:49 +00002048 // Finally, apply any attributes on the decl itself.
2049 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002050 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002051}
John McCall54abf7d2009-11-04 02:18:39 +00002052
2053/// PushParsingDeclaration - Enter a new "scope" of deprecation
2054/// warnings.
2055///
2056/// The state token we use is the start index of this scope
2057/// on the warning stack.
2058Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2059 ParsingDeclDepth++;
2060 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2061}
2062
2063static bool isDeclDeprecated(Decl *D) {
2064 do {
2065 if (D->hasAttr<DeprecatedAttr>())
2066 return true;
2067 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2068 return false;
2069}
2070
2071void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2072 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2073 ParsingDeclDepth--;
2074
2075 if (DelayedDeprecationWarnings.empty())
2076 return;
2077
2078 unsigned SavedIndex = (unsigned) S;
2079 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2080 "saved index is out of bounds");
2081
2082 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2083 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2084 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2085 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2086 if (ND) {
2087 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2088
2089 // Prevent this from triggering multiple times.
2090 ND = 0;
2091 }
2092 }
2093 }
2094
2095 DelayedDeprecationWarnings.set_size(SavedIndex);
2096}
2097
2098void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2099 // Delay if we're currently parsing a declaration.
2100 if (ParsingDeclDepth) {
2101 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2102 return;
2103 }
2104
2105 // Otherwise, don't warn if our current context is deprecated.
2106 if (isDeclDeprecated(cast<Decl>(CurContext)))
2107 return;
2108
2109 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2110}