blob: 803be138c2626a9d4f21334b53f84f2780f799e1 [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()) {
467 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000468 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000469 return false;
470 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000471 }
Mike Stumpbf916502009-07-24 19:02:52 +0000472
Ted Kremenekb7252322009-04-10 00:01:14 +0000473 return true;
474}
475
476static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000477 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000478 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000479}
480
481static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
482 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000483 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000484 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000485}
486
Ted Kremenek73798892008-07-25 04:39:19 +0000487static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
488 // check the attribute arguments.
489 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000490 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000491 return;
492 }
Mike Stumpbf916502009-07-24 19:02:52 +0000493
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000494 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000495 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000496 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000497 return;
498 }
Mike Stumpbf916502009-07-24 19:02:52 +0000499
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000500 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000501}
502
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000503static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
504 // check the attribute arguments.
505 if (Attr.getNumArgs() != 0) {
506 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
507 return;
508 }
Mike Stumpbf916502009-07-24 19:02:52 +0000509
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000510 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000511 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000512 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
513 return;
514 }
515 } else if (!isFunctionOrMethod(d)) {
516 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000517 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000518 return;
519 }
Mike Stumpbf916502009-07-24 19:02:52 +0000520
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000521 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000522}
523
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000524static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
525 // check the attribute arguments.
526 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
528 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000529 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000530 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000531
532 int priority = 65535; // FIXME: Do not hardcode such constants.
533 if (Attr.getNumArgs() > 0) {
534 Expr *E = static_cast<Expr *>(Attr.getArg(0));
535 llvm::APSInt Idx(32);
536 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000537 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000538 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000539 return;
540 }
541 priority = Idx.getZExtValue();
542 }
Mike Stumpbf916502009-07-24 19:02:52 +0000543
Chris Lattnerc5197432009-04-14 17:02:11 +0000544 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000545 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000546 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000547 return;
548 }
549
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000550 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000551}
552
553static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
554 // check the attribute arguments.
555 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000556 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
557 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000558 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000559 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000560
561 int priority = 65535; // FIXME: Do not hardcode such constants.
562 if (Attr.getNumArgs() > 0) {
563 Expr *E = static_cast<Expr *>(Attr.getArg(0));
564 llvm::APSInt Idx(32);
565 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000566 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000567 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000568 return;
569 }
570 priority = Idx.getZExtValue();
571 }
Mike Stumpbf916502009-07-24 19:02:52 +0000572
Anders Carlsson6782fc62008-08-22 22:10:48 +0000573 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000574 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000575 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000576 return;
577 }
578
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000579 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000580}
581
Chris Lattner803d0802008-06-29 00:43:07 +0000582static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000583 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000584 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000586 return;
587 }
Mike Stumpbf916502009-07-24 19:02:52 +0000588
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000589 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000590}
591
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000592static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
593 // check the attribute arguments.
594 if (Attr.getNumArgs() != 0) {
595 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
596 return;
597 }
Mike Stumpbf916502009-07-24 19:02:52 +0000598
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000599 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000600}
601
Chris Lattner803d0802008-06-29 00:43:07 +0000602static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000603 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000604 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000605 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 return;
607 }
Mike Stumpbf916502009-07-24 19:02:52 +0000608
Chris Lattner545dd342008-06-28 23:36:30 +0000609 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000610 Arg = Arg->IgnoreParenCasts();
611 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000612
Chris Lattner6b6b5372008-06-26 18:38:35 +0000613 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000614 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000615 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000616 return;
617 }
Mike Stumpbf916502009-07-24 19:02:52 +0000618
Chris Lattner6b6b5372008-06-26 18:38:35 +0000619 const char *TypeStr = Str->getStrData();
620 unsigned TypeLen = Str->getByteLength();
621 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Chris Lattner6b6b5372008-06-26 18:38:35 +0000623 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
624 type = VisibilityAttr::DefaultVisibility;
625 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
626 type = VisibilityAttr::HiddenVisibility;
627 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
628 type = VisibilityAttr::HiddenVisibility; // FIXME
629 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
630 type = VisibilityAttr::ProtectedVisibility;
631 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000632 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000633 return;
634 }
Mike Stumpbf916502009-07-24 19:02:52 +0000635
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000636 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000637}
638
Chris Lattner0db29ec2009-02-14 08:09:34 +0000639static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
640 Sema &S) {
641 if (Attr.getNumArgs() != 0) {
642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
643 return;
644 }
Mike Stumpbf916502009-07-24 19:02:52 +0000645
Chris Lattner0db29ec2009-02-14 08:09:34 +0000646 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
647 if (OCI == 0) {
648 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
649 return;
650 }
Mike Stumpbf916502009-07-24 19:02:52 +0000651
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000652 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000653}
654
655static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000656 if (Attr.getNumArgs() != 0) {
657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
658 return;
659 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000660 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000661 QualType T = TD->getUnderlyingType();
662 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000663 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000664 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
665 return;
666 }
667 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000668 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000669}
670
Mike Stumpbf916502009-07-24 19:02:52 +0000671static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000672HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
673 if (Attr.getNumArgs() != 0) {
674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
675 return;
676 }
677
678 if (!isa<FunctionDecl>(D)) {
679 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
680 return;
681 }
682
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000683 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000684}
685
Steve Naroff9eae5762008-09-18 16:44:58 +0000686static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000687 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000688 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000689 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000690 return;
691 }
Mike Stumpbf916502009-07-24 19:02:52 +0000692
Steve Naroff9eae5762008-09-18 16:44:58 +0000693 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000694 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000695 return;
696 }
Mike Stumpbf916502009-07-24 19:02:52 +0000697
Steve Naroff9eae5762008-09-18 16:44:58 +0000698 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000699 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000700 type = BlocksAttr::ByRef;
701 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000702 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000703 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000704 return;
705 }
Mike Stumpbf916502009-07-24 19:02:52 +0000706
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000707 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000708}
709
Anders Carlsson77091822008-10-05 18:05:59 +0000710static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
711 // check the attribute arguments.
712 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000713 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
714 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000715 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000716 }
717
Anders Carlsson77091822008-10-05 18:05:59 +0000718 int sentinel = 0;
719 if (Attr.getNumArgs() > 0) {
720 Expr *E = static_cast<Expr *>(Attr.getArg(0));
721 llvm::APSInt Idx(32);
722 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000723 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000724 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000725 return;
726 }
727 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000728
Anders Carlsson77091822008-10-05 18:05:59 +0000729 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000730 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
731 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000732 return;
733 }
734 }
735
736 int nullPos = 0;
737 if (Attr.getNumArgs() > 1) {
738 Expr *E = static_cast<Expr *>(Attr.getArg(1));
739 llvm::APSInt Idx(32);
740 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000741 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000742 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000743 return;
744 }
745 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000746
Anders Carlsson77091822008-10-05 18:05:59 +0000747 if (nullPos > 1 || nullPos < 0) {
748 // FIXME: This error message could be improved, it would be nice
749 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000750 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
751 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000752 return;
753 }
754 }
755
756 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000757 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000758 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000759
Chris Lattner897cd902009-03-17 23:03:47 +0000760 if (isa<FunctionNoProtoType>(FT)) {
761 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
762 return;
763 }
Mike Stumpbf916502009-07-24 19:02:52 +0000764
Chris Lattner897cd902009-03-17 23:03:47 +0000765 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000766 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000767 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000768 }
Anders Carlsson77091822008-10-05 18:05:59 +0000769 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
770 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000771 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000772 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000773 }
774 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000775 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
776 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000777 ;
778 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000779 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000780 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000781 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000782 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000783 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000784 int m = Ty->isFunctionPointerType() ? 0 : 1;
785 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000786 return;
787 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000788 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000790 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000791 return;
792 }
Anders Carlsson77091822008-10-05 18:05:59 +0000793 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000794 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000795 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000796 return;
797 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000798 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000799}
800
Chris Lattner026dc962009-02-14 07:37:35 +0000801static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
802 // check the attribute arguments.
803 if (Attr.getNumArgs() != 0) {
804 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
805 return;
806 }
807
808 // TODO: could also be applied to methods?
809 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
810 if (!Fn) {
811 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000812 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000813 return;
814 }
Mike Stumpbf916502009-07-24 19:02:52 +0000815
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000816 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000817}
818
819static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000820 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000821 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000822 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000823 return;
824 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000825
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000826 /* weak only applies to non-static declarations */
827 bool isStatic = false;
828 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
829 isStatic = VD->getStorageClass() == VarDecl::Static;
830 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
831 isStatic = FD->getStorageClass() == FunctionDecl::Static;
832 }
833 if (isStatic) {
834 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
835 dyn_cast<NamedDecl>(D)->getNameAsString();
836 return;
837 }
838
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000839 // TODO: could also be applied to methods?
840 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
841 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000842 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000843 return;
844 }
Mike Stumpbf916502009-07-24 19:02:52 +0000845
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000846 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000847}
848
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000849static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
850 // check the attribute arguments.
851 if (Attr.getNumArgs() != 0) {
852 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
853 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000854 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000855
856 // weak_import only applies to variable & function declarations.
857 bool isDef = false;
858 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
859 isDef = (!VD->hasExternalStorage() || VD->getInit());
860 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000861 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000862 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
863 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000864 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000865 } else {
866 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000867 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000868 return;
869 }
870
871 // Merge should handle any subsequent violations.
872 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000873 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000874 diag::warn_attribute_weak_import_invalid_on_definition)
875 << "weak_import" << 2 /*variable and function*/;
876 return;
877 }
878
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000879 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000880}
881
Chris Lattner026dc962009-02-14 07:37:35 +0000882static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000883 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000884 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000885 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000886 return;
887 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000888
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000889 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000890 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000891 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000892 return;
893 }
894
Chris Lattner026dc962009-02-14 07:37:35 +0000895 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000896 if (!FD) {
897 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000898 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000899 return;
900 }
901
902 // Currently, the dllimport attribute is ignored for inlined functions.
903 // Warning is emitted.
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000904 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000905 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
906 return;
907 }
908
909 // The attribute is also overridden by a subsequent declaration as dllexport.
910 // Warning is emitted.
911 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
912 nextAttr = nextAttr->getNext()) {
913 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
914 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
915 return;
916 }
917 }
918
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000919 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000920 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
921 return;
922 }
923
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000924 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000925}
926
Chris Lattner026dc962009-02-14 07:37:35 +0000927static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000928 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000929 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000930 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000931 return;
932 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000933
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000934 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000935 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000936 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000937 return;
938 }
939
Chris Lattner026dc962009-02-14 07:37:35 +0000940 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000941 if (!FD) {
942 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000943 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000944 return;
945 }
946
Mike Stumpbf916502009-07-24 19:02:52 +0000947 // Currently, the dllexport attribute is ignored for inlined functions, unless
948 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000949 if (FD->isInlineSpecified()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000950 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
951 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
952 return;
953 }
954
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000955 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000956}
957
Nate Begeman6f3d8382009-06-26 06:32:41 +0000958static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
959 Sema &S) {
960 // Attribute has 3 arguments.
961 if (Attr.getNumArgs() != 3) {
962 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
963 return;
964 }
965
966 unsigned WGSize[3];
967 for (unsigned i = 0; i < 3; ++i) {
968 Expr *E = static_cast<Expr *>(Attr.getArg(i));
969 llvm::APSInt ArgNum(32);
970 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
971 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
972 << "reqd_work_group_size" << E->getSourceRange();
973 return;
974 }
975 WGSize[i] = (unsigned) ArgNum.getZExtValue();
976 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000977 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000978 WGSize[2]));
979}
980
Chris Lattner026dc962009-02-14 07:37:35 +0000981static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000982 // Attribute has no arguments.
983 if (Attr.getNumArgs() != 1) {
984 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
985 return;
986 }
987
988 // Make sure that there is a string literal as the sections's single
989 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000990 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
991 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000992 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000993 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000994 return;
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattner797c3c42009-08-10 19:03:04 +0000997 std::string SectionStr(SE->getStrData(), SE->getByteLength());
998
999 // If the target wants to validate the section specifier, make it happen.
1000 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
1001 if (Error.empty()) {
1002 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
1003 return;
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner797c3c42009-08-10 19:03:04 +00001006 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1007 << Error;
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001009}
1010
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001011static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1012 // Attribute has no arguments.
1013 if (Attr.getNumArgs() != 0) {
1014 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1015 return;
1016 }
1017
1018 // Attribute can be applied only to functions.
1019 if (!isa<FunctionDecl>(d)) {
1020 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1021 << Attr.getName() << 0 /*function*/;
1022 return;
1023 }
1024
1025 // cdecl and fastcall attributes are mutually incompatible.
1026 if (d->getAttr<FastCallAttr>()) {
1027 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1028 << "cdecl" << "fastcall";
1029 return;
1030 }
1031
1032 // cdecl and stdcall attributes are mutually incompatible.
1033 if (d->getAttr<StdCallAttr>()) {
1034 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1035 << "cdecl" << "stdcall";
1036 return;
1037 }
1038
1039 d->addAttr(::new (S.Context) CDeclAttr());
1040}
1041
1042
Chris Lattner803d0802008-06-29 00:43:07 +00001043static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001044 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001045 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001046 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001047 return;
1048 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001049
1050 // Attribute can be applied only to functions.
1051 if (!isa<FunctionDecl>(d)) {
1052 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001053 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001054 return;
1055 }
1056
1057 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001058 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001059 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1060 << "stdcall" << "fastcall";
1061 return;
1062 }
1063
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001064 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001065}
1066
John McCall9112b932009-11-04 03:36:09 +00001067/// Diagnose the use of a non-standard calling convention on the given
1068/// function.
1069static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1070 SourceLocation Loc, Sema &S) {
1071 if (!D->hasPrototype()) {
1072 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1073 return;
1074 }
1075
1076 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1077 if (T->isVariadic()) {
1078 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1079 return;
1080 }
1081}
1082
Chris Lattner803d0802008-06-29 00:43:07 +00001083static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001084 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001085 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001086 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001087 return;
1088 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001089
1090 if (!isa<FunctionDecl>(d)) {
1091 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001092 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001093 return;
1094 }
1095
John McCall9112b932009-11-04 03:36:09 +00001096 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1097
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001098 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001099 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001100 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1101 << "fastcall" << "stdcall";
1102 return;
1103 }
1104
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001105 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001106}
1107
Chris Lattner803d0802008-06-29 00:43:07 +00001108static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001109 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001110 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001111 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001112 return;
1113 }
Mike Stumpbf916502009-07-24 19:02:52 +00001114
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001115 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001116}
1117
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001118static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1119 // check the attribute arguments.
1120 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001121 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001122 return;
1123 }
Mike Stumpbf916502009-07-24 19:02:52 +00001124
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001125 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001126}
1127
1128static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1129 // check the attribute arguments.
1130 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001132 return;
1133 }
Mike Stumpbf916502009-07-24 19:02:52 +00001134
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001135 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001136}
1137
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001138static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001139 // Match gcc which ignores cleanup attrs when compiling C++.
1140 if (S.getLangOptions().CPlusPlus)
1141 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001142
1143 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001144 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1145 return;
1146 }
Mike Stumpbf916502009-07-24 19:02:52 +00001147
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001148 if (Attr.getNumArgs() != 0) {
1149 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1150 return;
1151 }
Mike Stumpbf916502009-07-24 19:02:52 +00001152
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001153 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001154
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001155 if (!VD || !VD->hasLocalStorage()) {
1156 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1157 return;
1158 }
Mike Stumpbf916502009-07-24 19:02:52 +00001159
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001160 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001161 NamedDecl *CleanupDecl
1162 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1163 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001164 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001165 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001166 Attr.getParameterName();
1167 return;
1168 }
Mike Stumpbf916502009-07-24 19:02:52 +00001169
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001170 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1171 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001172 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001173 Attr.getParameterName();
1174 return;
1175 }
1176
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001177 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001178 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001179 Attr.getParameterName();
1180 return;
1181 }
Mike Stumpbf916502009-07-24 19:02:52 +00001182
Anders Carlsson89941c12009-02-07 23:16:50 +00001183 // We're currently more strict than GCC about what function types we accept.
1184 // If this ever proves to be a problem it should be easy to fix.
1185 QualType Ty = S.Context.getPointerType(VD->getType());
1186 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001187 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001188 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001189 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1190 Attr.getParameterName() << ParamTy << Ty;
1191 return;
1192 }
Mike Stumpbf916502009-07-24 19:02:52 +00001193
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001194 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001195}
1196
Mike Stumpbf916502009-07-24 19:02:52 +00001197/// Handle __attribute__((format_arg((idx)))) attribute based on
1198/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1199static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001200 if (Attr.getNumArgs() != 1) {
1201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1202 return;
1203 }
1204 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1205 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1206 << Attr.getName() << 0 /*function*/;
1207 return;
1208 }
Mike Stumpbf916502009-07-24 19:02:52 +00001209 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1210 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001211 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1212 unsigned FirstIdx = 1;
1213 // checks for the 2nd argument
1214 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1215 llvm::APSInt Idx(32);
1216 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1217 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1218 << "format" << 2 << IdxExpr->getSourceRange();
1219 return;
1220 }
Mike Stumpbf916502009-07-24 19:02:52 +00001221
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001222 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1223 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1224 << "format" << 2 << IdxExpr->getSourceRange();
1225 return;
1226 }
Mike Stumpbf916502009-07-24 19:02:52 +00001227
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001228 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001229
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001230 // make sure the format string is really a string
1231 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001232
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001233 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1234 if (not_nsstring_type &&
1235 !isCFStringType(Ty, S.Context) &&
1236 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001237 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001238 // FIXME: Should highlight the actual expression that has the wrong type.
1239 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001240 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001241 << IdxExpr->getSourceRange();
1242 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001243 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001244 Ty = getFunctionOrMethodResultType(d);
1245 if (!isNSStringType(Ty, S.Context) &&
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_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001251 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001252 << IdxExpr->getSourceRange();
1253 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001254 }
1255
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001256 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001257}
1258
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001259enum FormatAttrKind {
1260 CFStringFormat,
1261 NSStringFormat,
1262 StrftimeFormat,
1263 SupportedFormat,
1264 InvalidFormat
1265};
1266
1267/// getFormatAttrKind - Map from format attribute names to supported format
1268/// types.
1269static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1270 // Check for formats that get handled specially.
1271 if (Format == "NSString")
1272 return NSStringFormat;
1273 if (Format == "CFString")
1274 return CFStringFormat;
1275 if (Format == "strftime")
1276 return StrftimeFormat;
1277
1278 // Otherwise, check for supported formats.
1279 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1280 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1281 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1282 Format == "zcmn_err")
1283 return SupportedFormat;
1284
1285 return InvalidFormat;
1286}
1287
Mike Stumpbf916502009-07-24 19:02:52 +00001288/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1289/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001290static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001291
Chris Lattner545dd342008-06-28 23:36:30 +00001292 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001293 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001294 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001295 return;
1296 }
1297
Chris Lattner545dd342008-06-28 23:36:30 +00001298 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001299 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001300 return;
1301 }
1302
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001303 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001304 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001305 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001306 return;
1307 }
1308
Daniel Dunbar35682492008-09-26 04:12:28 +00001309 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001310 unsigned FirstIdx = 1;
1311
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001312 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313
1314 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001315 if (Format.startswith("__") && Format.endswith("__"))
1316 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001317
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001318 // Check for supported formats.
1319 FormatAttrKind Kind = getFormatAttrKind(Format);
1320 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001321 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001322 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001323 return;
1324 }
1325
1326 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001327 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001328 llvm::APSInt Idx(32);
1329 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001330 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001331 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001332 return;
1333 }
1334
Anders Carlsson4fb77202009-08-25 14:12:34 +00001335 // FIXME: We should handle the implicit 'this' parameter in a more generic
1336 // way that can be used for other arguments.
1337 bool HasImplicitThisParam = false;
1338 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1339 if (MD->isInstance()) {
1340 HasImplicitThisParam = true;
1341 NumArgs++;
1342 }
1343 }
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Chris Lattner6b6b5372008-06-26 18:38:35 +00001345 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001346 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001347 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001348 return;
1349 }
1350
1351 // FIXME: Do we need to bounds check?
1352 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001353
Anders Carlsson4fb77202009-08-25 14:12:34 +00001354 if (HasImplicitThisParam) ArgIdx--;
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Chris Lattner6b6b5372008-06-26 18:38:35 +00001356 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001357 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001358
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001359 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001360 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001361 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1362 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001363 return;
1364 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001365 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001366 // FIXME: do we need to check if the type is NSString*? What are the
1367 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001368 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001369 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001370 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1371 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001372 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001373 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001374 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001375 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001376 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001377 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1378 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001379 return;
1380 }
1381
1382 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001383 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001384 llvm::APSInt FirstArg(32);
1385 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001387 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001388 return;
1389 }
1390
1391 // check if the function is variadic if the 3rd argument non-zero
1392 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001393 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001394 ++NumArgs; // +1 for ...
1395 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001396 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001397 return;
1398 }
1399 }
1400
Chris Lattner3c73c412008-11-19 08:23:25 +00001401 // strftime requires FirstArg to be 0 because it doesn't read from any
1402 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001403 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001404 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001405 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1406 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001407 return;
1408 }
1409 // if 0 it disables parameter checking (to use with e.g. va_list)
1410 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001411 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001412 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001413 return;
1414 }
1415
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001416 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1417 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001418}
1419
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001420static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1421 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001422 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001423 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425 return;
1426 }
1427
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001428 // Try to find the underlying union declaration.
1429 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001430 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001431 if (TD && TD->getUnderlyingType()->isUnionType())
1432 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1433 else
1434 RD = dyn_cast<RecordDecl>(d);
1435
1436 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001437 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001438 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001439 return;
1440 }
1441
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001442 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001443 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001444 diag::warn_transparent_union_attribute_not_definition);
1445 return;
1446 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001447
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001448 RecordDecl::field_iterator Field = RD->field_begin(),
1449 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001450 if (Field == FieldEnd) {
1451 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1452 return;
1453 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001454
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001455 FieldDecl *FirstField = *Field;
1456 QualType FirstType = FirstField->getType();
1457 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001458 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001459 diag::warn_transparent_union_attribute_floating);
1460 return;
1461 }
1462
1463 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1464 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1465 for (; Field != FieldEnd; ++Field) {
1466 QualType FieldType = Field->getType();
1467 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1468 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1469 // Warn if we drop the attribute.
1470 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001471 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001472 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001473 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001474 diag::warn_transparent_union_attribute_field_size_align)
1475 << isSize << Field->getDeclName() << FieldBits;
1476 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001477 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001478 diag::note_transparent_union_first_field_size_align)
1479 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001480 return;
1481 }
1482 }
1483
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001484 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001485}
1486
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001487static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001488 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001489 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001491 return;
1492 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001493 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1494 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001495
Chris Lattner6b6b5372008-06-26 18:38:35 +00001496 // Make sure that there is a string literal as the annotation's single
1497 // argument.
1498 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001499 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001500 return;
1501 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001502 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001503 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001504}
1505
Chris Lattner803d0802008-06-29 00:43:07 +00001506static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001507 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001508 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001510 return;
1511 }
1512
1513 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001514 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001515 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001516 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001517 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001518 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001519 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001520 }
Mike Stumpbf916502009-07-24 19:02:52 +00001521
Chris Lattner49e2d342008-06-28 23:50:44 +00001522 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1523 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001524 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1526 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001527 return;
1528 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001529 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001530 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001531 << alignmentExpr->getSourceRange();
1532 return;
1533 }
1534
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001535 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001536}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001537
Mike Stumpbf916502009-07-24 19:02:52 +00001538/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1539/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001540///
Mike Stumpbf916502009-07-24 19:02:52 +00001541/// Despite what would be logical, the mode attribute is a decl attribute, not a
1542/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1543/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001544static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001545 // This attribute isn't documented, but glibc uses it. It changes
1546 // the width of an int or unsigned int to the specified size.
1547
1548 // Check that there aren't any arguments
1549 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001550 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001551 return;
1552 }
1553
1554 IdentifierInfo *Name = Attr.getParameterName();
1555 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001556 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001557 return;
1558 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001559
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001560 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001561
1562 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001563 if (Str.startswith("__") && Str.endswith("__"))
1564 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001565
1566 unsigned DestWidth = 0;
1567 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001568 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001569 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001570 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001571 switch (Str[0]) {
1572 case 'Q': DestWidth = 8; break;
1573 case 'H': DestWidth = 16; break;
1574 case 'S': DestWidth = 32; break;
1575 case 'D': DestWidth = 64; break;
1576 case 'X': DestWidth = 96; break;
1577 case 'T': DestWidth = 128; break;
1578 }
1579 if (Str[1] == 'F') {
1580 IntegerMode = false;
1581 } else if (Str[1] == 'C') {
1582 IntegerMode = false;
1583 ComplexMode = true;
1584 } else if (Str[1] != 'I') {
1585 DestWidth = 0;
1586 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587 break;
1588 case 4:
1589 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1590 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001591 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001592 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001593 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001594 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 break;
1596 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001597 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001598 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001599 break;
1600 }
1601
1602 QualType OldTy;
1603 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1604 OldTy = TD->getUnderlyingType();
1605 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1606 OldTy = VD->getType();
1607 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001608 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1609 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001610 return;
1611 }
Eli Friedman73397492009-03-03 06:41:03 +00001612
John McCall183700f2009-09-21 23:43:11 +00001613 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001614 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1615 else if (IntegerMode) {
1616 if (!OldTy->isIntegralType())
1617 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1618 } else if (ComplexMode) {
1619 if (!OldTy->isComplexType())
1620 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1621 } else {
1622 if (!OldTy->isFloatingType())
1623 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1624 }
1625
Mike Stump390b4cc2009-05-16 07:39:55 +00001626 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1627 // and friends, at least with glibc.
1628 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1629 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001630 // FIXME: Make sure floating-point mappings are accurate
1631 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001632 QualType NewTy;
1633 switch (DestWidth) {
1634 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001635 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001636 return;
1637 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001638 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001639 return;
1640 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001641 if (!IntegerMode) {
1642 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1643 return;
1644 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001645 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001646 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001647 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001648 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001649 break;
1650 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001651 if (!IntegerMode) {
1652 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1653 return;
1654 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001655 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001656 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001657 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001658 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001659 break;
1660 case 32:
1661 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001662 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001663 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001664 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001665 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001666 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001667 break;
1668 case 64:
1669 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001670 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001671 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001672 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001673 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001674 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001675 break;
Eli Friedman73397492009-03-03 06:41:03 +00001676 case 96:
1677 NewTy = S.Context.LongDoubleTy;
1678 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001679 case 128:
1680 if (!IntegerMode) {
1681 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1682 return;
1683 }
1684 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001685 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001686 }
1687
Eli Friedman73397492009-03-03 06:41:03 +00001688 if (ComplexMode) {
1689 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001690 }
1691
1692 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001693 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1694 // FIXME: preserve existing source info.
1695 TD->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(NewTy));
1696 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001697 cast<ValueDecl>(D)->setType(NewTy);
1698}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001699
Mike Stump1feade82009-08-26 22:31:08 +00001700static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001701 // check the attribute arguments.
1702 if (Attr.getNumArgs() > 0) {
1703 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1704 return;
1705 }
Anders Carlssone896d982009-02-13 08:11:52 +00001706
Anders Carlsson5bab7882009-02-19 19:16:48 +00001707 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001708 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001709 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001710 return;
1711 }
Mike Stumpbf916502009-07-24 19:02:52 +00001712
Mike Stump1feade82009-08-26 22:31:08 +00001713 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001714}
1715
Mike Stump1feade82009-08-26 22:31:08 +00001716static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001717 // check the attribute arguments.
1718 if (Attr.getNumArgs() != 0) {
1719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1720 return;
1721 }
Mike Stumpbf916502009-07-24 19:02:52 +00001722
Chris Lattnerc5197432009-04-14 17:02:11 +00001723 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001725 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001726 return;
1727 }
Mike Stumpbf916502009-07-24 19:02:52 +00001728
Mike Stump1feade82009-08-26 22:31:08 +00001729 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001730}
1731
Chris Lattnercf2a7212009-04-20 19:12:28 +00001732static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001733 // check the attribute arguments.
1734 if (Attr.getNumArgs() != 0) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1736 return;
1737 }
Mike Stumpbf916502009-07-24 19:02:52 +00001738
Chris Lattnerc5197432009-04-14 17:02:11 +00001739 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1740 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001742 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001743 return;
1744 }
Mike Stumpbf916502009-07-24 19:02:52 +00001745
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001746 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001747 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001748 return;
1749 }
Mike Stumpbf916502009-07-24 19:02:52 +00001750
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001751 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001752}
1753
Fariborz Jahanianee760332009-03-27 18:38:55 +00001754static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1755 // check the attribute arguments.
1756 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001758 return;
1759 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001760
Fariborz Jahanianee760332009-03-27 18:38:55 +00001761 if (!isFunctionOrMethod(d)) {
1762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001763 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001764 return;
1765 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001766
1767 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1768 llvm::APSInt NumParams(32);
1769 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1771 << "regparm" << NumParamsExpr->getSourceRange();
1772 return;
1773 }
1774
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001775 if (S.Context.Target.getRegParmMax() == 0) {
1776 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001777 << NumParamsExpr->getSourceRange();
1778 return;
1779 }
1780
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001781 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001782 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1783 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001784 return;
1785 }
1786
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001787 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001788}
1789
Chris Lattner0744e5f2008-06-29 00:23:49 +00001790//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001791// Checker-specific attribute handlers.
1792//===----------------------------------------------------------------------===//
1793
1794static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1795 Sema &S) {
1796
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001797 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001798
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001799 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1800 RetTy = MD->getResultType();
1801 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1802 RetTy = FD->getResultType();
1803 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001804 SourceLocation L = Attr.getLoc();
1805 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1806 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001807 return;
1808 }
Mike Stumpbf916502009-07-24 19:02:52 +00001809
Ted Kremenek6217b802009-07-29 21:53:49 +00001810 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001811 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001812 SourceLocation L = Attr.getLoc();
1813 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1814 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001815 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001816 }
Mike Stumpbf916502009-07-24 19:02:52 +00001817
Ted Kremenekb71368d2009-05-09 02:44:38 +00001818 switch (Attr.getKind()) {
1819 default:
1820 assert(0 && "invalid ownership attribute");
1821 return;
1822 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001823 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001824 return;
1825 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001826 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001827 return;
1828 };
1829}
1830
1831//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001832// Top Level Sema Entry Points
1833//===----------------------------------------------------------------------===//
1834
Sebastian Redla89d82c2008-12-21 19:24:58 +00001835/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001836/// the attribute applies to decls. If the attribute is a type attribute, just
1837/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001838static void ProcessDeclAttribute(Scope *scope, Decl *D,
1839 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001840 if (Attr.isDeclspecAttribute())
1841 // FIXME: Try to deal with __declspec attributes!
1842 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001843 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001844 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001845 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001846 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001847 // Ignore these, these are type attributes, handled by
1848 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001849 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001850 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1851 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001852 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001853 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001854 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001855 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001856 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001857 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001858 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1859 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1860 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1861 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1862 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001863 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001864 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001865 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001866 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1867 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001868 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001869 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001870 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001871 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001872 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1873 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1874 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001875
1876 // Checker-specific.
1877 case AttributeList::AT_ns_returns_retained:
1878 case AttributeList::AT_cf_returns_retained:
1879 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1880
Nate Begeman6f3d8382009-06-26 06:32:41 +00001881 case AttributeList::AT_reqd_wg_size:
1882 HandleReqdWorkGroupSize(D, Attr, S); break;
1883
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001884 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001885 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001886 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001887 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001888 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001889 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001890 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001891 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001892 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1893 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001894 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001895 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001896 case AttributeList::AT_transparent_union:
1897 HandleTransparentUnionAttr(D, Attr, S);
1898 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001899 case AttributeList::AT_objc_exception:
1900 HandleObjCExceptionAttr(D, Attr, S);
1901 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001902 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001903 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001904 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001905 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001906 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1907 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001908 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Mike Stump1feade82009-08-26 22:31:08 +00001909 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1910 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001911 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001912 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001913 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001914 // Just ignore
1915 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001916 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001917 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001918 break;
1919 }
1920}
1921
1922/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1923/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001924void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001925 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001926 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001927 AttrList = AttrList->getNext();
1928 }
1929}
1930
Ryan Flynne25ff832009-07-30 03:15:39 +00001931/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1932/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001933NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001934 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001935 NamedDecl *NewD = 0;
1936 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1937 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1938 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001939 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001940 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1941 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1942 VD->getLocation(), II,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001943 VD->getType(), VD->getDeclaratorInfo(),
1944 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001945 }
1946 return NewD;
1947}
1948
1949/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1950/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001951void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001952 if (W.getUsed()) return; // only do this once
1953 W.setUsed(true);
1954 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1955 IdentifierInfo *NDId = ND->getIdentifier();
1956 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1957 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1958 NewD->addAttr(::new (Context) WeakAttr());
1959 WeakTopLevelDecl.push_back(NewD);
1960 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1961 // to insert Decl at TU scope, sorry.
1962 DeclContext *SavedContext = CurContext;
1963 CurContext = Context.getTranslationUnitDecl();
1964 PushOnScopeChains(NewD, S);
1965 CurContext = SavedContext;
1966 } else { // just add weak to existing
1967 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001968 }
1969}
1970
Chris Lattner0744e5f2008-06-29 00:23:49 +00001971/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1972/// it, apply them to D. This is a bit tricky because PD can have attributes
1973/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001974void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001975 // Handle #pragma weak
1976 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1977 if (ND->hasLinkage()) {
1978 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1979 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001980 // Identifier referenced by #pragma weak before it was declared
1981 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001982 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1983 }
1984 }
1985 }
1986
Chris Lattner0744e5f2008-06-29 00:23:49 +00001987 // Apply decl attributes from the DeclSpec if present.
1988 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001989 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001990
Chris Lattner0744e5f2008-06-29 00:23:49 +00001991 // Walk the declarator structure, applying decl attributes that were in a type
1992 // position to the decl itself. This handles cases like:
1993 // int *__attr__(x)** D;
1994 // when X is a decl attribute.
1995 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1996 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001997 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001998
Chris Lattner0744e5f2008-06-29 00:23:49 +00001999 // Finally, apply any attributes on the decl itself.
2000 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002001 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002002}
John McCall54abf7d2009-11-04 02:18:39 +00002003
2004/// PushParsingDeclaration - Enter a new "scope" of deprecation
2005/// warnings.
2006///
2007/// The state token we use is the start index of this scope
2008/// on the warning stack.
2009Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2010 ParsingDeclDepth++;
2011 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2012}
2013
2014static bool isDeclDeprecated(Decl *D) {
2015 do {
2016 if (D->hasAttr<DeprecatedAttr>())
2017 return true;
2018 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2019 return false;
2020}
2021
2022void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2023 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2024 ParsingDeclDepth--;
2025
2026 if (DelayedDeprecationWarnings.empty())
2027 return;
2028
2029 unsigned SavedIndex = (unsigned) S;
2030 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2031 "saved index is out of bounds");
2032
2033 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2034 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2035 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2036 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2037 if (ND) {
2038 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2039
2040 // Prevent this from triggering multiple times.
2041 ND = 0;
2042 }
2043 }
2044 }
2045
2046 DelayedDeprecationWarnings.set_size(SavedIndex);
2047}
2048
2049void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2050 // Delay if we're currently parsing a declaration.
2051 if (ParsingDeclDepth) {
2052 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2053 return;
2054 }
2055
2056 // Otherwise, don't warn if our current context is deprecated.
2057 if (isDeclDeprecated(cast<Decl>(CurContext)))
2058 return;
2059
2060 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2061}