blob: 18f57da76912793ad9a7ad3060f0fb18893f8bb7 [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
Chris Lattner803d0802008-06-29 00:43:07 +00001011static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001012 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001013 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001014 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001015 return;
1016 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001017
1018 // Attribute can be applied only to functions.
1019 if (!isa<FunctionDecl>(d)) {
1020 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001021 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001022 return;
1023 }
1024
1025 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001026 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001027 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1028 << "stdcall" << "fastcall";
1029 return;
1030 }
1031
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001032 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001033}
1034
John McCall9112b932009-11-04 03:36:09 +00001035/// Diagnose the use of a non-standard calling convention on the given
1036/// function.
1037static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
1038 SourceLocation Loc, Sema &S) {
1039 if (!D->hasPrototype()) {
1040 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1041 return;
1042 }
1043
1044 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1045 if (T->isVariadic()) {
1046 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1047 return;
1048 }
1049}
1050
Chris Lattner803d0802008-06-29 00:43:07 +00001051static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001052 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001053 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001054 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001055 return;
1056 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001057
1058 if (!isa<FunctionDecl>(d)) {
1059 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001060 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001061 return;
1062 }
1063
John McCall9112b932009-11-04 03:36:09 +00001064 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1065
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001066 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001067 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001068 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1069 << "fastcall" << "stdcall";
1070 return;
1071 }
1072
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001073 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001074}
1075
Chris Lattner803d0802008-06-29 00:43:07 +00001076static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001077 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001078 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001079 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001080 return;
1081 }
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001083 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001084}
1085
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001086static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1087 // check the attribute arguments.
1088 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001089 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001090 return;
1091 }
Mike Stumpbf916502009-07-24 19:02:52 +00001092
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001093 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001094}
1095
1096static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1097 // check the attribute arguments.
1098 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001099 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001100 return;
1101 }
Mike Stumpbf916502009-07-24 19:02:52 +00001102
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001103 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001104}
1105
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001106static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001107 // Match gcc which ignores cleanup attrs when compiling C++.
1108 if (S.getLangOptions().CPlusPlus)
1109 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001110
1111 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001112 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1113 return;
1114 }
Mike Stumpbf916502009-07-24 19:02:52 +00001115
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001116 if (Attr.getNumArgs() != 0) {
1117 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1118 return;
1119 }
Mike Stumpbf916502009-07-24 19:02:52 +00001120
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001121 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001122
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001123 if (!VD || !VD->hasLocalStorage()) {
1124 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1125 return;
1126 }
Mike Stumpbf916502009-07-24 19:02:52 +00001127
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001128 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001129 NamedDecl *CleanupDecl
1130 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1131 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001132 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001133 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001134 Attr.getParameterName();
1135 return;
1136 }
Mike Stumpbf916502009-07-24 19:02:52 +00001137
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001138 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1139 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001141 Attr.getParameterName();
1142 return;
1143 }
1144
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001145 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001146 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001147 Attr.getParameterName();
1148 return;
1149 }
Mike Stumpbf916502009-07-24 19:02:52 +00001150
Anders Carlsson89941c12009-02-07 23:16:50 +00001151 // We're currently more strict than GCC about what function types we accept.
1152 // If this ever proves to be a problem it should be easy to fix.
1153 QualType Ty = S.Context.getPointerType(VD->getType());
1154 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001155 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001156 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001157 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1158 Attr.getParameterName() << ParamTy << Ty;
1159 return;
1160 }
Mike Stumpbf916502009-07-24 19:02:52 +00001161
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001162 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001163}
1164
Mike Stumpbf916502009-07-24 19:02:52 +00001165/// Handle __attribute__((format_arg((idx)))) attribute based on
1166/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1167static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001168 if (Attr.getNumArgs() != 1) {
1169 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1170 return;
1171 }
1172 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1173 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1174 << Attr.getName() << 0 /*function*/;
1175 return;
1176 }
Mike Stumpbf916502009-07-24 19:02:52 +00001177 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1178 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001179 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1180 unsigned FirstIdx = 1;
1181 // checks for the 2nd argument
1182 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1183 llvm::APSInt Idx(32);
1184 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1186 << "format" << 2 << IdxExpr->getSourceRange();
1187 return;
1188 }
Mike Stumpbf916502009-07-24 19:02:52 +00001189
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001190 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1192 << "format" << 2 << IdxExpr->getSourceRange();
1193 return;
1194 }
Mike Stumpbf916502009-07-24 19:02:52 +00001195
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001196 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001197
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001198 // make sure the format string is really a string
1199 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001200
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001201 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1202 if (not_nsstring_type &&
1203 !isCFStringType(Ty, S.Context) &&
1204 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001205 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001206 // FIXME: Should highlight the actual expression that has the wrong type.
1207 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001208 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001209 << IdxExpr->getSourceRange();
1210 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001211 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001212 Ty = getFunctionOrMethodResultType(d);
1213 if (!isNSStringType(Ty, S.Context) &&
1214 !isCFStringType(Ty, S.Context) &&
1215 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001216 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001217 // FIXME: Should highlight the actual expression that has the wrong type.
1218 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001219 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001220 << IdxExpr->getSourceRange();
1221 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001222 }
1223
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001224 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001225}
1226
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001227enum FormatAttrKind {
1228 CFStringFormat,
1229 NSStringFormat,
1230 StrftimeFormat,
1231 SupportedFormat,
1232 InvalidFormat
1233};
1234
1235/// getFormatAttrKind - Map from format attribute names to supported format
1236/// types.
1237static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1238 // Check for formats that get handled specially.
1239 if (Format == "NSString")
1240 return NSStringFormat;
1241 if (Format == "CFString")
1242 return CFStringFormat;
1243 if (Format == "strftime")
1244 return StrftimeFormat;
1245
1246 // Otherwise, check for supported formats.
1247 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1248 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1249 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1250 Format == "zcmn_err")
1251 return SupportedFormat;
1252
1253 return InvalidFormat;
1254}
1255
Mike Stumpbf916502009-07-24 19:02:52 +00001256/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1257/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001258static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001259
Chris Lattner545dd342008-06-28 23:36:30 +00001260 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001262 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263 return;
1264 }
1265
Chris Lattner545dd342008-06-28 23:36:30 +00001266 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001268 return;
1269 }
1270
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001271 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001272 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001273 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274 return;
1275 }
1276
Daniel Dunbar35682492008-09-26 04:12:28 +00001277 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001278 unsigned FirstIdx = 1;
1279
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001280 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001281
1282 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001283 if (Format.startswith("__") && Format.endswith("__"))
1284 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001285
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001286 // Check for supported formats.
1287 FormatAttrKind Kind = getFormatAttrKind(Format);
1288 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001289 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001290 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001291 return;
1292 }
1293
1294 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001295 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001296 llvm::APSInt Idx(32);
1297 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001298 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001299 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001300 return;
1301 }
1302
Anders Carlsson4fb77202009-08-25 14:12:34 +00001303 // FIXME: We should handle the implicit 'this' parameter in a more generic
1304 // way that can be used for other arguments.
1305 bool HasImplicitThisParam = false;
1306 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1307 if (MD->isInstance()) {
1308 HasImplicitThisParam = true;
1309 NumArgs++;
1310 }
1311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001315 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001316 return;
1317 }
1318
1319 // FIXME: Do we need to bounds check?
1320 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001321
Anders Carlsson4fb77202009-08-25 14:12:34 +00001322 if (HasImplicitThisParam) ArgIdx--;
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Chris Lattner6b6b5372008-06-26 18:38:35 +00001324 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001325 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001326
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001327 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001328 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001329 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1330 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001331 return;
1332 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001333 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001334 // FIXME: do we need to check if the type is NSString*? What are the
1335 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001336 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001337 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001338 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1339 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001340 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001341 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001342 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001343 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001344 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001345 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1346 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347 return;
1348 }
1349
1350 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001351 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001352 llvm::APSInt FirstArg(32);
1353 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001354 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001355 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001356 return;
1357 }
1358
1359 // check if the function is variadic if the 3rd argument non-zero
1360 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001361 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001362 ++NumArgs; // +1 for ...
1363 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001364 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001365 return;
1366 }
1367 }
1368
Chris Lattner3c73c412008-11-19 08:23:25 +00001369 // strftime requires FirstArg to be 0 because it doesn't read from any
1370 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001371 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001372 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001373 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1374 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001375 return;
1376 }
1377 // if 0 it disables parameter checking (to use with e.g. va_list)
1378 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001379 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001380 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001381 return;
1382 }
1383
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001384 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1385 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001386}
1387
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001388static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1389 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001390 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001391 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001393 return;
1394 }
1395
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001396 // Try to find the underlying union declaration.
1397 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001398 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001399 if (TD && TD->getUnderlyingType()->isUnionType())
1400 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1401 else
1402 RD = dyn_cast<RecordDecl>(d);
1403
1404 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001405 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001406 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001407 return;
1408 }
1409
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001410 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001411 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001412 diag::warn_transparent_union_attribute_not_definition);
1413 return;
1414 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001415
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001416 RecordDecl::field_iterator Field = RD->field_begin(),
1417 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001418 if (Field == FieldEnd) {
1419 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1420 return;
1421 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001422
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001423 FieldDecl *FirstField = *Field;
1424 QualType FirstType = FirstField->getType();
1425 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001426 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001427 diag::warn_transparent_union_attribute_floating);
1428 return;
1429 }
1430
1431 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1432 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1433 for (; Field != FieldEnd; ++Field) {
1434 QualType FieldType = Field->getType();
1435 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1436 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1437 // Warn if we drop the attribute.
1438 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001439 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001440 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001441 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001442 diag::warn_transparent_union_attribute_field_size_align)
1443 << isSize << Field->getDeclName() << FieldBits;
1444 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001445 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001446 diag::note_transparent_union_first_field_size_align)
1447 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001448 return;
1449 }
1450 }
1451
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001452 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453}
1454
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001455static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001456 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001457 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001459 return;
1460 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001461 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1462 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001463
Chris Lattner6b6b5372008-06-26 18:38:35 +00001464 // Make sure that there is a string literal as the annotation's single
1465 // argument.
1466 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001467 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001468 return;
1469 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001470 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001471 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001472}
1473
Chris Lattner803d0802008-06-29 00:43:07 +00001474static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001475 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001476 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001477 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001478 return;
1479 }
1480
1481 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001482 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001483 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001484 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001485 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001486 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001487 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001488 }
Mike Stumpbf916502009-07-24 19:02:52 +00001489
Chris Lattner49e2d342008-06-28 23:50:44 +00001490 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1491 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001492 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001493 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1494 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001495 return;
1496 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001497 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001498 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001499 << alignmentExpr->getSourceRange();
1500 return;
1501 }
1502
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001503 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001504}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001505
Mike Stumpbf916502009-07-24 19:02:52 +00001506/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1507/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001508///
Mike Stumpbf916502009-07-24 19:02:52 +00001509/// Despite what would be logical, the mode attribute is a decl attribute, not a
1510/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1511/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001512static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001513 // This attribute isn't documented, but glibc uses it. It changes
1514 // the width of an int or unsigned int to the specified size.
1515
1516 // Check that there aren't any arguments
1517 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001518 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001519 return;
1520 }
1521
1522 IdentifierInfo *Name = Attr.getParameterName();
1523 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001524 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001525 return;
1526 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001527
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001528 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001529
1530 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001531 if (Str.startswith("__") && Str.endswith("__"))
1532 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001533
1534 unsigned DestWidth = 0;
1535 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001536 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001537 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001538 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001539 switch (Str[0]) {
1540 case 'Q': DestWidth = 8; break;
1541 case 'H': DestWidth = 16; break;
1542 case 'S': DestWidth = 32; break;
1543 case 'D': DestWidth = 64; break;
1544 case 'X': DestWidth = 96; break;
1545 case 'T': DestWidth = 128; break;
1546 }
1547 if (Str[1] == 'F') {
1548 IntegerMode = false;
1549 } else if (Str[1] == 'C') {
1550 IntegerMode = false;
1551 ComplexMode = true;
1552 } else if (Str[1] != 'I') {
1553 DestWidth = 0;
1554 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001555 break;
1556 case 4:
1557 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1558 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001559 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001560 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001561 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001562 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001563 break;
1564 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001565 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001566 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001567 break;
1568 }
1569
1570 QualType OldTy;
1571 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1572 OldTy = TD->getUnderlyingType();
1573 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1574 OldTy = VD->getType();
1575 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001576 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1577 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001578 return;
1579 }
Eli Friedman73397492009-03-03 06:41:03 +00001580
John McCall183700f2009-09-21 23:43:11 +00001581 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001582 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1583 else if (IntegerMode) {
1584 if (!OldTy->isIntegralType())
1585 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1586 } else if (ComplexMode) {
1587 if (!OldTy->isComplexType())
1588 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1589 } else {
1590 if (!OldTy->isFloatingType())
1591 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1592 }
1593
Mike Stump390b4cc2009-05-16 07:39:55 +00001594 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1595 // and friends, at least with glibc.
1596 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1597 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001598 // FIXME: Make sure floating-point mappings are accurate
1599 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001600 QualType NewTy;
1601 switch (DestWidth) {
1602 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001603 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001604 return;
1605 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001606 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001607 return;
1608 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001609 if (!IntegerMode) {
1610 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1611 return;
1612 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001613 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001614 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001615 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001616 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001617 break;
1618 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001619 if (!IntegerMode) {
1620 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1621 return;
1622 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001623 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001624 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001625 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001626 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001627 break;
1628 case 32:
1629 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001630 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001631 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001632 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001633 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001634 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001635 break;
1636 case 64:
1637 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001638 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001639 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001640 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001641 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001642 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001643 break;
Eli Friedman73397492009-03-03 06:41:03 +00001644 case 96:
1645 NewTy = S.Context.LongDoubleTy;
1646 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001647 case 128:
1648 if (!IntegerMode) {
1649 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1650 return;
1651 }
1652 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001653 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001654 }
1655
Eli Friedman73397492009-03-03 06:41:03 +00001656 if (ComplexMode) {
1657 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001658 }
1659
1660 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001661 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1662 // FIXME: preserve existing source info.
1663 TD->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(NewTy));
1664 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001665 cast<ValueDecl>(D)->setType(NewTy);
1666}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001667
Mike Stump1feade82009-08-26 22:31:08 +00001668static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001669 // check the attribute arguments.
1670 if (Attr.getNumArgs() > 0) {
1671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1672 return;
1673 }
Anders Carlssone896d982009-02-13 08:11:52 +00001674
Anders Carlsson5bab7882009-02-19 19:16:48 +00001675 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001676 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001677 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001678 return;
1679 }
Mike Stumpbf916502009-07-24 19:02:52 +00001680
Mike Stump1feade82009-08-26 22:31:08 +00001681 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001682}
1683
Mike Stump1feade82009-08-26 22:31:08 +00001684static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001685 // check the attribute arguments.
1686 if (Attr.getNumArgs() != 0) {
1687 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1688 return;
1689 }
Mike Stumpbf916502009-07-24 19:02:52 +00001690
Chris Lattnerc5197432009-04-14 17:02:11 +00001691 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001692 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001693 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001694 return;
1695 }
Mike Stumpbf916502009-07-24 19:02:52 +00001696
Mike Stump1feade82009-08-26 22:31:08 +00001697 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001698}
1699
Chris Lattnercf2a7212009-04-20 19:12:28 +00001700static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +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 }
Mike Stumpbf916502009-07-24 19:02:52 +00001706
Chris Lattnerc5197432009-04-14 17:02:11 +00001707 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1708 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001709 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001710 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001711 return;
1712 }
Mike Stumpbf916502009-07-24 19:02:52 +00001713
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001714 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001715 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001716 return;
1717 }
Mike Stumpbf916502009-07-24 19:02:52 +00001718
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001719 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001720}
1721
Fariborz Jahanianee760332009-03-27 18:38:55 +00001722static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1723 // check the attribute arguments.
1724 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001725 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001726 return;
1727 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001728
Fariborz Jahanianee760332009-03-27 18:38:55 +00001729 if (!isFunctionOrMethod(d)) {
1730 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001731 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001732 return;
1733 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001734
1735 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1736 llvm::APSInt NumParams(32);
1737 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1738 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1739 << "regparm" << NumParamsExpr->getSourceRange();
1740 return;
1741 }
1742
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001743 if (S.Context.Target.getRegParmMax() == 0) {
1744 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001745 << NumParamsExpr->getSourceRange();
1746 return;
1747 }
1748
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001749 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1751 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001752 return;
1753 }
1754
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001755 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001756}
1757
Chris Lattner0744e5f2008-06-29 00:23:49 +00001758//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001759// Checker-specific attribute handlers.
1760//===----------------------------------------------------------------------===//
1761
1762static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1763 Sema &S) {
1764
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001765 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001766
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001767 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1768 RetTy = MD->getResultType();
1769 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1770 RetTy = FD->getResultType();
1771 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001772 SourceLocation L = Attr.getLoc();
1773 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1774 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001775 return;
1776 }
Mike Stumpbf916502009-07-24 19:02:52 +00001777
Ted Kremenek6217b802009-07-29 21:53:49 +00001778 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001779 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001780 SourceLocation L = Attr.getLoc();
1781 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1782 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001783 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001784 }
Mike Stumpbf916502009-07-24 19:02:52 +00001785
Ted Kremenekb71368d2009-05-09 02:44:38 +00001786 switch (Attr.getKind()) {
1787 default:
1788 assert(0 && "invalid ownership attribute");
1789 return;
1790 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001791 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001792 return;
1793 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001794 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001795 return;
1796 };
1797}
1798
1799//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001800// Top Level Sema Entry Points
1801//===----------------------------------------------------------------------===//
1802
Sebastian Redla89d82c2008-12-21 19:24:58 +00001803/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001804/// the attribute applies to decls. If the attribute is a type attribute, just
1805/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001806static void ProcessDeclAttribute(Scope *scope, Decl *D,
1807 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001808 if (Attr.isDeclspecAttribute())
1809 // FIXME: Try to deal with __declspec attributes!
1810 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001811 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001812 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001813 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001814 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001815 // Ignore these, these are type attributes, handled by
1816 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001817 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001818 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1819 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001820 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001821 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001822 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001823 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001824 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1825 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1826 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1827 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1828 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1829 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001830 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001831 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001832 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001833 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1834 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001835 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001836 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001837 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001838 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001839 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1840 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1841 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001842
1843 // Checker-specific.
1844 case AttributeList::AT_ns_returns_retained:
1845 case AttributeList::AT_cf_returns_retained:
1846 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1847
Nate Begeman6f3d8382009-06-26 06:32:41 +00001848 case AttributeList::AT_reqd_wg_size:
1849 HandleReqdWorkGroupSize(D, Attr, S); break;
1850
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001851 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001852 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001853 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001854 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001855 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001856 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001857 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001858 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001859 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1860 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001861 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001862 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001863 case AttributeList::AT_transparent_union:
1864 HandleTransparentUnionAttr(D, Attr, S);
1865 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001866 case AttributeList::AT_objc_exception:
1867 HandleObjCExceptionAttr(D, Attr, S);
1868 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001869 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001870 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001871 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001872 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001873 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1874 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001875 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Mike Stump1feade82009-08-26 22:31:08 +00001876 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1877 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001878 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001879 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001880 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001881 // Just ignore
1882 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001883 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001884 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001885 break;
1886 }
1887}
1888
1889/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1890/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001891void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001892 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001893 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001894 AttrList = AttrList->getNext();
1895 }
1896}
1897
Ryan Flynne25ff832009-07-30 03:15:39 +00001898/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1899/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001900NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001901 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001902 NamedDecl *NewD = 0;
1903 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1904 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1905 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001906 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001907 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1908 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1909 VD->getLocation(), II,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001910 VD->getType(), VD->getDeclaratorInfo(),
1911 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001912 }
1913 return NewD;
1914}
1915
1916/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1917/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001918void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001919 if (W.getUsed()) return; // only do this once
1920 W.setUsed(true);
1921 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1922 IdentifierInfo *NDId = ND->getIdentifier();
1923 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1924 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1925 NewD->addAttr(::new (Context) WeakAttr());
1926 WeakTopLevelDecl.push_back(NewD);
1927 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1928 // to insert Decl at TU scope, sorry.
1929 DeclContext *SavedContext = CurContext;
1930 CurContext = Context.getTranslationUnitDecl();
1931 PushOnScopeChains(NewD, S);
1932 CurContext = SavedContext;
1933 } else { // just add weak to existing
1934 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001935 }
1936}
1937
Chris Lattner0744e5f2008-06-29 00:23:49 +00001938/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1939/// it, apply them to D. This is a bit tricky because PD can have attributes
1940/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001941void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001942 // Handle #pragma weak
1943 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1944 if (ND->hasLinkage()) {
1945 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1946 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001947 // Identifier referenced by #pragma weak before it was declared
1948 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001949 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1950 }
1951 }
1952 }
1953
Chris Lattner0744e5f2008-06-29 00:23:49 +00001954 // Apply decl attributes from the DeclSpec if present.
1955 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001956 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001957
Chris Lattner0744e5f2008-06-29 00:23:49 +00001958 // Walk the declarator structure, applying decl attributes that were in a type
1959 // position to the decl itself. This handles cases like:
1960 // int *__attr__(x)** D;
1961 // when X is a decl attribute.
1962 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1963 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001964 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001965
Chris Lattner0744e5f2008-06-29 00:23:49 +00001966 // Finally, apply any attributes on the decl itself.
1967 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001968 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001969}
John McCall54abf7d2009-11-04 02:18:39 +00001970
1971/// PushParsingDeclaration - Enter a new "scope" of deprecation
1972/// warnings.
1973///
1974/// The state token we use is the start index of this scope
1975/// on the warning stack.
1976Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
1977 ParsingDeclDepth++;
1978 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
1979}
1980
1981static bool isDeclDeprecated(Decl *D) {
1982 do {
1983 if (D->hasAttr<DeprecatedAttr>())
1984 return true;
1985 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
1986 return false;
1987}
1988
1989void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
1990 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
1991 ParsingDeclDepth--;
1992
1993 if (DelayedDeprecationWarnings.empty())
1994 return;
1995
1996 unsigned SavedIndex = (unsigned) S;
1997 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
1998 "saved index is out of bounds");
1999
2000 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2001 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2002 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2003 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2004 if (ND) {
2005 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2006
2007 // Prevent this from triggering multiple times.
2008 ND = 0;
2009 }
2010 }
2011 }
2012
2013 DelayedDeprecationWarnings.set_size(SavedIndex);
2014}
2015
2016void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2017 // Delay if we're currently parsing a declaration.
2018 if (ParsingDeclDepth) {
2019 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2020 return;
2021 }
2022
2023 // Otherwise, don't warn if our current context is deprecated.
2024 if (isDeclDeprecated(cast<Decl>(CurContext)))
2025 return;
2026
2027 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2028}