blob: 50ebb49e7d5b49222bfa49d8e0118454b2da6d46 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattner58418ff2008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Ted Kremenek527042b2009-08-14 20:49:40 +000027static const FunctionType *getFunctionType(const Decl *d,
28 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000029 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000030 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000032 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000034 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035 Ty = decl->getUnderlyingType();
36 else
37 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000038
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000040 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000041 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000042 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000043
John McCall9dd450b2009-09-21 23:43:11 +000044 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000045}
46
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000047// FIXME: We should provide an abstraction around a method or function
48// to provide the following bits of information.
49
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000050/// isFunctionOrMethod - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000051/// type (function or function-typed variable).
52static bool isFunction(const Decl *d) {
53 return getFunctionType(d, false) != NULL;
54}
55
56/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000057/// type (function or function-typed variable) or an Objective-C
58/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000059static bool isFunctionOrMethod(const Decl *d) {
60 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000061}
62
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063/// isFunctionOrMethodOrBlock - Return true if the given decl has function
64/// type (function or function-typed variable) or an Objective-C
65/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000066static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067 if (isFunctionOrMethod(d))
68 return true;
69 // check for block is more involved.
70 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
71 QualType Ty = V->getType();
72 return Ty->isBlockPointerType();
73 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000074 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000075}
76
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000077/// hasFunctionProto - Return true if the given decl has a argument
78/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000079/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000080static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000081 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000082 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000083 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000084 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000085 return true;
86 }
87}
88
89/// getFunctionOrMethodNumArgs - Return number of function or method
90/// arguments. It is an error to call this on a K&R function (use
91/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000092static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000093 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000094 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
96 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +000097 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000098}
99
Ted Kremenek527042b2009-08-14 20:49:40 +0000100static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000101 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000102 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000103 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
104 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000105
Chris Lattnera4997152009-02-20 18:43:26 +0000106 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000107}
108
Ted Kremenek527042b2009-08-14 20:49:40 +0000109static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000110 if (const FunctionType *FnTy = getFunctionType(d))
111 return cast<FunctionProtoType>(FnTy)->getResultType();
112 return cast<ObjCMethodDecl>(d)->getResultType();
113}
114
Ted Kremenek527042b2009-08-14 20:49:40 +0000115static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000116 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000118 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000119 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
120 return BD->IsVariadic();
121 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000122 return cast<ObjCMethodDecl>(d)->isVariadic();
123 }
124}
125
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000126static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000127 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000128 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000129 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000130
John McCall9dd450b2009-09-21 23:43:11 +0000131 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000132 if (!ClsT)
133 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000134
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000135 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000136
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000137 // FIXME: Should we walk the chain of classes?
138 return ClsName == &Ctx.Idents.get("NSString") ||
139 ClsName == &Ctx.Idents.get("NSMutableString");
140}
141
Daniel Dunbar980c6692008-09-26 03:32:58 +0000142static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000143 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000144 if (!PT)
145 return false;
146
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000147 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000148 if (!RT)
149 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000150
Daniel Dunbar980c6692008-09-26 03:32:58 +0000151 const RecordDecl *RD = RT->getDecl();
152 if (RD->getTagKind() != TagDecl::TK_struct)
153 return false;
154
155 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
156}
157
Chris Lattner58418ff2008-06-29 00:16:31 +0000158//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000159// Attribute Implementations
160//===----------------------------------------------------------------------===//
161
Daniel Dunbar032db472008-07-31 22:40:48 +0000162// FIXME: All this manual attribute parsing code is gross. At the
163// least add some helper functions to check most argument patterns (#
164// and types of args).
165
Mike Stumpd3bb5572009-07-24 19:02:52 +0000166static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000167 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000168 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
169 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000171 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000174 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000175
176 Expr *sizeExpr;
177
178 // Special case where the argument is a template id.
179 if (Attr.getParameterName()) {
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 Lattner2c6fcf52008-06-26 18:38:35 +0000190 }
Douglas Gregor758a8692009-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()) {
196 tDecl->setUnderlyingType(T);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000197
Douglas Gregor758a8692009-06-17 21:51:59 +0000198 // Remember this typedef decl, we will need it later for diagnostics.
199 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000200 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000201}
202
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000203
Mike Stumpd3bb5572009-07-24 19:02:52 +0000204/// HandleVectorSizeAttribute - this attribute is only applicable to integral
205/// and float scalars, although arrays, pointers, and function return values are
206/// allowed in conjunction with this construct. Aggregates with this attribute
207/// are invalid, even if they are of the same size as a corresponding scalar.
208/// The raw attribute should contain precisely 1 argument, the vector size for
209/// the variable, measured in bytes. If curType and rawAttr are well formed,
210/// this routine will return a new vector type.
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000211static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000212 QualType CurType;
213 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
214 CurType = VD->getType();
215 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
216 CurType = TD->getUnderlyingType();
217 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000218 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
219 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000220 return;
221 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000222
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000223 // Check the attribute arugments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000224 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000226 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000227 }
Chris Lattner4a927cb2008-06-28 23:36:30 +0000228 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000229 llvm::APSInt vecSize(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000230 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
232 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000233 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000234 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000235 // navigate to the base type - we need to provide for vector pointers, vector
236 // arrays, and functions returning vectors.
Chris Lattner574dee62008-07-26 22:17:49 +0000237 if (CurType->isPointerType() || CurType->isArrayType() ||
238 CurType->isFunctionType()) {
Chris Lattner851eb922009-05-13 04:00:12 +0000239 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
240 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000241 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
242 do {
243 if (PointerType *PT = dyn_cast<PointerType>(canonType))
244 canonType = PT->getPointeeType().getTypePtr();
245 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
246 canonType = AT->getElementType().getTypePtr();
247 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
248 canonType = FT->getResultType().getTypePtr();
249 } while (canonType->isPointerType() || canonType->isArrayType() ||
250 canonType->isFunctionType());
251 */
252 }
Chris Lattnerb70f3de2009-05-13 05:13:44 +0000253 // the base type must be integer or float, and can't already be a vector.
254 if (CurType->isVectorType() ||
255 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattner1e5665e2008-11-24 06:25:27 +0000256 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000257 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000258 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000259 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000260 // vecSize is specified in bytes - convert to bits.
Mike Stumpd3bb5572009-07-24 19:02:52 +0000261 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
262
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000263 // the vector size needs to be an integral multiple of the type size.
264 if (vectorSize % typeSize) {
Chris Lattner3b054132008-11-19 05:08:23 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
266 << sizeExpr->getSourceRange();
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000267 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000268 }
269 if (vectorSize == 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000270 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
271 << sizeExpr->getSourceRange();
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000272 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000273 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000274
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000275 // Success! Instantiate the vector type, the number of elements is > 0, and
276 // not required to be a power of 2, unlike GCC.
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000277 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000278
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000279 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
280 VD->setType(CurType);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000281 else
Chris Lattnered9cbbc2008-06-28 23:48:25 +0000282 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000283}
284
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000285static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000286 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000287 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000288 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000289 return;
290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000291
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000292 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlsson68e0b682009-08-08 18:23:56 +0000293 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000294 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
295 // If the alignment is less than or equal to 8 bits, the packed attribute
296 // has no effect.
297 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000298 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000299 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000300 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000301 else
Anders Carlsson68e0b682009-08-08 18:23:56 +0000302 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000303 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000304 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000305}
306
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000307static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
308 // check the attribute arguments.
309 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000311 return;
312 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000313
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000314 // The IBOutlet attribute only applies to instance variables of Objective-C
315 // classes.
Ted Kremenek2620a062009-02-17 22:20:20 +0000316 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000317 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000318 else
Ted Kremenek2620a062009-02-17 22:20:20 +0000319 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000320}
321
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000322static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000323 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
324 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000325 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000326 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000327 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000328 return;
329 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000330
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000331 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000332
333 // The nonnull attribute only applies to pointers.
334 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000335
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000336 for (AttributeList::arg_iterator I=Attr.arg_begin(),
337 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000338
339
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000340 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000341 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000342 llvm::APSInt ArgNum(32);
343 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
345 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000346 return;
347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000348
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000349 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000350
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000351 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000352 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000353 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000354 return;
355 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000356
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000357 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000358
359 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000360 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000361 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000362 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000363 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
364 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000365 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000366 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000367
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000368 NonNullArgs.push_back(x);
369 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000370
371 // If no arguments were specified to __attribute__((nonnull)) then all pointer
372 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000373 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000374 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
375 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000376 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000377 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000378 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000379
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000380 if (NonNullArgs.empty()) {
381 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
382 return;
383 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000384 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000385
386 unsigned* start = &NonNullArgs[0];
387 unsigned size = NonNullArgs.size();
388 std::sort(start, start + size);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000389 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000390}
391
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000392static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000393 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000394 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000396 return;
397 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000398
Chris Lattner4a927cb2008-06-28 23:36:30 +0000399 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000400 Arg = Arg->IgnoreParenCasts();
401 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000402
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000403 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000404 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000405 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000406 return;
407 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000408
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000409 const char *Alias = Str->getStrData();
410 unsigned AliasLen = Str->getByteLength();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000411
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000412 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000413
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000414 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000415}
416
Mike Stumpd3bb5572009-07-24 19:02:52 +0000417static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000418 Sema &S) {
419 // check the attribute arguments.
420 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000422 return;
423 }
Anders Carlsson88097122009-02-19 19:16:48 +0000424
Chris Lattner4225e232009-04-14 17:02:11 +0000425 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000426 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000427 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000428 return;
429 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000430
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000431 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000432}
433
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000434static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
435 // check the attribute arguments.
436 if (Attr.getNumArgs() != 0) {
437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
438 return;
439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Ted Kremenek08479ae2009-08-15 00:51:46 +0000441 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000442 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000443 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
444 d->addAttr(::new (S.Context) MallocAttr());
445 return;
446 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000447 }
448
Ted Kremenek08479ae2009-08-15 00:51:46 +0000449 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000450}
451
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000452static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000453 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000454 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000455 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000456 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000457 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000458 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000459
Mike Stump88788fe2009-04-29 19:03:13 +0000460 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
461 ValueDecl *VD = dyn_cast<ValueDecl>(d);
462 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000464 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000465 return false;
466 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000467 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000468
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000469 return true;
470}
471
472static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000473 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000474 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000475}
476
477static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
478 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000479 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000480 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000481}
482
Ted Kremenek39c59a82008-07-25 04:39:19 +0000483static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
484 // check the attribute arguments.
485 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000486 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000487 return;
488 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000489
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000490 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000491 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000492 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000493 return;
494 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000495
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000496 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000497}
498
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000499static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
500 // check the attribute arguments.
501 if (Attr.getNumArgs() != 0) {
502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
503 return;
504 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000505
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000506 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000507 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000508 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
509 return;
510 }
511 } else if (!isFunctionOrMethod(d)) {
512 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000513 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000514 return;
515 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000516
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000517 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000518}
519
Daniel Dunbar032db472008-07-31 22:40:48 +0000520static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
521 // check the attribute arguments.
522 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
524 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000525 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000526 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000527
528 int priority = 65535; // FIXME: Do not hardcode such constants.
529 if (Attr.getNumArgs() > 0) {
530 Expr *E = static_cast<Expr *>(Attr.getArg(0));
531 llvm::APSInt Idx(32);
532 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000533 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000534 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000535 return;
536 }
537 priority = Idx.getZExtValue();
538 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000539
Chris Lattner4225e232009-04-14 17:02:11 +0000540 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000541 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000542 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000543 return;
544 }
545
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000546 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000547}
548
549static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
550 // check the attribute arguments.
551 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
553 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000554 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000555 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000556
557 int priority = 65535; // FIXME: Do not hardcode such constants.
558 if (Attr.getNumArgs() > 0) {
559 Expr *E = static_cast<Expr *>(Attr.getArg(0));
560 llvm::APSInt Idx(32);
561 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000562 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000563 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000564 return;
565 }
566 priority = Idx.getZExtValue();
567 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000568
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000569 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000570 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000571 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000572 return;
573 }
574
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000575 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000576}
577
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000578static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000579 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000580 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000582 return;
583 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000584
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000585 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000586}
587
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000588static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
589 // check the attribute arguments.
590 if (Attr.getNumArgs() != 0) {
591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
592 return;
593 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000594
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000595 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000596}
597
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000598static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000599 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000600 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000602 return;
603 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000604
Chris Lattner4a927cb2008-06-28 23:36:30 +0000605 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000606 Arg = Arg->IgnoreParenCasts();
607 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000608
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000609 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000611 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000612 return;
613 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000614
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000615 const char *TypeStr = Str->getStrData();
616 unsigned TypeLen = Str->getByteLength();
617 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000618
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000619 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
620 type = VisibilityAttr::DefaultVisibility;
621 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
622 type = VisibilityAttr::HiddenVisibility;
623 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
624 type = VisibilityAttr::HiddenVisibility; // FIXME
625 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
626 type = VisibilityAttr::ProtectedVisibility;
627 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000628 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000629 return;
630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000631
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000632 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000633}
634
Chris Lattner677a3582009-02-14 08:09:34 +0000635static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
636 Sema &S) {
637 if (Attr.getNumArgs() != 0) {
638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
639 return;
640 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000641
Chris Lattner677a3582009-02-14 08:09:34 +0000642 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
643 if (OCI == 0) {
644 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
645 return;
646 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000647
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000648 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000649}
650
651static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000652 if (Attr.getNumArgs() != 0) {
653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
654 return;
655 }
Chris Lattner677a3582009-02-14 08:09:34 +0000656 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000657 QualType T = TD->getUnderlyingType();
658 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000659 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000660 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
661 return;
662 }
663 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000664 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000665}
666
Mike Stumpd3bb5572009-07-24 19:02:52 +0000667static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000668HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
669 if (Attr.getNumArgs() != 0) {
670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
671 return;
672 }
673
674 if (!isa<FunctionDecl>(D)) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
676 return;
677 }
678
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000679 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000680}
681
Steve Naroff3405a732008-09-18 16:44:58 +0000682static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000683 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000684 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000685 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000686 return;
687 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000688
Steve Naroff3405a732008-09-18 16:44:58 +0000689 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000691 return;
692 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000693
Steve Naroff3405a732008-09-18 16:44:58 +0000694 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000695 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000696 type = BlocksAttr::ByRef;
697 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000698 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000699 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000700 return;
701 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000702
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000703 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000704}
705
Anders Carlssonc181b012008-10-05 18:05:59 +0000706static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
707 // check the attribute arguments.
708 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000709 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
710 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000711 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000712 }
713
Anders Carlssonc181b012008-10-05 18:05:59 +0000714 int sentinel = 0;
715 if (Attr.getNumArgs() > 0) {
716 Expr *E = static_cast<Expr *>(Attr.getArg(0));
717 llvm::APSInt Idx(32);
718 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000720 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000721 return;
722 }
723 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000724
Anders Carlssonc181b012008-10-05 18:05:59 +0000725 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000726 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
727 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000728 return;
729 }
730 }
731
732 int nullPos = 0;
733 if (Attr.getNumArgs() > 1) {
734 Expr *E = static_cast<Expr *>(Attr.getArg(1));
735 llvm::APSInt Idx(32);
736 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000738 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000739 return;
740 }
741 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000742
Anders Carlssonc181b012008-10-05 18:05:59 +0000743 if (nullPos > 1 || nullPos < 0) {
744 // FIXME: This error message could be improved, it would be nice
745 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
747 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000748 return;
749 }
750 }
751
752 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000753 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000754 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000755
Chris Lattner9363e312009-03-17 23:03:47 +0000756 if (isa<FunctionNoProtoType>(FT)) {
757 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
758 return;
759 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000760
Chris Lattner9363e312009-03-17 23:03:47 +0000761 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000762 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000763 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000764 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000765 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
766 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000767 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000768 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000769 }
770 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000771 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
772 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000773 ;
774 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000775 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000776 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000777 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000778 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000779 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000780 int m = Ty->isFunctionPointerType() ? 0 : 1;
781 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000782 return;
783 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000784 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000785 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000786 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000787 return;
788 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000789 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000790 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000791 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000792 return;
793 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000794 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000795}
796
Chris Lattner237f2752009-02-14 07:37:35 +0000797static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
798 // check the attribute arguments.
799 if (Attr.getNumArgs() != 0) {
800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
801 return;
802 }
803
804 // TODO: could also be applied to methods?
805 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
806 if (!Fn) {
807 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000808 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000809 return;
810 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000811
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000812 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000813}
814
815static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000816 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000817 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000818 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000819 return;
820 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000821
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000822 /* weak only applies to non-static declarations */
823 bool isStatic = false;
824 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
825 isStatic = VD->getStorageClass() == VarDecl::Static;
826 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
827 isStatic = FD->getStorageClass() == FunctionDecl::Static;
828 }
829 if (isStatic) {
830 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
831 dyn_cast<NamedDecl>(D)->getNameAsString();
832 return;
833 }
834
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000835 // TODO: could also be applied to methods?
836 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000838 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000839 return;
840 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000841
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000842 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000843}
844
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000845static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
846 // check the attribute arguments.
847 if (Attr.getNumArgs() != 0) {
848 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
849 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000850 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000851
852 // weak_import only applies to variable & function declarations.
853 bool isDef = false;
854 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
855 isDef = (!VD->hasExternalStorage() || VD->getInit());
856 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000857 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000858 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
859 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000860 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000861 } else {
862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000863 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000864 return;
865 }
866
867 // Merge should handle any subsequent violations.
868 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000869 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000870 diag::warn_attribute_weak_import_invalid_on_definition)
871 << "weak_import" << 2 /*variable and function*/;
872 return;
873 }
874
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000875 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000876}
877
Chris Lattner237f2752009-02-14 07:37:35 +0000878static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000879 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000880 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000881 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000882 return;
883 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000884
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000885 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000886 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000887 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000888 return;
889 }
890
Chris Lattner237f2752009-02-14 07:37:35 +0000891 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000892 if (!FD) {
893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000894 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000895 return;
896 }
897
898 // Currently, the dllimport attribute is ignored for inlined functions.
899 // Warning is emitted.
900 if (FD->isInline()) {
901 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
902 return;
903 }
904
905 // The attribute is also overridden by a subsequent declaration as dllexport.
906 // Warning is emitted.
907 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
908 nextAttr = nextAttr->getNext()) {
909 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
910 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
911 return;
912 }
913 }
914
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000915 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
917 return;
918 }
919
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000920 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000921}
922
Chris Lattner237f2752009-02-14 07:37:35 +0000923static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000924 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000925 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000927 return;
928 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000929
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000930 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000931 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000932 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000933 return;
934 }
935
Chris Lattner237f2752009-02-14 07:37:35 +0000936 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000937 if (!FD) {
938 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000939 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000940 return;
941 }
942
Mike Stumpd3bb5572009-07-24 19:02:52 +0000943 // Currently, the dllexport attribute is ignored for inlined functions, unless
944 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000945 if (FD->isInline()) {
946 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
947 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
948 return;
949 }
950
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000951 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000952}
953
Nate Begemanf2758702009-06-26 06:32:41 +0000954static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
955 Sema &S) {
956 // Attribute has 3 arguments.
957 if (Attr.getNumArgs() != 3) {
958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
959 return;
960 }
961
962 unsigned WGSize[3];
963 for (unsigned i = 0; i < 3; ++i) {
964 Expr *E = static_cast<Expr *>(Attr.getArg(i));
965 llvm::APSInt ArgNum(32);
966 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
968 << "reqd_work_group_size" << E->getSourceRange();
969 return;
970 }
971 WGSize[i] = (unsigned) ArgNum.getZExtValue();
972 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000973 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000974 WGSize[2]));
975}
976
Chris Lattner237f2752009-02-14 07:37:35 +0000977static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000978 // Attribute has no arguments.
979 if (Attr.getNumArgs() != 1) {
980 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
981 return;
982 }
983
984 // Make sure that there is a string literal as the sections's single
985 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +0000986 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
987 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +0000988 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +0000989 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +0000990 return;
991 }
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattner30ba6742009-08-10 19:03:04 +0000993 std::string SectionStr(SE->getStrData(), SE->getByteLength());
994
995 // If the target wants to validate the section specifier, make it happen.
996 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
997 if (Error.empty()) {
998 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
999 return;
1000 }
Mike Stump11289f42009-09-09 15:08:12 +00001001
Chris Lattner30ba6742009-08-10 19:03:04 +00001002 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1003 << Error;
Mike Stump11289f42009-09-09 15:08:12 +00001004
Daniel Dunbar648bf782009-02-12 17:28:23 +00001005}
1006
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001007static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001008 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001009 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001011 return;
1012 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001013
1014 // Attribute can be applied only to functions.
1015 if (!isa<FunctionDecl>(d)) {
1016 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001017 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001018 return;
1019 }
1020
1021 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001022 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001023 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1024 << "stdcall" << "fastcall";
1025 return;
1026 }
1027
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001028 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001029}
1030
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001031static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001032 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001033 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001035 return;
1036 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001037
1038 if (!isa<FunctionDecl>(d)) {
1039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001040 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001041 return;
1042 }
1043
1044 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001045 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001046 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1047 << "fastcall" << "stdcall";
1048 return;
1049 }
1050
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001051 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001052}
1053
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001054static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001055 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001056 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001057 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001058 return;
1059 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001060
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001061 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001062}
1063
Anders Carlssonb8316282008-10-05 23:32:53 +00001064static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1065 // check the attribute arguments.
1066 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001067 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001068 return;
1069 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001070
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001071 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001072}
1073
1074static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1075 // check the attribute arguments.
1076 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001078 return;
1079 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001080
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001081 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001082}
1083
Anders Carlssond277d792009-01-31 01:16:18 +00001084static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001085 // Match gcc which ignores cleanup attrs when compiling C++.
1086 if (S.getLangOptions().CPlusPlus)
1087 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001088
1089 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1091 return;
1092 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001093
Anders Carlssond277d792009-01-31 01:16:18 +00001094 if (Attr.getNumArgs() != 0) {
1095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1096 return;
1097 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001098
Anders Carlssond277d792009-01-31 01:16:18 +00001099 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001100
Anders Carlssond277d792009-01-31 01:16:18 +00001101 if (!VD || !VD->hasLocalStorage()) {
1102 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1103 return;
1104 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001105
Anders Carlssond277d792009-01-31 01:16:18 +00001106 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +00001107 NamedDecl *CleanupDecl
1108 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1109 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001110 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001111 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001112 Attr.getParameterName();
1113 return;
1114 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001115
Anders Carlssond277d792009-01-31 01:16:18 +00001116 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1117 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001118 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001119 Attr.getParameterName();
1120 return;
1121 }
1122
Anders Carlssond277d792009-01-31 01:16:18 +00001123 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001124 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001125 Attr.getParameterName();
1126 return;
1127 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001128
Anders Carlsson723f55d2009-02-07 23:16:50 +00001129 // We're currently more strict than GCC about what function types we accept.
1130 // If this ever proves to be a problem it should be easy to fix.
1131 QualType Ty = S.Context.getPointerType(VD->getType());
1132 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001133 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001134 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001135 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1136 Attr.getParameterName() << ParamTy << Ty;
1137 return;
1138 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001139
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001140 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001141}
1142
Mike Stumpd3bb5572009-07-24 19:02:52 +00001143/// Handle __attribute__((format_arg((idx)))) attribute based on
1144/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1145static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001146 if (Attr.getNumArgs() != 1) {
1147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1148 return;
1149 }
1150 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1151 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1152 << Attr.getName() << 0 /*function*/;
1153 return;
1154 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1156 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001157 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1158 unsigned FirstIdx = 1;
1159 // checks for the 2nd argument
1160 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1161 llvm::APSInt Idx(32);
1162 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1163 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1164 << "format" << 2 << IdxExpr->getSourceRange();
1165 return;
1166 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001167
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001168 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1170 << "format" << 2 << IdxExpr->getSourceRange();
1171 return;
1172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001173
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001174 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001175
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001176 // make sure the format string is really a string
1177 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001178
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001179 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1180 if (not_nsstring_type &&
1181 !isCFStringType(Ty, S.Context) &&
1182 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001183 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001184 // FIXME: Should highlight the actual expression that has the wrong type.
1185 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001186 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001187 << IdxExpr->getSourceRange();
1188 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001189 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001190 Ty = getFunctionOrMethodResultType(d);
1191 if (!isNSStringType(Ty, S.Context) &&
1192 !isCFStringType(Ty, S.Context) &&
1193 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001194 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001195 // FIXME: Should highlight the actual expression that has the wrong type.
1196 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001197 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001198 << IdxExpr->getSourceRange();
1199 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001200 }
1201
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001202 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001203}
1204
Mike Stumpd3bb5572009-07-24 19:02:52 +00001205/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1206/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001207static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001208
Chris Lattner4a927cb2008-06-28 23:36:30 +00001209 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001210 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001211 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001212 return;
1213 }
1214
Chris Lattner4a927cb2008-06-28 23:36:30 +00001215 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001216 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001217 return;
1218 }
1219
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001220 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001221 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001222 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001223 return;
1224 }
1225
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001226 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001227 unsigned FirstIdx = 1;
1228
Chris Lattner4a927cb2008-06-28 23:36:30 +00001229 const char *Format = Attr.getParameterName()->getName();
1230 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001231
1232 // Normalize the argument, __foo__ becomes foo.
1233 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1234 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1235 Format += 2;
1236 FormatLen -= 4;
1237 }
1238
1239 bool Supported = false;
1240 bool is_NSString = false;
1241 bool is_strftime = false;
Daniel Dunbar980c6692008-09-26 03:32:58 +00001242 bool is_CFString = false;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001243
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001244 switch (FormatLen) {
1245 default: break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001246 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1247 case 6: Supported = !memcmp(Format, "printf", 6); break;
Ryan Flynnaa5e5fd2009-08-06 03:00:50 +00001248 case 7: Supported = !memcmp(Format, "printf0", 7) ||
Edward O'Callaghanf258caa2009-10-13 23:05:14 +00001249 !memcmp(Format, "strfmon", 7) ||
1250 !memcmp(Format, "cmn_err", 7); break;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001251 case 8:
Daniel Dunbar980c6692008-09-26 03:32:58 +00001252 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1253 (is_NSString = !memcmp(Format, "NSString", 8)) ||
Edward O'Callaghanf258caa2009-10-13 23:05:14 +00001254 !memcmp(Format, "vcmn_err", 8) ||
1255 !memcmp(Format, "zcmn_err", 8) ||
Daniel Dunbar980c6692008-09-26 03:32:58 +00001256 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001257 break;
1258 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001259
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001260 if (!Supported) {
Chris Lattner3b054132008-11-19 05:08:23 +00001261 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1262 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001263 return;
1264 }
1265
1266 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001267 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001268 llvm::APSInt Idx(32);
1269 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001271 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001272 return;
1273 }
1274
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001275 // FIXME: We should handle the implicit 'this' parameter in a more generic
1276 // way that can be used for other arguments.
1277 bool HasImplicitThisParam = false;
1278 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1279 if (MD->isInstance()) {
1280 HasImplicitThisParam = true;
1281 NumArgs++;
1282 }
1283 }
Mike Stump11289f42009-09-09 15:08:12 +00001284
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001285 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001287 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001288 return;
1289 }
1290
1291 // FIXME: Do we need to bounds check?
1292 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001293
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001294 if (HasImplicitThisParam) ArgIdx--;
Mike Stump11289f42009-09-09 15:08:12 +00001295
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001296 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001297 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001298
Daniel Dunbar980c6692008-09-26 03:32:58 +00001299 if (is_CFString) {
1300 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001301 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1302 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001303 return;
1304 }
1305 } else if (is_NSString) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001306 // FIXME: do we need to check if the type is NSString*? What are the
1307 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001308 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001309 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001310 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1311 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001312 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001313 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001314 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001315 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001316 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001317 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1318 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001319 return;
1320 }
1321
1322 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001323 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001324 llvm::APSInt FirstArg(32);
1325 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001327 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001328 return;
1329 }
1330
1331 // check if the function is variadic if the 3rd argument non-zero
1332 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001333 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001334 ++NumArgs; // +1 for ...
1335 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001336 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001337 return;
1338 }
1339 }
1340
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001341 // strftime requires FirstArg to be 0 because it doesn't read from any
1342 // variable the input is just the current time + the format string.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001343 if (is_strftime) {
1344 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001345 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1346 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001347 return;
1348 }
1349 // if 0 it disables parameter checking (to use with e.g. va_list)
1350 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001352 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001353 return;
1354 }
1355
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001356 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001357 Idx.getZExtValue(), FirstArg.getZExtValue()));
1358}
1359
Chris Lattnera663a0a2008-06-29 00:28:59 +00001360static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1361 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001362 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001363 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001364 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001365 return;
1366 }
1367
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001368 // Try to find the underlying union declaration.
1369 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001370 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001371 if (TD && TD->getUnderlyingType()->isUnionType())
1372 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1373 else
1374 RD = dyn_cast<RecordDecl>(d);
1375
1376 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001377 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001378 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001379 return;
1380 }
1381
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001382 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001383 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001384 diag::warn_transparent_union_attribute_not_definition);
1385 return;
1386 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001387
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001388 RecordDecl::field_iterator Field = RD->field_begin(),
1389 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001390 if (Field == FieldEnd) {
1391 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1392 return;
1393 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001394
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001395 FieldDecl *FirstField = *Field;
1396 QualType FirstType = FirstField->getType();
1397 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001398 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001399 diag::warn_transparent_union_attribute_floating);
1400 return;
1401 }
1402
1403 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1404 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1405 for (; Field != FieldEnd; ++Field) {
1406 QualType FieldType = Field->getType();
1407 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1408 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1409 // Warn if we drop the attribute.
1410 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001411 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001412 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001413 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001414 diag::warn_transparent_union_attribute_field_size_align)
1415 << isSize << Field->getDeclName() << FieldBits;
1416 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001417 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001418 diag::note_transparent_union_first_field_size_align)
1419 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001420 return;
1421 }
1422 }
1423
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001424 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001425}
1426
Chris Lattnera663a0a2008-06-29 00:28:59 +00001427static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001428 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001429 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001431 return;
1432 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001433 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1434 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001435
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001436 // Make sure that there is a string literal as the annotation's single
1437 // argument.
1438 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001439 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001440 return;
1441 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001442 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner9631e182009-03-04 06:34:08 +00001443 SE->getByteLength())));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001444}
1445
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001446static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001447 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001448 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001449 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001450 return;
1451 }
1452
1453 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001454 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001455 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001456 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001457 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001458 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001459 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001460 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001461
Chris Lattner4627b742008-06-28 23:50:44 +00001462 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1463 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001464 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001465 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1466 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001467 return;
1468 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001469 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001470 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001471 << alignmentExpr->getSourceRange();
1472 return;
1473 }
1474
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001475 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001476}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001477
Mike Stumpd3bb5572009-07-24 19:02:52 +00001478/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1479/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001480///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001481/// Despite what would be logical, the mode attribute is a decl attribute, not a
1482/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1483/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001484static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001485 // This attribute isn't documented, but glibc uses it. It changes
1486 // the width of an int or unsigned int to the specified size.
1487
1488 // Check that there aren't any arguments
1489 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001491 return;
1492 }
1493
1494 IdentifierInfo *Name = Attr.getParameterName();
1495 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001496 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001497 return;
1498 }
1499 const char *Str = Name->getName();
1500 unsigned Len = Name->getLength();
1501
1502 // Normalize the attribute name, __foo__ becomes foo.
1503 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1504 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1505 Str += 2;
1506 Len -= 4;
1507 }
1508
1509 unsigned DestWidth = 0;
1510 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001511 bool ComplexMode = false;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001512 switch (Len) {
1513 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001514 switch (Str[0]) {
1515 case 'Q': DestWidth = 8; break;
1516 case 'H': DestWidth = 16; break;
1517 case 'S': DestWidth = 32; break;
1518 case 'D': DestWidth = 64; break;
1519 case 'X': DestWidth = 96; break;
1520 case 'T': DestWidth = 128; break;
1521 }
1522 if (Str[1] == 'F') {
1523 IntegerMode = false;
1524 } else if (Str[1] == 'C') {
1525 IntegerMode = false;
1526 ComplexMode = true;
1527 } else if (Str[1] != 'I') {
1528 DestWidth = 0;
1529 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001530 break;
1531 case 4:
1532 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1533 // pointer on PIC16 and other embedded platforms.
1534 if (!memcmp(Str, "word", 4))
Chris Lattnera663a0a2008-06-29 00:28:59 +00001535 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001536 if (!memcmp(Str, "byte", 4))
Chris Lattnera663a0a2008-06-29 00:28:59 +00001537 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001538 break;
1539 case 7:
1540 if (!memcmp(Str, "pointer", 7))
Chris Lattnera663a0a2008-06-29 00:28:59 +00001541 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001542 break;
1543 }
1544
1545 QualType OldTy;
1546 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1547 OldTy = TD->getUnderlyingType();
1548 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1549 OldTy = VD->getType();
1550 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001551 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1552 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001553 return;
1554 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001555
John McCall9dd450b2009-09-21 23:43:11 +00001556 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001557 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1558 else if (IntegerMode) {
1559 if (!OldTy->isIntegralType())
1560 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1561 } else if (ComplexMode) {
1562 if (!OldTy->isComplexType())
1563 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1564 } else {
1565 if (!OldTy->isFloatingType())
1566 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1567 }
1568
Mike Stump87c57ac2009-05-16 07:39:55 +00001569 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1570 // and friends, at least with glibc.
1571 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1572 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001573 // FIXME: Make sure floating-point mappings are accurate
1574 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001575 QualType NewTy;
1576 switch (DestWidth) {
1577 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001578 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001579 return;
1580 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001581 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001582 return;
1583 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001584 if (!IntegerMode) {
1585 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1586 return;
1587 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001588 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001589 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001590 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001591 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001592 break;
1593 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001594 if (!IntegerMode) {
1595 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1596 return;
1597 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001598 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001599 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001600 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001601 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001602 break;
1603 case 32:
1604 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001605 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001606 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001607 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001608 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001609 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001610 break;
1611 case 64:
1612 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001613 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001614 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001615 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001616 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001617 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001618 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001619 case 96:
1620 NewTy = S.Context.LongDoubleTy;
1621 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001622 case 128:
1623 if (!IntegerMode) {
1624 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1625 return;
1626 }
1627 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman4735374e2009-03-03 06:41:03 +00001628 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001629 }
1630
Eli Friedman4735374e2009-03-03 06:41:03 +00001631 if (ComplexMode) {
1632 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001633 }
1634
1635 // Install the new type.
1636 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1637 TD->setUnderlyingType(NewTy);
1638 else
1639 cast<ValueDecl>(D)->setType(NewTy);
1640}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001641
Mike Stump3722f582009-08-26 22:31:08 +00001642static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001643 // check the attribute arguments.
1644 if (Attr.getNumArgs() > 0) {
1645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1646 return;
1647 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001648
Anders Carlsson88097122009-02-19 19:16:48 +00001649 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001650 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001651 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001652 return;
1653 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001654
Mike Stump3722f582009-08-26 22:31:08 +00001655 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001656}
1657
Mike Stump3722f582009-08-26 22:31:08 +00001658static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001659 // check the attribute arguments.
1660 if (Attr.getNumArgs() != 0) {
1661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1662 return;
1663 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001664
Chris Lattner4225e232009-04-14 17:02:11 +00001665 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001667 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001668 return;
1669 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001670
Mike Stump3722f582009-08-26 22:31:08 +00001671 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001672}
1673
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001674static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001675 // check the attribute arguments.
1676 if (Attr.getNumArgs() != 0) {
1677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1678 return;
1679 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001680
Chris Lattner4225e232009-04-14 17:02:11 +00001681 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1682 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001683 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001684 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001685 return;
1686 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001687
Chris Lattner4225e232009-04-14 17:02:11 +00001688 if (!Fn->isInline()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001689 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001690 return;
1691 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001692
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001693 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001694}
1695
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001696static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1697 // check the attribute arguments.
1698 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001699 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001700 return;
1701 }
Eli Friedman7044b762009-03-27 21:06:47 +00001702
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001703 if (!isFunctionOrMethod(d)) {
1704 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001705 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001706 return;
1707 }
Eli Friedman7044b762009-03-27 21:06:47 +00001708
1709 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1710 llvm::APSInt NumParams(32);
1711 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1712 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1713 << "regparm" << NumParamsExpr->getSourceRange();
1714 return;
1715 }
1716
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001717 if (S.Context.Target.getRegParmMax() == 0) {
1718 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001719 << NumParamsExpr->getSourceRange();
1720 return;
1721 }
1722
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001723 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001724 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1725 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001726 return;
1727 }
1728
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001729 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001730}
1731
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001732//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001733// Checker-specific attribute handlers.
1734//===----------------------------------------------------------------------===//
1735
1736static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1737 Sema &S) {
1738
Ted Kremenek3b204e42009-05-13 21:07:32 +00001739 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001740
Ted Kremenek3b204e42009-05-13 21:07:32 +00001741 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1742 RetTy = MD->getResultType();
1743 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1744 RetTy = FD->getResultType();
1745 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001746 SourceLocation L = Attr.getLoc();
1747 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1748 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001749 return;
1750 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001751
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001752 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001753 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001754 SourceLocation L = Attr.getLoc();
1755 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1756 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001757 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001758 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001759
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001760 switch (Attr.getKind()) {
1761 default:
1762 assert(0 && "invalid ownership attribute");
1763 return;
1764 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001765 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001766 return;
1767 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001768 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001769 return;
1770 };
1771}
1772
1773//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001774// Top Level Sema Entry Points
1775//===----------------------------------------------------------------------===//
1776
Sebastian Redlfc24b632008-12-21 19:24:58 +00001777/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001778/// the attribute applies to decls. If the attribute is a type attribute, just
1779/// silently ignore it.
Mike Stumpd3bb5572009-07-24 19:02:52 +00001780static void ProcessDeclAttribute(Scope *scope, Decl *D,
1781 const AttributeList &Attr, Sema &S) {
Eli Friedman53339e02009-06-08 23:27:34 +00001782 if (Attr.isDeclspecAttribute())
1783 // FIXME: Try to deal with __declspec attributes!
1784 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001785 switch (Attr.getKind()) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001786 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001787 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001788 case AttributeList::AT_objc_gc:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001789 // Ignore these, these are type attributes, handled by
1790 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001791 break;
Daniel Dunbar032db472008-07-31 22:40:48 +00001792 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1793 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001794 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001795 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001796 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001797 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar032db472008-07-31 22:40:48 +00001798 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1799 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1800 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1801 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1802 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1803 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001804 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001805 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001806 break;
Daniel Dunbar032db472008-07-31 22:40:48 +00001807 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1808 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001809 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001810 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001811 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001812 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar032db472008-07-31 22:40:48 +00001813 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1814 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1815 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001816
1817 // Checker-specific.
1818 case AttributeList::AT_ns_returns_retained:
1819 case AttributeList::AT_cf_returns_retained:
1820 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1821
Nate Begemanf2758702009-06-26 06:32:41 +00001822 case AttributeList::AT_reqd_wg_size:
1823 HandleReqdWorkGroupSize(D, Attr, S); break;
1824
Daniel Dunbar032db472008-07-31 22:40:48 +00001825 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar648bf782009-02-12 17:28:23 +00001826 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar032db472008-07-31 22:40:48 +00001827 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001828 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar032db472008-07-31 22:40:48 +00001829 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001830 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar032db472008-07-31 22:40:48 +00001831 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001832 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00001833 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1834 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001835 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001836 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001837 case AttributeList::AT_transparent_union:
1838 HandleTransparentUnionAttr(D, Attr, S);
1839 break;
Chris Lattner677a3582009-02-14 08:09:34 +00001840 case AttributeList::AT_objc_exception:
1841 HandleObjCExceptionAttr(D, Attr, S);
1842 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001843 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001844 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff3405a732008-09-18 16:44:58 +00001845 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlssonc181b012008-10-05 18:05:59 +00001846 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlssonb8316282008-10-05 23:32:53 +00001847 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1848 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssond277d792009-01-31 01:16:18 +00001849 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Mike Stump3722f582009-08-26 22:31:08 +00001850 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1851 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
Eli Friedman7044b762009-03-27 21:06:47 +00001852 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001853 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00001854 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00001855 // Just ignore
1856 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001857 default:
Anders Carlsson76187b42009-02-13 06:46:13 +00001858 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001859 break;
1860 }
1861}
1862
1863/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1864/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00001865void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001866 while (AttrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +00001867 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001868 AttrList = AttrList->getNext();
1869 }
1870}
1871
Ryan Flynn7d470f32009-07-30 03:15:39 +00001872/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1873/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00001874NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001875 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00001876 NamedDecl *NewD = 0;
1877 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1878 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1879 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001880 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001881 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1882 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1883 VD->getLocation(), II,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001884 VD->getType(), VD->getDeclaratorInfo(),
1885 VD->getStorageClass());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001886 }
1887 return NewD;
1888}
1889
1890/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1891/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00001892void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00001893 if (W.getUsed()) return; // only do this once
1894 W.setUsed(true);
1895 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1896 IdentifierInfo *NDId = ND->getIdentifier();
1897 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1898 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1899 NewD->addAttr(::new (Context) WeakAttr());
1900 WeakTopLevelDecl.push_back(NewD);
1901 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1902 // to insert Decl at TU scope, sorry.
1903 DeclContext *SavedContext = CurContext;
1904 CurContext = Context.getTranslationUnitDecl();
1905 PushOnScopeChains(NewD, S);
1906 CurContext = SavedContext;
1907 } else { // just add weak to existing
1908 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001909 }
1910}
1911
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001912/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1913/// it, apply them to D. This is a bit tricky because PD can have attributes
1914/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00001915void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00001916 // Handle #pragma weak
1917 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1918 if (ND->hasLinkage()) {
1919 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1920 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001921 // Identifier referenced by #pragma weak before it was declared
1922 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00001923 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1924 }
1925 }
1926 }
1927
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001928 // Apply decl attributes from the DeclSpec if present.
1929 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00001930 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001931
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001932 // Walk the declarator structure, applying decl attributes that were in a type
1933 // position to the decl itself. This handles cases like:
1934 // int *__attr__(x)** D;
1935 // when X is a decl attribute.
1936 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1937 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00001938 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001939
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001940 // Finally, apply any attributes on the decl itself.
1941 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00001942 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001943}