blob: 4952e629001ed1c70512827dab92130d282ca2ae [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Ted Kremeneka18d7d82009-08-14 20:49:40 +000027static const FunctionType *getFunctionType(const Decl *d,
28 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000030 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000031 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000032 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000033 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000034 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000035 Ty = decl->getUnderlyingType();
36 else
37 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000038
Chris Lattner6b6b5372008-06-26 18:38:35 +000039 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000040 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000041 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000042 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000043
44 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000045}
46
Daniel Dunbar35682492008-09-26 04:12:28 +000047// FIXME: We should provide an abstraction around a method or function
48// to provide the following bits of information.
49
Daniel Dunbard3f2c102008-10-19 02:04:16 +000050/// isFunctionOrMethod - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000051/// type (function or function-typed variable).
52static bool isFunction(const Decl *d) {
53 return getFunctionType(d, false) != NULL;
54}
55
56/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000057/// type (function or function-typed variable) or an Objective-C
58/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000059static bool isFunctionOrMethod(const Decl *d) {
60 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000061}
62
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000063/// isFunctionOrMethodOrBlock - Return true if the given decl has function
64/// type (function or function-typed variable) or an Objective-C
65/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000066static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000067 if (isFunctionOrMethod(d))
68 return true;
69 // check for block is more involved.
70 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
71 QualType Ty = V->getType();
72 return Ty->isBlockPointerType();
73 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000074 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000075}
76
Daniel Dunbard3f2c102008-10-19 02:04:16 +000077/// hasFunctionProto - Return true if the given decl has a argument
78/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000079/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000080static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000081 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000082 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000083 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000084 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000085 return true;
86 }
87}
88
89/// getFunctionOrMethodNumArgs - Return number of function or method
90/// arguments. It is an error to call this on a K&R function (use
91/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000092static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000093 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000094 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
96 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000097 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000098}
99
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000100static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000101 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000102 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000103 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
104 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000105
Chris Lattner89951a82009-02-20 18:43:26 +0000106 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000107}
108
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000109static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000110 if (const FunctionType *FnTy = getFunctionType(d))
111 return cast<FunctionProtoType>(FnTy)->getResultType();
112 return cast<ObjCMethodDecl>(d)->getResultType();
113}
114
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000115static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000116 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000118 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000119 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
120 return BD->IsVariadic();
121 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000122 return cast<ObjCMethodDecl>(d)->isVariadic();
123 }
124}
125
Chris Lattner6b6b5372008-06-26 18:38:35 +0000126static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Steve Naroff14108da2009-07-10 23:34:53 +0000127 const ObjCObjectPointerType *PT = T->getAsObjCObjectPointerType();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000128 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000129 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000130
Chris Lattnerb77792e2008-07-26 22:17:49 +0000131 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000132 if (!ClsT)
133 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000134
Chris Lattner6b6b5372008-06-26 18:38:35 +0000135 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000136
Chris Lattner6b6b5372008-06-26 18:38:35 +0000137 // FIXME: Should we walk the chain of classes?
138 return ClsName == &Ctx.Idents.get("NSString") ||
139 ClsName == &Ctx.Idents.get("NSMutableString");
140}
141
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000142static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000143 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000144 if (!PT)
145 return false;
146
Ted Kremenek6217b802009-07-29 21:53:49 +0000147 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000148 if (!RT)
149 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000150
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000151 const RecordDecl *RD = RT->getDecl();
152 if (RD->getTagKind() != TagDecl::TK_struct)
153 return false;
154
155 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
156}
157
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000158//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159// Attribute Implementations
160//===----------------------------------------------------------------------===//
161
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000162// FIXME: All this manual attribute parsing code is gross. At the
163// least add some helper functions to check most argument patterns (#
164// and types of args).
165
Mike Stumpbf916502009-07-24 19:02:52 +0000166static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000167 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000168 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
169 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000171 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 }
Mike Stumpbf916502009-07-24 19:02:52 +0000173
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000175
176 Expr *sizeExpr;
177
178 // Special case where the argument is a template id.
179 if (Attr.getParameterName()) {
180 sizeExpr = S.ActOnDeclarationNameExpr(scope, Attr.getLoc(),
181 Attr.getParameterName(),
182 false, 0, false).takeAs<Expr>();
183 } else {
184 // check the attribute arguments.
185 if (Attr.getNumArgs() != 1) {
186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
187 return;
188 }
189 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000190 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000191
192 // Instantiate/Install the vector type, and let Sema build the type for us.
193 // This will run the reguired checks.
194 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
195 if (!T.isNull()) {
196 tDecl->setUnderlyingType(T);
Mike Stumpbf916502009-07-24 19:02:52 +0000197
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000198 // Remember this typedef decl, we will need it later for diagnostics.
199 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000200 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000201}
202
Chris Lattner065c5a82008-06-28 23:48:25 +0000203
Mike Stumpbf916502009-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 Lattner803d0802008-06-29 00:43:07 +0000211static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000218 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
219 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000220 return;
221 }
Mike Stumpbf916502009-07-24 19:02:52 +0000222
Chris Lattner065c5a82008-06-28 23:48:25 +0000223 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000224 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000226 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000227 }
Chris Lattner545dd342008-06-28 23:36:30 +0000228 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000229 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000230 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
232 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000233 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000234 }
Mike Stumpbf916502009-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 Lattnerb77792e2008-07-26 22:17:49 +0000237 if (CurType->isPointerType() || CurType->isArrayType() ||
238 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000239 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
240 return;
Chris Lattner6b6b5372008-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 Lattner82afa2d2009-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 Lattnerd1625842008-11-24 06:25:27 +0000256 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000257 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000258 }
Chris Lattner803d0802008-06-29 00:43:07 +0000259 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000260 // vecSize is specified in bytes - convert to bits.
Mike Stumpbf916502009-07-24 19:02:52 +0000261 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
262
Chris Lattner6b6b5372008-06-26 18:38:35 +0000263 // the vector size needs to be an integral multiple of the type size.
264 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
266 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000267 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000268 }
269 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000270 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
271 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000272 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000273 }
Mike Stumpbf916502009-07-24 19:02:52 +0000274
Chris Lattner065c5a82008-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 Lattner803d0802008-06-29 00:43:07 +0000277 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Mike Stumpbf916502009-07-24 19:02:52 +0000278
Chris Lattner065c5a82008-06-28 23:48:25 +0000279 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
280 VD->setType(CurType);
Mike Stumpbf916502009-07-24 19:02:52 +0000281 else
Chris Lattner065c5a82008-06-28 23:48:25 +0000282 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000283}
284
Chris Lattner803d0802008-06-29 00:43:07 +0000285static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000286 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000287 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000288 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000289 return;
290 }
Mike Stumpbf916502009-07-24 19:02:52 +0000291
Chris Lattner6b6b5372008-06-26 18:38:35 +0000292 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000293 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-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 Lattner803d0802008-06-29 00:43:07 +0000298 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000299 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000300 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000301 else
Anders Carlssona860e752009-08-08 18:23:56 +0000302 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000303 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000304 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000305}
306
Ted Kremenek96329d42008-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 Lattner3c73c412008-11-19 08:23:25 +0000310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000311 return;
312 }
Mike Stumpbf916502009-07-24 19:02:52 +0000313
Ted Kremenek96329d42008-07-15 22:26:48 +0000314 // The IBOutlet attribute only applies to instance variables of Objective-C
315 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000316 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000317 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000318 else
Ted Kremenek32742602009-02-17 22:20:20 +0000319 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000320}
321
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000322static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-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 Dunbard3f2c102008-10-19 02:04:16 +0000325 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000326 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000327 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000328 return;
329 }
Mike Stumpbf916502009-07-24 19:02:52 +0000330
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000331 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332
333 // The nonnull attribute only applies to pointers.
334 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000335
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336 for (AttributeList::arg_iterator I=Attr.arg_begin(),
337 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000338
339
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000341 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000342 llvm::APSInt ArgNum(32);
343 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
345 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000346 return;
347 }
Mike Stumpbf916502009-07-24 19:02:52 +0000348
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000349 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000350
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000352 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000353 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000354 return;
355 }
Mike Stumpbf916502009-07-24 19:02:52 +0000356
Ted Kremenek465172f2008-07-21 22:09:15 +0000357 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000358
359 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000360 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000361 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000362 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000363 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
364 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000365 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000366 }
Mike Stumpbf916502009-07-24 19:02:52 +0000367
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000368 NonNullArgs.push_back(x);
369 }
Mike Stumpbf916502009-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 Kremenek7fb43c12008-09-01 19:57:52 +0000373 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000374 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
375 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000376 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000377 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000378 }
Mike Stumpbf916502009-07-24 19:02:52 +0000379
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000380 if (NonNullArgs.empty()) {
381 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
382 return;
383 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000384 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000385
386 unsigned* start = &NonNullArgs[0];
387 unsigned size = NonNullArgs.size();
388 std::sort(start, start + size);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000389 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000390}
391
Chris Lattner803d0802008-06-29 00:43:07 +0000392static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000393 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000394 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000396 return;
397 }
Mike Stumpbf916502009-07-24 19:02:52 +0000398
Chris Lattner545dd342008-06-28 23:36:30 +0000399 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000400 Arg = Arg->IgnoreParenCasts();
401 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000402
Chris Lattner6b6b5372008-06-26 18:38:35 +0000403 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000404 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000405 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000406 return;
407 }
Mike Stumpbf916502009-07-24 19:02:52 +0000408
Chris Lattner6b6b5372008-06-26 18:38:35 +0000409 const char *Alias = Str->getStrData();
410 unsigned AliasLen = Str->getByteLength();
Mike Stumpbf916502009-07-24 19:02:52 +0000411
Chris Lattner6b6b5372008-06-26 18:38:35 +0000412 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000413
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000414 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000415}
416
Mike Stumpbf916502009-07-24 19:02:52 +0000417static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000418 Sema &S) {
419 // check the attribute arguments.
420 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000422 return;
423 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000424
Chris Lattnerc5197432009-04-14 17:02:11 +0000425 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000426 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000427 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000428 return;
429 }
Mike Stumpbf916502009-07-24 19:02:52 +0000430
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000431 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000432}
433
Ryan Flynn76168e22009-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 }
440
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000441 if (!isFunction(d)) {
Ryan Flynn76168e22009-08-09 20:07:29 +0000442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
443 << Attr.getName() << 0 /*function*/;
444 return;
445 }
446
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000447 const FunctionDecl *FD = cast<FunctionDecl>(d);
448 QualType RetTy = FD->getResultType();
449
450 if (!(RetTy->isAnyPointerType() || RetTy->isBlockPointerType())) {
451 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
452 return;
Ryan Flynnfd6ad3c2009-08-09 22:36:29 +0000453 }
454
Ryan Flynn76168e22009-08-09 20:07:29 +0000455 d->addAttr(::new (S.Context) MallocAttr());
456}
457
Ted Kremenekb7252322009-04-10 00:01:14 +0000458static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000459 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000460 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000461 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000462 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000463 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000464 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000465
Mike Stump19c30c02009-04-29 19:03:13 +0000466 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
467 ValueDecl *VD = dyn_cast<ValueDecl>(d);
468 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
469 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000470 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000471 return false;
472 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000473 }
Mike Stumpbf916502009-07-24 19:02:52 +0000474
Ted Kremenekb7252322009-04-10 00:01:14 +0000475 return true;
476}
477
478static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000479 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000480 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000481}
482
483static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
484 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000485 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000486 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000487}
488
Ted Kremenek73798892008-07-25 04:39:19 +0000489static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
490 // check the attribute arguments.
491 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000493 return;
494 }
Mike Stumpbf916502009-07-24 19:02:52 +0000495
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000496 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000497 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000498 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000499 return;
500 }
Mike Stumpbf916502009-07-24 19:02:52 +0000501
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000502 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000503}
504
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000505static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
506 // check the attribute arguments.
507 if (Attr.getNumArgs() != 0) {
508 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
509 return;
510 }
Mike Stumpbf916502009-07-24 19:02:52 +0000511
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000512 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000513 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000514 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
515 return;
516 }
517 } else if (!isFunctionOrMethod(d)) {
518 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000519 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000520 return;
521 }
Mike Stumpbf916502009-07-24 19:02:52 +0000522
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000523 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000524}
525
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000526static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
527 // check the attribute arguments.
528 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000529 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
530 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000531 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000532 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000533
534 int priority = 65535; // FIXME: Do not hardcode such constants.
535 if (Attr.getNumArgs() > 0) {
536 Expr *E = static_cast<Expr *>(Attr.getArg(0));
537 llvm::APSInt Idx(32);
538 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000540 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000541 return;
542 }
543 priority = Idx.getZExtValue();
544 }
Mike Stumpbf916502009-07-24 19:02:52 +0000545
Chris Lattnerc5197432009-04-14 17:02:11 +0000546 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000547 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000548 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000549 return;
550 }
551
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000552 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000553}
554
555static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
556 // check the attribute arguments.
557 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000558 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
559 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000560 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000561 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000562
563 int priority = 65535; // FIXME: Do not hardcode such constants.
564 if (Attr.getNumArgs() > 0) {
565 Expr *E = static_cast<Expr *>(Attr.getArg(0));
566 llvm::APSInt Idx(32);
567 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000568 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000569 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000570 return;
571 }
572 priority = Idx.getZExtValue();
573 }
Mike Stumpbf916502009-07-24 19:02:52 +0000574
Anders Carlsson6782fc62008-08-22 22:10:48 +0000575 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000576 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000577 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000578 return;
579 }
580
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000581 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000582}
583
Chris Lattner803d0802008-06-29 00:43:07 +0000584static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000585 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000586 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000588 return;
589 }
Mike Stumpbf916502009-07-24 19:02:52 +0000590
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000591 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000592}
593
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000594static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
595 // check the attribute arguments.
596 if (Attr.getNumArgs() != 0) {
597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
598 return;
599 }
Mike Stumpbf916502009-07-24 19:02:52 +0000600
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000601 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000602}
603
Chris Lattner803d0802008-06-29 00:43:07 +0000604static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000605 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000606 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000607 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000608 return;
609 }
Mike Stumpbf916502009-07-24 19:02:52 +0000610
Chris Lattner545dd342008-06-28 23:36:30 +0000611 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000612 Arg = Arg->IgnoreParenCasts();
613 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000614
Chris Lattner6b6b5372008-06-26 18:38:35 +0000615 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000616 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000617 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000618 return;
619 }
Mike Stumpbf916502009-07-24 19:02:52 +0000620
Chris Lattner6b6b5372008-06-26 18:38:35 +0000621 const char *TypeStr = Str->getStrData();
622 unsigned TypeLen = Str->getByteLength();
623 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000624
Chris Lattner6b6b5372008-06-26 18:38:35 +0000625 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
626 type = VisibilityAttr::DefaultVisibility;
627 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
628 type = VisibilityAttr::HiddenVisibility;
629 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
630 type = VisibilityAttr::HiddenVisibility; // FIXME
631 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
632 type = VisibilityAttr::ProtectedVisibility;
633 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000634 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000635 return;
636 }
Mike Stumpbf916502009-07-24 19:02:52 +0000637
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000638 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000639}
640
Chris Lattner0db29ec2009-02-14 08:09:34 +0000641static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
642 Sema &S) {
643 if (Attr.getNumArgs() != 0) {
644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
645 return;
646 }
Mike Stumpbf916502009-07-24 19:02:52 +0000647
Chris Lattner0db29ec2009-02-14 08:09:34 +0000648 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
649 if (OCI == 0) {
650 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
651 return;
652 }
Mike Stumpbf916502009-07-24 19:02:52 +0000653
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000654 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000655}
656
657static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000658 if (Attr.getNumArgs() != 0) {
659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
660 return;
661 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000662 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000663 QualType T = TD->getUnderlyingType();
664 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000665 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000666 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
667 return;
668 }
669 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000670 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000671}
672
Mike Stumpbf916502009-07-24 19:02:52 +0000673static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000674HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
675 if (Attr.getNumArgs() != 0) {
676 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
677 return;
678 }
679
680 if (!isa<FunctionDecl>(D)) {
681 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
682 return;
683 }
684
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000685 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000686}
687
Steve Naroff9eae5762008-09-18 16:44:58 +0000688static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000689 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000691 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000692 return;
693 }
Mike Stumpbf916502009-07-24 19:02:52 +0000694
Steve Naroff9eae5762008-09-18 16:44:58 +0000695 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000696 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000697 return;
698 }
Mike Stumpbf916502009-07-24 19:02:52 +0000699
Steve Naroff9eae5762008-09-18 16:44:58 +0000700 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000701 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000702 type = BlocksAttr::ByRef;
703 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000704 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000705 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000706 return;
707 }
Mike Stumpbf916502009-07-24 19:02:52 +0000708
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000709 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000710}
711
Anders Carlsson77091822008-10-05 18:05:59 +0000712static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
713 // check the attribute arguments.
714 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000715 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
716 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000717 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000718 }
719
Anders Carlsson77091822008-10-05 18:05:59 +0000720 int sentinel = 0;
721 if (Attr.getNumArgs() > 0) {
722 Expr *E = static_cast<Expr *>(Attr.getArg(0));
723 llvm::APSInt Idx(32);
724 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000725 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000726 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000727 return;
728 }
729 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000730
Anders Carlsson77091822008-10-05 18:05:59 +0000731 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000732 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
733 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000734 return;
735 }
736 }
737
738 int nullPos = 0;
739 if (Attr.getNumArgs() > 1) {
740 Expr *E = static_cast<Expr *>(Attr.getArg(1));
741 llvm::APSInt Idx(32);
742 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000743 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000744 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000745 return;
746 }
747 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000748
Anders Carlsson77091822008-10-05 18:05:59 +0000749 if (nullPos > 1 || nullPos < 0) {
750 // FIXME: This error message could be improved, it would be nice
751 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
753 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000754 return;
755 }
756 }
757
758 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000759 const FunctionType *FT = FD->getType()->getAsFunctionType();
760 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000761
Chris Lattner897cd902009-03-17 23:03:47 +0000762 if (isa<FunctionNoProtoType>(FT)) {
763 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
764 return;
765 }
Mike Stumpbf916502009-07-24 19:02:52 +0000766
Chris Lattner897cd902009-03-17 23:03:47 +0000767 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000768 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000769 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000770 }
Anders Carlsson77091822008-10-05 18:05:59 +0000771 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
772 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000773 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000774 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000775 }
776 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000777 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
778 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000779 ;
780 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000781 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000782 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000783 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Ted Kremenek6217b802009-07-29 21:53:49 +0000784 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000785 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000786 int m = Ty->isFunctionPointerType() ? 0 : 1;
787 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000788 return;
789 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000790 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000791 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000792 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000793 return;
794 }
Anders Carlsson77091822008-10-05 18:05:59 +0000795 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000797 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000798 return;
799 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000800 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000801}
802
Chris Lattner026dc962009-02-14 07:37:35 +0000803static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
804 // check the attribute arguments.
805 if (Attr.getNumArgs() != 0) {
806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
807 return;
808 }
809
810 // TODO: could also be applied to methods?
811 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
812 if (!Fn) {
813 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000814 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000815 return;
816 }
Mike Stumpbf916502009-07-24 19:02:52 +0000817
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000818 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000819}
820
821static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000822 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000823 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000824 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000825 return;
826 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000827
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000828 /* weak only applies to non-static declarations */
829 bool isStatic = false;
830 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
831 isStatic = VD->getStorageClass() == VarDecl::Static;
832 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
833 isStatic = FD->getStorageClass() == FunctionDecl::Static;
834 }
835 if (isStatic) {
836 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
837 dyn_cast<NamedDecl>(D)->getNameAsString();
838 return;
839 }
840
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000841 // TODO: could also be applied to methods?
842 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
843 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000844 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000845 return;
846 }
Mike Stumpbf916502009-07-24 19:02:52 +0000847
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000848 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000849}
850
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000851static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
852 // check the attribute arguments.
853 if (Attr.getNumArgs() != 0) {
854 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
855 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000856 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000857
858 // weak_import only applies to variable & function declarations.
859 bool isDef = false;
860 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
861 isDef = (!VD->hasExternalStorage() || VD->getInit());
862 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000863 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000864 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
865 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000866 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000867 } else {
868 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000869 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000870 return;
871 }
872
873 // Merge should handle any subsequent violations.
874 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000875 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000876 diag::warn_attribute_weak_import_invalid_on_definition)
877 << "weak_import" << 2 /*variable and function*/;
878 return;
879 }
880
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000881 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000882}
883
Chris Lattner026dc962009-02-14 07:37:35 +0000884static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000885 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000886 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000888 return;
889 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000890
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000891 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000892 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000893 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000894 return;
895 }
896
Chris Lattner026dc962009-02-14 07:37:35 +0000897 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000898 if (!FD) {
899 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000900 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000901 return;
902 }
903
904 // Currently, the dllimport attribute is ignored for inlined functions.
905 // Warning is emitted.
906 if (FD->isInline()) {
907 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
908 return;
909 }
910
911 // The attribute is also overridden by a subsequent declaration as dllexport.
912 // Warning is emitted.
913 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
914 nextAttr = nextAttr->getNext()) {
915 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
917 return;
918 }
919 }
920
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000921 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000922 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
923 return;
924 }
925
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000926 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000927}
928
Chris Lattner026dc962009-02-14 07:37:35 +0000929static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000930 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000931 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000932 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000933 return;
934 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000935
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000936 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000937 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000938 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000939 return;
940 }
941
Chris Lattner026dc962009-02-14 07:37:35 +0000942 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000943 if (!FD) {
944 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000945 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000946 return;
947 }
948
Mike Stumpbf916502009-07-24 19:02:52 +0000949 // Currently, the dllexport attribute is ignored for inlined functions, unless
950 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000951 if (FD->isInline()) {
952 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
953 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
954 return;
955 }
956
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000957 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000958}
959
Nate Begeman6f3d8382009-06-26 06:32:41 +0000960static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
961 Sema &S) {
962 // Attribute has 3 arguments.
963 if (Attr.getNumArgs() != 3) {
964 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
965 return;
966 }
967
968 unsigned WGSize[3];
969 for (unsigned i = 0; i < 3; ++i) {
970 Expr *E = static_cast<Expr *>(Attr.getArg(i));
971 llvm::APSInt ArgNum(32);
972 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
973 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
974 << "reqd_work_group_size" << E->getSourceRange();
975 return;
976 }
977 WGSize[i] = (unsigned) ArgNum.getZExtValue();
978 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000979 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000980 WGSize[2]));
981}
982
Chris Lattner026dc962009-02-14 07:37:35 +0000983static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000984 // Attribute has no arguments.
985 if (Attr.getNumArgs() != 1) {
986 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
987 return;
988 }
989
990 // Make sure that there is a string literal as the sections's single
991 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000992 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
993 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000994 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000995 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000996 return;
997 }
Chris Lattner797c3c42009-08-10 19:03:04 +0000998
999 std::string SectionStr(SE->getStrData(), SE->getByteLength());
1000
1001 // If the target wants to validate the section specifier, make it happen.
1002 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
1003 if (Error.empty()) {
1004 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
1005 return;
1006 }
1007
1008 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1009 << Error;
1010
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001011}
1012
Chris Lattner803d0802008-06-29 00:43:07 +00001013static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001014 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001015 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001016 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001017 return;
1018 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001019
1020 // Attribute can be applied only to functions.
1021 if (!isa<FunctionDecl>(d)) {
1022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001023 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001024 return;
1025 }
1026
1027 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001028 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001029 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1030 << "stdcall" << "fastcall";
1031 return;
1032 }
1033
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001034 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001035}
1036
Chris Lattner803d0802008-06-29 00:43:07 +00001037static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001038 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001039 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001041 return;
1042 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001043
1044 if (!isa<FunctionDecl>(d)) {
1045 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001046 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001047 return;
1048 }
1049
1050 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001051 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001052 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1053 << "fastcall" << "stdcall";
1054 return;
1055 }
1056
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001057 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001058}
1059
Chris Lattner803d0802008-06-29 00:43:07 +00001060static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001061 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001062 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001063 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001064 return;
1065 }
Mike Stumpbf916502009-07-24 19:02:52 +00001066
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001067 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001068}
1069
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001070static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1071 // check the attribute arguments.
1072 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001073 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001074 return;
1075 }
Mike Stumpbf916502009-07-24 19:02:52 +00001076
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001077 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001078}
1079
1080static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1081 // check the attribute arguments.
1082 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001083 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001084 return;
1085 }
Mike Stumpbf916502009-07-24 19:02:52 +00001086
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001087 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001088}
1089
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001090static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001091 // Match gcc which ignores cleanup attrs when compiling C++.
1092 if (S.getLangOptions().CPlusPlus)
1093 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001094
1095 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001096 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1097 return;
1098 }
Mike Stumpbf916502009-07-24 19:02:52 +00001099
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001100 if (Attr.getNumArgs() != 0) {
1101 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1102 return;
1103 }
Mike Stumpbf916502009-07-24 19:02:52 +00001104
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001105 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001106
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001107 if (!VD || !VD->hasLocalStorage()) {
1108 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1109 return;
1110 }
Mike Stumpbf916502009-07-24 19:02:52 +00001111
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001112 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001113 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1114 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001115 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001116 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001117 Attr.getParameterName();
1118 return;
1119 }
Mike Stumpbf916502009-07-24 19:02:52 +00001120
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001121 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1122 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001123 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001124 Attr.getParameterName();
1125 return;
1126 }
1127
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001128 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001129 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001130 Attr.getParameterName();
1131 return;
1132 }
Mike Stumpbf916502009-07-24 19:02:52 +00001133
Anders Carlsson89941c12009-02-07 23:16:50 +00001134 // We're currently more strict than GCC about what function types we accept.
1135 // If this ever proves to be a problem it should be easy to fix.
1136 QualType Ty = S.Context.getPointerType(VD->getType());
1137 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001138 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001139 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001140 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1141 Attr.getParameterName() << ParamTy << Ty;
1142 return;
1143 }
Mike Stumpbf916502009-07-24 19:02:52 +00001144
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001145 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001146}
1147
Mike Stumpbf916502009-07-24 19:02:52 +00001148/// Handle __attribute__((format_arg((idx)))) attribute based on
1149/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1150static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001151 if (Attr.getNumArgs() != 1) {
1152 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1153 return;
1154 }
1155 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1156 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1157 << Attr.getName() << 0 /*function*/;
1158 return;
1159 }
Mike Stumpbf916502009-07-24 19:02:52 +00001160 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1161 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001162 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1163 unsigned FirstIdx = 1;
1164 // checks for the 2nd argument
1165 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1166 llvm::APSInt Idx(32);
1167 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1169 << "format" << 2 << IdxExpr->getSourceRange();
1170 return;
1171 }
Mike Stumpbf916502009-07-24 19:02:52 +00001172
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001173 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1174 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1175 << "format" << 2 << IdxExpr->getSourceRange();
1176 return;
1177 }
Mike Stumpbf916502009-07-24 19:02:52 +00001178
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001179 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001180
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001181 // make sure the format string is really a string
1182 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001183
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001184 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1185 if (not_nsstring_type &&
1186 !isCFStringType(Ty, S.Context) &&
1187 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001188 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001189 // FIXME: Should highlight the actual expression that has the wrong type.
1190 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001191 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001192 << IdxExpr->getSourceRange();
1193 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001194 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001195 Ty = getFunctionOrMethodResultType(d);
1196 if (!isNSStringType(Ty, S.Context) &&
1197 !isCFStringType(Ty, S.Context) &&
1198 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001199 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001200 // FIXME: Should highlight the actual expression that has the wrong type.
1201 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001202 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001203 << IdxExpr->getSourceRange();
1204 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001205 }
1206
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001207 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001208}
1209
Mike Stumpbf916502009-07-24 19:02:52 +00001210/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1211/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001212static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001213
Chris Lattner545dd342008-06-28 23:36:30 +00001214 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001215 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001216 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001217 return;
1218 }
1219
Chris Lattner545dd342008-06-28 23:36:30 +00001220 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001221 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001222 return;
1223 }
1224
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001225 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001227 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228 return;
1229 }
1230
Mike Stumpbf916502009-07-24 19:02:52 +00001231 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1232 // needed in order to be compatible with GCC the index must start in 1 and the
1233 // limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001234 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001235 unsigned FirstIdx = 1;
1236
Chris Lattner545dd342008-06-28 23:36:30 +00001237 const char *Format = Attr.getParameterName()->getName();
1238 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001239
1240 // Normalize the argument, __foo__ becomes foo.
1241 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1242 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1243 Format += 2;
1244 FormatLen -= 4;
1245 }
1246
1247 bool Supported = false;
1248 bool is_NSString = false;
1249 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001250 bool is_CFString = false;
Mike Stumpbf916502009-07-24 19:02:52 +00001251
Chris Lattner6b6b5372008-06-26 18:38:35 +00001252 switch (FormatLen) {
1253 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001254 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1255 case 6: Supported = !memcmp(Format, "printf", 6); break;
Ryan Flynn4403a5e2009-08-06 03:00:50 +00001256 case 7: Supported = !memcmp(Format, "printf0", 7) ||
1257 !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001258 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001259 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1260 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1261 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001262 break;
1263 }
Mike Stumpbf916502009-07-24 19:02:52 +00001264
Chris Lattner6b6b5372008-06-26 18:38:35 +00001265 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001266 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1267 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001268 return;
1269 }
1270
1271 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001272 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001273 llvm::APSInt Idx(32);
1274 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001276 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001277 return;
1278 }
1279
1280 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001282 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283 return;
1284 }
1285
1286 // FIXME: Do we need to bounds check?
1287 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001288
Chris Lattner6b6b5372008-06-26 18:38:35 +00001289 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001290 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001291
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001292 if (is_CFString) {
1293 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001294 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1295 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001296 return;
1297 }
1298 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001299 // FIXME: do we need to check if the type is NSString*? What are the
1300 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001301 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001302 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001303 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1304 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001305 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001306 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001307 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001308 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001309 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001310 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1311 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001312 return;
1313 }
1314
1315 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001316 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001317 llvm::APSInt FirstArg(32);
1318 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001320 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001321 return;
1322 }
1323
1324 // check if the function is variadic if the 3rd argument non-zero
1325 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001326 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001327 ++NumArgs; // +1 for ...
1328 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001329 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001330 return;
1331 }
1332 }
1333
Chris Lattner3c73c412008-11-19 08:23:25 +00001334 // strftime requires FirstArg to be 0 because it doesn't read from any
1335 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001336 if (is_strftime) {
1337 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001338 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1339 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001340 return;
1341 }
1342 // if 0 it disables parameter checking (to use with e.g. va_list)
1343 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001345 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001346 return;
1347 }
1348
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001349 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 Idx.getZExtValue(), FirstArg.getZExtValue()));
1351}
1352
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001353static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1354 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001355 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001356 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001357 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001358 return;
1359 }
1360
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001361 // Try to find the underlying union declaration.
1362 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001363 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001364 if (TD && TD->getUnderlyingType()->isUnionType())
1365 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1366 else
1367 RD = dyn_cast<RecordDecl>(d);
1368
1369 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001370 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001371 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001372 return;
1373 }
1374
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001375 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001376 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001377 diag::warn_transparent_union_attribute_not_definition);
1378 return;
1379 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001380
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001381 RecordDecl::field_iterator Field = RD->field_begin(),
1382 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001383 if (Field == FieldEnd) {
1384 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1385 return;
1386 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001387
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001388 FieldDecl *FirstField = *Field;
1389 QualType FirstType = FirstField->getType();
1390 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001391 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001392 diag::warn_transparent_union_attribute_floating);
1393 return;
1394 }
1395
1396 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1397 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1398 for (; Field != FieldEnd; ++Field) {
1399 QualType FieldType = Field->getType();
1400 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1401 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1402 // Warn if we drop the attribute.
1403 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001404 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001405 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001406 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001407 diag::warn_transparent_union_attribute_field_size_align)
1408 << isSize << Field->getDeclName() << FieldBits;
1409 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001410 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001411 diag::note_transparent_union_first_field_size_align)
1412 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001413 return;
1414 }
1415 }
1416
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001417 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001418}
1419
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001420static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001421 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001422 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001424 return;
1425 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001426 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1427 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001428
Chris Lattner6b6b5372008-06-26 18:38:35 +00001429 // Make sure that there is a string literal as the annotation's single
1430 // argument.
1431 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001432 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001433 return;
1434 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001435 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001436 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001437}
1438
Chris Lattner803d0802008-06-29 00:43:07 +00001439static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001440 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001441 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001442 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001443 return;
1444 }
1445
1446 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001447 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001448 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001449 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001450 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001451 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001452 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453 }
Mike Stumpbf916502009-07-24 19:02:52 +00001454
Chris Lattner49e2d342008-06-28 23:50:44 +00001455 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1456 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001457 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1459 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001460 return;
1461 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001462 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001463 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001464 << alignmentExpr->getSourceRange();
1465 return;
1466 }
1467
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001468 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001469}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001470
Mike Stumpbf916502009-07-24 19:02:52 +00001471/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1472/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001473///
Mike Stumpbf916502009-07-24 19:02:52 +00001474/// Despite what would be logical, the mode attribute is a decl attribute, not a
1475/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1476/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001477static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001478 // This attribute isn't documented, but glibc uses it. It changes
1479 // the width of an int or unsigned int to the specified size.
1480
1481 // Check that there aren't any arguments
1482 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001484 return;
1485 }
1486
1487 IdentifierInfo *Name = Attr.getParameterName();
1488 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001489 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001490 return;
1491 }
1492 const char *Str = Name->getName();
1493 unsigned Len = Name->getLength();
1494
1495 // Normalize the attribute name, __foo__ becomes foo.
1496 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1497 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1498 Str += 2;
1499 Len -= 4;
1500 }
1501
1502 unsigned DestWidth = 0;
1503 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001504 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001505 switch (Len) {
1506 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001507 switch (Str[0]) {
1508 case 'Q': DestWidth = 8; break;
1509 case 'H': DestWidth = 16; break;
1510 case 'S': DestWidth = 32; break;
1511 case 'D': DestWidth = 64; break;
1512 case 'X': DestWidth = 96; break;
1513 case 'T': DestWidth = 128; break;
1514 }
1515 if (Str[1] == 'F') {
1516 IntegerMode = false;
1517 } else if (Str[1] == 'C') {
1518 IntegerMode = false;
1519 ComplexMode = true;
1520 } else if (Str[1] != 'I') {
1521 DestWidth = 0;
1522 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001523 break;
1524 case 4:
1525 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1526 // pointer on PIC16 and other embedded platforms.
1527 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001528 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001529 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001530 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001531 break;
1532 case 7:
1533 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001534 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001535 break;
1536 }
1537
1538 QualType OldTy;
1539 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1540 OldTy = TD->getUnderlyingType();
1541 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1542 OldTy = VD->getType();
1543 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001544 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1545 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001546 return;
1547 }
Eli Friedman73397492009-03-03 06:41:03 +00001548
1549 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1550 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1551 else if (IntegerMode) {
1552 if (!OldTy->isIntegralType())
1553 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1554 } else if (ComplexMode) {
1555 if (!OldTy->isComplexType())
1556 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1557 } else {
1558 if (!OldTy->isFloatingType())
1559 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1560 }
1561
Mike Stump390b4cc2009-05-16 07:39:55 +00001562 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1563 // and friends, at least with glibc.
1564 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1565 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001566 // FIXME: Make sure floating-point mappings are accurate
1567 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001568 QualType NewTy;
1569 switch (DestWidth) {
1570 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001571 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001572 return;
1573 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001574 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001575 return;
1576 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001577 if (!IntegerMode) {
1578 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1579 return;
1580 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001581 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001582 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001583 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001584 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001585 break;
1586 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001587 if (!IntegerMode) {
1588 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1589 return;
1590 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001591 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001592 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001593 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001594 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 break;
1596 case 32:
1597 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001598 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001599 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001600 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001601 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001602 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001603 break;
1604 case 64:
1605 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001606 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001607 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001608 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001609 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001610 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001611 break;
Eli Friedman73397492009-03-03 06:41:03 +00001612 case 96:
1613 NewTy = S.Context.LongDoubleTy;
1614 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001615 case 128:
1616 if (!IntegerMode) {
1617 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1618 return;
1619 }
1620 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001621 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001622 }
1623
Eli Friedman73397492009-03-03 06:41:03 +00001624 if (ComplexMode) {
1625 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001626 }
1627
1628 // Install the new type.
1629 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1630 TD->setUnderlyingType(NewTy);
1631 else
1632 cast<ValueDecl>(D)->setType(NewTy);
1633}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001634
Anders Carlssond87df372009-02-13 06:46:13 +00001635static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1636 // check the attribute arguments.
1637 if (Attr.getNumArgs() > 0) {
1638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1639 return;
1640 }
Anders Carlssone896d982009-02-13 08:11:52 +00001641
Anders Carlsson5bab7882009-02-19 19:16:48 +00001642 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001643 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001644 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001645 return;
1646 }
Mike Stumpbf916502009-07-24 19:02:52 +00001647
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001648 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001649}
1650
Anders Carlsson5bab7882009-02-19 19:16:48 +00001651static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1652 // check the attribute arguments.
1653 if (Attr.getNumArgs() != 0) {
1654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1655 return;
1656 }
Mike Stumpbf916502009-07-24 19:02:52 +00001657
Chris Lattnerc5197432009-04-14 17:02:11 +00001658 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001660 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001661 return;
1662 }
Mike Stumpbf916502009-07-24 19:02:52 +00001663
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001664 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001665}
1666
Chris Lattnercf2a7212009-04-20 19:12:28 +00001667static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001668 // check the attribute arguments.
1669 if (Attr.getNumArgs() != 0) {
1670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1671 return;
1672 }
Mike Stumpbf916502009-07-24 19:02:52 +00001673
Chris Lattnerc5197432009-04-14 17:02:11 +00001674 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1675 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001676 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001677 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001678 return;
1679 }
Mike Stumpbf916502009-07-24 19:02:52 +00001680
Chris Lattnerc5197432009-04-14 17:02:11 +00001681 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001682 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001683 return;
1684 }
Mike Stumpbf916502009-07-24 19:02:52 +00001685
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001686 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001687}
1688
Fariborz Jahanianee760332009-03-27 18:38:55 +00001689static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1690 // check the attribute arguments.
1691 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001693 return;
1694 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001695
Fariborz Jahanianee760332009-03-27 18:38:55 +00001696 if (!isFunctionOrMethod(d)) {
1697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001698 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001699 return;
1700 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001701
1702 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1703 llvm::APSInt NumParams(32);
1704 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1705 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1706 << "regparm" << NumParamsExpr->getSourceRange();
1707 return;
1708 }
1709
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001710 if (S.Context.Target.getRegParmMax() == 0) {
1711 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001712 << NumParamsExpr->getSourceRange();
1713 return;
1714 }
1715
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001716 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001717 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1718 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001719 return;
1720 }
1721
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001722 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001723}
1724
Chris Lattner0744e5f2008-06-29 00:23:49 +00001725//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001726// Checker-specific attribute handlers.
1727//===----------------------------------------------------------------------===//
1728
1729static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1730 Sema &S) {
1731
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001732 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001733
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001734 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1735 RetTy = MD->getResultType();
1736 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1737 RetTy = FD->getResultType();
1738 else {
1739 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1740 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001741 return;
1742 }
Mike Stumpbf916502009-07-24 19:02:52 +00001743
Ted Kremenek6217b802009-07-29 21:53:49 +00001744 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
Steve Naroff14108da2009-07-10 23:34:53 +00001745 || RetTy->getAsObjCObjectPointerType())) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001746 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1747 << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001748 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001749 }
Mike Stumpbf916502009-07-24 19:02:52 +00001750
Ted Kremenekb71368d2009-05-09 02:44:38 +00001751 switch (Attr.getKind()) {
1752 default:
1753 assert(0 && "invalid ownership attribute");
1754 return;
1755 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001756 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001757 return;
1758 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001759 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001760 return;
1761 };
1762}
1763
1764//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001765// Top Level Sema Entry Points
1766//===----------------------------------------------------------------------===//
1767
Sebastian Redla89d82c2008-12-21 19:24:58 +00001768/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001769/// the attribute applies to decls. If the attribute is a type attribute, just
1770/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001771static void ProcessDeclAttribute(Scope *scope, Decl *D,
1772 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001773 if (Attr.isDeclspecAttribute())
1774 // FIXME: Try to deal with __declspec attributes!
1775 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001776 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001777 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001778 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001779 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001780 // Ignore these, these are type attributes, handled by
1781 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001782 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001783 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1784 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001785 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001786 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001787 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001788 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001789 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1790 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1791 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1792 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1793 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1794 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001795 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001796 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001797 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001798 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1799 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001800 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001801 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001802 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001803 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001804 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1805 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1806 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001807
1808 // Checker-specific.
1809 case AttributeList::AT_ns_returns_retained:
1810 case AttributeList::AT_cf_returns_retained:
1811 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1812
Nate Begeman6f3d8382009-06-26 06:32:41 +00001813 case AttributeList::AT_reqd_wg_size:
1814 HandleReqdWorkGroupSize(D, Attr, S); break;
1815
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001816 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001817 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001818 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001819 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001820 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001821 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001822 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001823 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001824 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1825 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001826 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001827 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001828 case AttributeList::AT_transparent_union:
1829 HandleTransparentUnionAttr(D, Attr, S);
1830 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001831 case AttributeList::AT_objc_exception:
1832 HandleObjCExceptionAttr(D, Attr, S);
1833 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001834 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001835 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001836 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001837 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001838 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1839 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001840 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001841 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001842 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001843 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001844 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001845 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001846 // Just ignore
1847 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001848 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001849 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001850 break;
1851 }
1852}
1853
1854/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1855/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001856void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001857 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001858 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001859 AttrList = AttrList->getNext();
1860 }
1861}
1862
Ryan Flynne25ff832009-07-30 03:15:39 +00001863/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1864/// #pragma weak needs a non-definition decl and source may not have one
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001865NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II)
Ryan Flynne25ff832009-07-30 03:15:39 +00001866{
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001867 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001868 NamedDecl *NewD = 0;
1869 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1870 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1871 FD->getLocation(), DeclarationName(II),
1872 FD->getType());
1873 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1874 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1875 VD->getLocation(), II,
1876 VD->getType(), VD->getStorageClass());
1877 }
1878 return NewD;
1879}
1880
1881/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1882/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001883void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001884 if (!W.getUsed()) { // only do this once
1885 W.setUsed(true);
1886 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1887 IdentifierInfo *NDId = ND->getIdentifier();
1888 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1889 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1890 NewD->addAttr(::new (Context) WeakAttr());
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001891 WeakTopLevelDecl.push_back(NewD);
1892 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1893 // to insert Decl at TU scope, sorry.
1894 DeclContext *SavedContext = CurContext;
1895 CurContext = Context.getTranslationUnitDecl();
1896 PushOnScopeChains(NewD, S);
1897 CurContext = SavedContext;
Ryan Flynne25ff832009-07-30 03:15:39 +00001898 } else { // just add weak to existing
1899 ND->addAttr(::new (Context) WeakAttr());
1900 }
1901 }
1902}
1903
Chris Lattner0744e5f2008-06-29 00:23:49 +00001904/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1905/// it, apply them to D. This is a bit tricky because PD can have attributes
1906/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001907void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001908 // Handle #pragma weak
1909 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1910 if (ND->hasLinkage()) {
1911 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1912 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001913 // Identifier referenced by #pragma weak before it was declared
1914 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001915 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1916 }
1917 }
1918 }
1919
Chris Lattner0744e5f2008-06-29 00:23:49 +00001920 // Apply decl attributes from the DeclSpec if present.
1921 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001922 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001923
Chris Lattner0744e5f2008-06-29 00:23:49 +00001924 // Walk the declarator structure, applying decl attributes that were in a type
1925 // position to the decl itself. This handles cases like:
1926 // int *__attr__(x)** D;
1927 // when X is a decl attribute.
1928 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1929 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001930 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001931
Chris Lattner0744e5f2008-06-29 00:23:49 +00001932 // Finally, apply any attributes on the decl itself.
1933 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001934 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001935}