blob: a3f8eb5a0e7710f0f33aa5bfbb0b61c3be42e056 [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
Chris Lattner803d0802008-06-29 00:43:07 +00001035static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001036 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001037 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001039 return;
1040 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001041
1042 if (!isa<FunctionDecl>(d)) {
1043 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001044 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001045 return;
1046 }
1047
1048 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001049 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001050 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1051 << "fastcall" << "stdcall";
1052 return;
1053 }
1054
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001055 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001056}
1057
Chris Lattner803d0802008-06-29 00:43:07 +00001058static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001059 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001060 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001061 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001062 return;
1063 }
Mike Stumpbf916502009-07-24 19:02:52 +00001064
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001065 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001066}
1067
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001068static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1069 // check the attribute arguments.
1070 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001071 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001072 return;
1073 }
Mike Stumpbf916502009-07-24 19:02:52 +00001074
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001075 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001076}
1077
1078static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1079 // check the attribute arguments.
1080 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001081 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001082 return;
1083 }
Mike Stumpbf916502009-07-24 19:02:52 +00001084
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001085 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001086}
1087
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001088static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001089 // Match gcc which ignores cleanup attrs when compiling C++.
1090 if (S.getLangOptions().CPlusPlus)
1091 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001092
1093 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001094 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1095 return;
1096 }
Mike Stumpbf916502009-07-24 19:02:52 +00001097
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001098 if (Attr.getNumArgs() != 0) {
1099 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1100 return;
1101 }
Mike Stumpbf916502009-07-24 19:02:52 +00001102
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001103 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001104
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001105 if (!VD || !VD->hasLocalStorage()) {
1106 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1107 return;
1108 }
Mike Stumpbf916502009-07-24 19:02:52 +00001109
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001110 // Look up the function
John McCallf36e02d2009-10-09 21:13:30 +00001111 NamedDecl *CleanupDecl
1112 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1113 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001114 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001115 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001116 Attr.getParameterName();
1117 return;
1118 }
Mike Stumpbf916502009-07-24 19:02:52 +00001119
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001120 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1121 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001122 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001123 Attr.getParameterName();
1124 return;
1125 }
1126
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001127 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001128 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001129 Attr.getParameterName();
1130 return;
1131 }
Mike Stumpbf916502009-07-24 19:02:52 +00001132
Anders Carlsson89941c12009-02-07 23:16:50 +00001133 // We're currently more strict than GCC about what function types we accept.
1134 // If this ever proves to be a problem it should be easy to fix.
1135 QualType Ty = S.Context.getPointerType(VD->getType());
1136 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001137 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001138 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001139 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1140 Attr.getParameterName() << ParamTy << Ty;
1141 return;
1142 }
Mike Stumpbf916502009-07-24 19:02:52 +00001143
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001144 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001145}
1146
Mike Stumpbf916502009-07-24 19:02:52 +00001147/// Handle __attribute__((format_arg((idx)))) attribute based on
1148/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1149static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001150 if (Attr.getNumArgs() != 1) {
1151 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1152 return;
1153 }
1154 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1155 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1156 << Attr.getName() << 0 /*function*/;
1157 return;
1158 }
Mike Stumpbf916502009-07-24 19:02:52 +00001159 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1160 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001161 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1162 unsigned FirstIdx = 1;
1163 // checks for the 2nd argument
1164 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1165 llvm::APSInt Idx(32);
1166 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1167 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1168 << "format" << 2 << IdxExpr->getSourceRange();
1169 return;
1170 }
Mike Stumpbf916502009-07-24 19:02:52 +00001171
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001172 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1173 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1174 << "format" << 2 << IdxExpr->getSourceRange();
1175 return;
1176 }
Mike Stumpbf916502009-07-24 19:02:52 +00001177
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001178 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001179
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001180 // make sure the format string is really a string
1181 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001182
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001183 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1184 if (not_nsstring_type &&
1185 !isCFStringType(Ty, S.Context) &&
1186 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001187 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001188 // FIXME: Should highlight the actual expression that has the wrong type.
1189 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001190 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001191 << IdxExpr->getSourceRange();
1192 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001193 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001194 Ty = getFunctionOrMethodResultType(d);
1195 if (!isNSStringType(Ty, S.Context) &&
1196 !isCFStringType(Ty, S.Context) &&
1197 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001198 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001199 // FIXME: Should highlight the actual expression that has the wrong type.
1200 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001201 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001202 << IdxExpr->getSourceRange();
1203 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001204 }
1205
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001206 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001207}
1208
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001209enum FormatAttrKind {
1210 CFStringFormat,
1211 NSStringFormat,
1212 StrftimeFormat,
1213 SupportedFormat,
1214 InvalidFormat
1215};
1216
1217/// getFormatAttrKind - Map from format attribute names to supported format
1218/// types.
1219static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1220 // Check for formats that get handled specially.
1221 if (Format == "NSString")
1222 return NSStringFormat;
1223 if (Format == "CFString")
1224 return CFStringFormat;
1225 if (Format == "strftime")
1226 return StrftimeFormat;
1227
1228 // Otherwise, check for supported formats.
1229 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1230 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1231 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1232 Format == "zcmn_err")
1233 return SupportedFormat;
1234
1235 return InvalidFormat;
1236}
1237
Mike Stumpbf916502009-07-24 19:02:52 +00001238/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1239/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001240static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241
Chris Lattner545dd342008-06-28 23:36:30 +00001242 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001243 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001244 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001245 return;
1246 }
1247
Chris Lattner545dd342008-06-28 23:36:30 +00001248 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001249 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001250 return;
1251 }
1252
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001253 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001254 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001255 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001256 return;
1257 }
1258
Daniel Dunbar35682492008-09-26 04:12:28 +00001259 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260 unsigned FirstIdx = 1;
1261
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001262 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263
1264 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001265 if (Format.startswith("__") && Format.endswith("__"))
1266 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001267
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001268 // Check for supported formats.
1269 FormatAttrKind Kind = getFormatAttrKind(Format);
1270 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001271 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001272 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001273 return;
1274 }
1275
1276 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001277 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001278 llvm::APSInt Idx(32);
1279 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001280 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001281 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001282 return;
1283 }
1284
Anders Carlsson4fb77202009-08-25 14:12:34 +00001285 // FIXME: We should handle the implicit 'this' parameter in a more generic
1286 // way that can be used for other arguments.
1287 bool HasImplicitThisParam = false;
1288 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1289 if (MD->isInstance()) {
1290 HasImplicitThisParam = true;
1291 NumArgs++;
1292 }
1293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Chris Lattner6b6b5372008-06-26 18:38:35 +00001295 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001296 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001297 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001298 return;
1299 }
1300
1301 // FIXME: Do we need to bounds check?
1302 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001303
Anders Carlsson4fb77202009-08-25 14:12:34 +00001304 if (HasImplicitThisParam) ArgIdx--;
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Chris Lattner6b6b5372008-06-26 18:38:35 +00001306 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001307 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001308
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001309 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001310 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001311 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1312 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001313 return;
1314 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001315 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001316 // FIXME: do we need to check if the type is NSString*? What are the
1317 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001318 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001319 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001320 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1321 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001322 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001323 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001324 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001325 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001326 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001327 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1328 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001329 return;
1330 }
1331
1332 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001333 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001334 llvm::APSInt FirstArg(32);
1335 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001337 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001338 return;
1339 }
1340
1341 // check if the function is variadic if the 3rd argument non-zero
1342 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001343 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001344 ++NumArgs; // +1 for ...
1345 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001346 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347 return;
1348 }
1349 }
1350
Chris Lattner3c73c412008-11-19 08:23:25 +00001351 // strftime requires FirstArg to be 0 because it doesn't read from any
1352 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001353 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001354 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001355 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1356 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001357 return;
1358 }
1359 // if 0 it disables parameter checking (to use with e.g. va_list)
1360 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001361 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001362 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001363 return;
1364 }
1365
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001366 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1367 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001368}
1369
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001370static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1371 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001372 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001373 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001375 return;
1376 }
1377
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001378 // Try to find the underlying union declaration.
1379 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001380 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001381 if (TD && TD->getUnderlyingType()->isUnionType())
1382 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1383 else
1384 RD = dyn_cast<RecordDecl>(d);
1385
1386 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001387 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001388 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001389 return;
1390 }
1391
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001392 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001393 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001394 diag::warn_transparent_union_attribute_not_definition);
1395 return;
1396 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001397
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001398 RecordDecl::field_iterator Field = RD->field_begin(),
1399 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001400 if (Field == FieldEnd) {
1401 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1402 return;
1403 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001404
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001405 FieldDecl *FirstField = *Field;
1406 QualType FirstType = FirstField->getType();
1407 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001408 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001409 diag::warn_transparent_union_attribute_floating);
1410 return;
1411 }
1412
1413 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1414 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1415 for (; Field != FieldEnd; ++Field) {
1416 QualType FieldType = Field->getType();
1417 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1418 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1419 // Warn if we drop the attribute.
1420 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001421 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001422 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001423 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001424 diag::warn_transparent_union_attribute_field_size_align)
1425 << isSize << Field->getDeclName() << FieldBits;
1426 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001427 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001428 diag::note_transparent_union_first_field_size_align)
1429 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001430 return;
1431 }
1432 }
1433
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001434 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001435}
1436
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001437static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001438 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001439 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001440 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001441 return;
1442 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001443 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1444 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001445
Chris Lattner6b6b5372008-06-26 18:38:35 +00001446 // Make sure that there is a string literal as the annotation's single
1447 // argument.
1448 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001449 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001450 return;
1451 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001452 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001453 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001454}
1455
Chris Lattner803d0802008-06-29 00:43:07 +00001456static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001457 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001458 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001460 return;
1461 }
1462
1463 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001464 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001465 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001466 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001467 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001468 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001469 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001470 }
Mike Stumpbf916502009-07-24 19:02:52 +00001471
Chris Lattner49e2d342008-06-28 23:50:44 +00001472 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1473 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001474 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001475 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1476 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001477 return;
1478 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001479 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001480 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001481 << alignmentExpr->getSourceRange();
1482 return;
1483 }
1484
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001485 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001486}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001487
Mike Stumpbf916502009-07-24 19:02:52 +00001488/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1489/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001490///
Mike Stumpbf916502009-07-24 19:02:52 +00001491/// Despite what would be logical, the mode attribute is a decl attribute, not a
1492/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1493/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001494static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001495 // This attribute isn't documented, but glibc uses it. It changes
1496 // the width of an int or unsigned int to the specified size.
1497
1498 // Check that there aren't any arguments
1499 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001500 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001501 return;
1502 }
1503
1504 IdentifierInfo *Name = Attr.getParameterName();
1505 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001506 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001507 return;
1508 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001509
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001510 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001511
1512 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001513 if (Str.startswith("__") && Str.endswith("__"))
1514 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001515
1516 unsigned DestWidth = 0;
1517 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001518 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001519 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001520 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001521 switch (Str[0]) {
1522 case 'Q': DestWidth = 8; break;
1523 case 'H': DestWidth = 16; break;
1524 case 'S': DestWidth = 32; break;
1525 case 'D': DestWidth = 64; break;
1526 case 'X': DestWidth = 96; break;
1527 case 'T': DestWidth = 128; break;
1528 }
1529 if (Str[1] == 'F') {
1530 IntegerMode = false;
1531 } else if (Str[1] == 'C') {
1532 IntegerMode = false;
1533 ComplexMode = true;
1534 } else if (Str[1] != 'I') {
1535 DestWidth = 0;
1536 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001537 break;
1538 case 4:
1539 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1540 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001541 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001542 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001543 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001544 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001545 break;
1546 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001547 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001548 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001549 break;
1550 }
1551
1552 QualType OldTy;
1553 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1554 OldTy = TD->getUnderlyingType();
1555 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1556 OldTy = VD->getType();
1557 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001558 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1559 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001560 return;
1561 }
Eli Friedman73397492009-03-03 06:41:03 +00001562
John McCall183700f2009-09-21 23:43:11 +00001563 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001564 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1565 else if (IntegerMode) {
1566 if (!OldTy->isIntegralType())
1567 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1568 } else if (ComplexMode) {
1569 if (!OldTy->isComplexType())
1570 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1571 } else {
1572 if (!OldTy->isFloatingType())
1573 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1574 }
1575
Mike Stump390b4cc2009-05-16 07:39:55 +00001576 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1577 // and friends, at least with glibc.
1578 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1579 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001580 // FIXME: Make sure floating-point mappings are accurate
1581 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001582 QualType NewTy;
1583 switch (DestWidth) {
1584 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001585 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001586 return;
1587 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001588 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001589 return;
1590 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001591 if (!IntegerMode) {
1592 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1593 return;
1594 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001596 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001597 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001598 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001599 break;
1600 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001601 if (!IntegerMode) {
1602 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1603 return;
1604 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001605 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001606 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001607 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001608 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001609 break;
1610 case 32:
1611 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001612 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001613 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001614 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001615 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001616 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001617 break;
1618 case 64:
1619 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001620 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001621 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001622 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001623 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001624 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001625 break;
Eli Friedman73397492009-03-03 06:41:03 +00001626 case 96:
1627 NewTy = S.Context.LongDoubleTy;
1628 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001629 case 128:
1630 if (!IntegerMode) {
1631 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1632 return;
1633 }
1634 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001635 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001636 }
1637
Eli Friedman73397492009-03-03 06:41:03 +00001638 if (ComplexMode) {
1639 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001640 }
1641
1642 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001643 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1644 // FIXME: preserve existing source info.
1645 TD->setTypeDeclaratorInfo(S.Context.getTrivialDeclaratorInfo(NewTy));
1646 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001647 cast<ValueDecl>(D)->setType(NewTy);
1648}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001649
Mike Stump1feade82009-08-26 22:31:08 +00001650static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001651 // check the attribute arguments.
1652 if (Attr.getNumArgs() > 0) {
1653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1654 return;
1655 }
Anders Carlssone896d982009-02-13 08:11:52 +00001656
Anders Carlsson5bab7882009-02-19 19:16:48 +00001657 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001659 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001660 return;
1661 }
Mike Stumpbf916502009-07-24 19:02:52 +00001662
Mike Stump1feade82009-08-26 22:31:08 +00001663 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001664}
1665
Mike Stump1feade82009-08-26 22:31:08 +00001666static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001667 // check the attribute arguments.
1668 if (Attr.getNumArgs() != 0) {
1669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1670 return;
1671 }
Mike Stumpbf916502009-07-24 19:02:52 +00001672
Chris Lattnerc5197432009-04-14 17:02:11 +00001673 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001674 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001675 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001676 return;
1677 }
Mike Stumpbf916502009-07-24 19:02:52 +00001678
Mike Stump1feade82009-08-26 22:31:08 +00001679 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001680}
1681
Chris Lattnercf2a7212009-04-20 19:12:28 +00001682static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001683 // check the attribute arguments.
1684 if (Attr.getNumArgs() != 0) {
1685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1686 return;
1687 }
Mike Stumpbf916502009-07-24 19:02:52 +00001688
Chris Lattnerc5197432009-04-14 17:02:11 +00001689 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1690 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001692 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001693 return;
1694 }
Mike Stumpbf916502009-07-24 19:02:52 +00001695
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001696 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001697 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001698 return;
1699 }
Mike Stumpbf916502009-07-24 19:02:52 +00001700
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001701 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001702}
1703
Fariborz Jahanianee760332009-03-27 18:38:55 +00001704static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1705 // check the attribute arguments.
1706 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001707 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001708 return;
1709 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001710
Fariborz Jahanianee760332009-03-27 18:38:55 +00001711 if (!isFunctionOrMethod(d)) {
1712 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001713 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001714 return;
1715 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001716
1717 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1718 llvm::APSInt NumParams(32);
1719 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1720 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1721 << "regparm" << NumParamsExpr->getSourceRange();
1722 return;
1723 }
1724
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001725 if (S.Context.Target.getRegParmMax() == 0) {
1726 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001727 << NumParamsExpr->getSourceRange();
1728 return;
1729 }
1730
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001731 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001732 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1733 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001734 return;
1735 }
1736
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001737 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001738}
1739
Chris Lattner0744e5f2008-06-29 00:23:49 +00001740//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001741// Checker-specific attribute handlers.
1742//===----------------------------------------------------------------------===//
1743
1744static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1745 Sema &S) {
1746
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001747 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001748
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001749 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1750 RetTy = MD->getResultType();
1751 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1752 RetTy = FD->getResultType();
1753 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001754 SourceLocation L = Attr.getLoc();
1755 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1756 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001757 return;
1758 }
Mike Stumpbf916502009-07-24 19:02:52 +00001759
Ted Kremenek6217b802009-07-29 21:53:49 +00001760 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001761 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001762 SourceLocation L = Attr.getLoc();
1763 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1764 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001765 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001766 }
Mike Stumpbf916502009-07-24 19:02:52 +00001767
Ted Kremenekb71368d2009-05-09 02:44:38 +00001768 switch (Attr.getKind()) {
1769 default:
1770 assert(0 && "invalid ownership attribute");
1771 return;
1772 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001773 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001774 return;
1775 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001776 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001777 return;
1778 };
1779}
1780
1781//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001782// Top Level Sema Entry Points
1783//===----------------------------------------------------------------------===//
1784
Sebastian Redla89d82c2008-12-21 19:24:58 +00001785/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001786/// the attribute applies to decls. If the attribute is a type attribute, just
1787/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001788static void ProcessDeclAttribute(Scope *scope, Decl *D,
1789 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001790 if (Attr.isDeclspecAttribute())
1791 // FIXME: Try to deal with __declspec attributes!
1792 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001793 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001794 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001795 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001796 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001797 // Ignore these, these are type attributes, handled by
1798 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001799 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001800 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1801 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001802 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001803 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001804 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001805 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001806 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1807 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1808 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1809 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1810 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1811 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001812 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001813 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001814 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001815 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1816 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001817 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001818 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001819 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001820 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001821 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1822 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1823 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001824
1825 // Checker-specific.
1826 case AttributeList::AT_ns_returns_retained:
1827 case AttributeList::AT_cf_returns_retained:
1828 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1829
Nate Begeman6f3d8382009-06-26 06:32:41 +00001830 case AttributeList::AT_reqd_wg_size:
1831 HandleReqdWorkGroupSize(D, Attr, S); break;
1832
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001833 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001834 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001835 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001836 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001837 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001838 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001839 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001840 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001841 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1842 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001843 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001844 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001845 case AttributeList::AT_transparent_union:
1846 HandleTransparentUnionAttr(D, Attr, S);
1847 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001848 case AttributeList::AT_objc_exception:
1849 HandleObjCExceptionAttr(D, Attr, S);
1850 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001851 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001852 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001853 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001854 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001855 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1856 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001857 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Mike Stump1feade82009-08-26 22:31:08 +00001858 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1859 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001860 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001861 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001862 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001863 // Just ignore
1864 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001865 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001866 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001867 break;
1868 }
1869}
1870
1871/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1872/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001873void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001874 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001875 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001876 AttrList = AttrList->getNext();
1877 }
1878}
1879
Ryan Flynne25ff832009-07-30 03:15:39 +00001880/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1881/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001882NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001883 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001884 NamedDecl *NewD = 0;
1885 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1886 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1887 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001888 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001889 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1890 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1891 VD->getLocation(), II,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001892 VD->getType(), VD->getDeclaratorInfo(),
1893 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001894 }
1895 return NewD;
1896}
1897
1898/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1899/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001900void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001901 if (W.getUsed()) return; // only do this once
1902 W.setUsed(true);
1903 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1904 IdentifierInfo *NDId = ND->getIdentifier();
1905 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1906 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1907 NewD->addAttr(::new (Context) WeakAttr());
1908 WeakTopLevelDecl.push_back(NewD);
1909 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1910 // to insert Decl at TU scope, sorry.
1911 DeclContext *SavedContext = CurContext;
1912 CurContext = Context.getTranslationUnitDecl();
1913 PushOnScopeChains(NewD, S);
1914 CurContext = SavedContext;
1915 } else { // just add weak to existing
1916 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001917 }
1918}
1919
Chris Lattner0744e5f2008-06-29 00:23:49 +00001920/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1921/// it, apply them to D. This is a bit tricky because PD can have attributes
1922/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001923void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001924 // Handle #pragma weak
1925 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1926 if (ND->hasLinkage()) {
1927 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1928 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001929 // Identifier referenced by #pragma weak before it was declared
1930 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001931 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1932 }
1933 }
1934 }
1935
Chris Lattner0744e5f2008-06-29 00:23:49 +00001936 // Apply decl attributes from the DeclSpec if present.
1937 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001938 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001939
Chris Lattner0744e5f2008-06-29 00:23:49 +00001940 // Walk the declarator structure, applying decl attributes that were in a type
1941 // position to the decl itself. This handles cases like:
1942 // int *__attr__(x)** D;
1943 // when X is a decl attribute.
1944 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1945 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001946 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Chris Lattner0744e5f2008-06-29 00:23:49 +00001948 // Finally, apply any attributes on the decl itself.
1949 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001950 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001951}
John McCall54abf7d2009-11-04 02:18:39 +00001952
1953/// PushParsingDeclaration - Enter a new "scope" of deprecation
1954/// warnings.
1955///
1956/// The state token we use is the start index of this scope
1957/// on the warning stack.
1958Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
1959 ParsingDeclDepth++;
1960 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
1961}
1962
1963static bool isDeclDeprecated(Decl *D) {
1964 do {
1965 if (D->hasAttr<DeprecatedAttr>())
1966 return true;
1967 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
1968 return false;
1969}
1970
1971void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
1972 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
1973 ParsingDeclDepth--;
1974
1975 if (DelayedDeprecationWarnings.empty())
1976 return;
1977
1978 unsigned SavedIndex = (unsigned) S;
1979 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
1980 "saved index is out of bounds");
1981
1982 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
1983 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
1984 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
1985 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
1986 if (ND) {
1987 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
1988
1989 // Prevent this from triggering multiple times.
1990 ND = 0;
1991 }
1992 }
1993 }
1994
1995 DelayedDeprecationWarnings.set_size(SavedIndex);
1996}
1997
1998void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
1999 // Delay if we're currently parsing a declaration.
2000 if (ParsingDeclDepth) {
2001 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2002 return;
2003 }
2004
2005 // Otherwise, don't warn if our current context is deprecated.
2006 if (isDeclDeprecated(cast<Decl>(CurContext)))
2007 return;
2008
2009 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2010}