blob: 23fe4010fb0b6cda1739861b421d5eb95fa29e4c [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 }
Ted Kremenekb1090692009-08-14 22:03:27 +0000440
441 const FunctionType *FT = getFunctionType(d, false);
442
443 if (!FT) {
Ryan Flynn76168e22009-08-09 20:07:29 +0000444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
445 << Attr.getName() << 0 /*function*/;
446 return;
447 }
448
Ted Kremenekb1090692009-08-14 22:03:27 +0000449 QualType RetTy = FT->getResultType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000450
451 if (!(RetTy->isAnyPointerType() || RetTy->isBlockPointerType())) {
452 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
453 return;
Ryan Flynnfd6ad3c2009-08-09 22:36:29 +0000454 }
455
Ryan Flynn76168e22009-08-09 20:07:29 +0000456 d->addAttr(::new (S.Context) MallocAttr());
457}
458
Ted Kremenekb7252322009-04-10 00:01:14 +0000459static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000460 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000461 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000462 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000463 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000464 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000465 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000466
Mike Stump19c30c02009-04-29 19:03:13 +0000467 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
468 ValueDecl *VD = dyn_cast<ValueDecl>(d);
469 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000471 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000472 return false;
473 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000474 }
Mike Stumpbf916502009-07-24 19:02:52 +0000475
Ted Kremenekb7252322009-04-10 00:01:14 +0000476 return true;
477}
478
479static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000480 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000481 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000482}
483
484static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
485 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000486 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000487 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000488}
489
Ted Kremenek73798892008-07-25 04:39:19 +0000490static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
491 // check the attribute arguments.
492 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000494 return;
495 }
Mike Stumpbf916502009-07-24 19:02:52 +0000496
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000497 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000499 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000500 return;
501 }
Mike Stumpbf916502009-07-24 19:02:52 +0000502
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000503 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000504}
505
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000506static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
507 // check the attribute arguments.
508 if (Attr.getNumArgs() != 0) {
509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
510 return;
511 }
Mike Stumpbf916502009-07-24 19:02:52 +0000512
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000513 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000514 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000515 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
516 return;
517 }
518 } else if (!isFunctionOrMethod(d)) {
519 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000520 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000521 return;
522 }
Mike Stumpbf916502009-07-24 19:02:52 +0000523
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000524 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000525}
526
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000527static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
528 // check the attribute arguments.
529 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000530 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
531 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000532 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000533 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000534
535 int priority = 65535; // FIXME: Do not hardcode such constants.
536 if (Attr.getNumArgs() > 0) {
537 Expr *E = static_cast<Expr *>(Attr.getArg(0));
538 llvm::APSInt Idx(32);
539 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000541 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000542 return;
543 }
544 priority = Idx.getZExtValue();
545 }
Mike Stumpbf916502009-07-24 19:02:52 +0000546
Chris Lattnerc5197432009-04-14 17:02:11 +0000547 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000548 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000549 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000550 return;
551 }
552
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000553 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000554}
555
556static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
557 // check the attribute arguments.
558 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
560 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000561 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000562 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000563
564 int priority = 65535; // FIXME: Do not hardcode such constants.
565 if (Attr.getNumArgs() > 0) {
566 Expr *E = static_cast<Expr *>(Attr.getArg(0));
567 llvm::APSInt Idx(32);
568 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000570 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000571 return;
572 }
573 priority = Idx.getZExtValue();
574 }
Mike Stumpbf916502009-07-24 19:02:52 +0000575
Anders Carlsson6782fc62008-08-22 22:10:48 +0000576 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000577 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000578 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000579 return;
580 }
581
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000582 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000583}
584
Chris Lattner803d0802008-06-29 00:43:07 +0000585static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000586 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000587 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000588 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000589 return;
590 }
Mike Stumpbf916502009-07-24 19:02:52 +0000591
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000592 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000593}
594
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000595static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
596 // check the attribute arguments.
597 if (Attr.getNumArgs() != 0) {
598 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
599 return;
600 }
Mike Stumpbf916502009-07-24 19:02:52 +0000601
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000602 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000603}
604
Chris Lattner803d0802008-06-29 00:43:07 +0000605static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000607 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000608 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000609 return;
610 }
Mike Stumpbf916502009-07-24 19:02:52 +0000611
Chris Lattner545dd342008-06-28 23:36:30 +0000612 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000613 Arg = Arg->IgnoreParenCasts();
614 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000615
Chris Lattner6b6b5372008-06-26 18:38:35 +0000616 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000617 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000618 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000619 return;
620 }
Mike Stumpbf916502009-07-24 19:02:52 +0000621
Chris Lattner6b6b5372008-06-26 18:38:35 +0000622 const char *TypeStr = Str->getStrData();
623 unsigned TypeLen = Str->getByteLength();
624 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000625
Chris Lattner6b6b5372008-06-26 18:38:35 +0000626 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
627 type = VisibilityAttr::DefaultVisibility;
628 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
629 type = VisibilityAttr::HiddenVisibility;
630 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
631 type = VisibilityAttr::HiddenVisibility; // FIXME
632 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
633 type = VisibilityAttr::ProtectedVisibility;
634 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000635 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000636 return;
637 }
Mike Stumpbf916502009-07-24 19:02:52 +0000638
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000639 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000640}
641
Chris Lattner0db29ec2009-02-14 08:09:34 +0000642static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
643 Sema &S) {
644 if (Attr.getNumArgs() != 0) {
645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
646 return;
647 }
Mike Stumpbf916502009-07-24 19:02:52 +0000648
Chris Lattner0db29ec2009-02-14 08:09:34 +0000649 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
650 if (OCI == 0) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
652 return;
653 }
Mike Stumpbf916502009-07-24 19:02:52 +0000654
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000655 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000656}
657
658static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000659 if (Attr.getNumArgs() != 0) {
660 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
661 return;
662 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000663 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000664 QualType T = TD->getUnderlyingType();
665 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000666 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000667 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
668 return;
669 }
670 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000671 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000672}
673
Mike Stumpbf916502009-07-24 19:02:52 +0000674static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000675HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
676 if (Attr.getNumArgs() != 0) {
677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
678 return;
679 }
680
681 if (!isa<FunctionDecl>(D)) {
682 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
683 return;
684 }
685
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000686 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000687}
688
Steve Naroff9eae5762008-09-18 16:44:58 +0000689static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000690 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000691 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000692 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000693 return;
694 }
Mike Stumpbf916502009-07-24 19:02:52 +0000695
Steve Naroff9eae5762008-09-18 16:44:58 +0000696 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000697 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000698 return;
699 }
Mike Stumpbf916502009-07-24 19:02:52 +0000700
Steve Naroff9eae5762008-09-18 16:44:58 +0000701 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000702 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000703 type = BlocksAttr::ByRef;
704 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000705 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000706 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000707 return;
708 }
Mike Stumpbf916502009-07-24 19:02:52 +0000709
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000710 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000711}
712
Anders Carlsson77091822008-10-05 18:05:59 +0000713static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
714 // check the attribute arguments.
715 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000716 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
717 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000718 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000719 }
720
Anders Carlsson77091822008-10-05 18:05:59 +0000721 int sentinel = 0;
722 if (Attr.getNumArgs() > 0) {
723 Expr *E = static_cast<Expr *>(Attr.getArg(0));
724 llvm::APSInt Idx(32);
725 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000726 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000727 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000728 return;
729 }
730 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000731
Anders Carlsson77091822008-10-05 18:05:59 +0000732 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000733 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
734 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000735 return;
736 }
737 }
738
739 int nullPos = 0;
740 if (Attr.getNumArgs() > 1) {
741 Expr *E = static_cast<Expr *>(Attr.getArg(1));
742 llvm::APSInt Idx(32);
743 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000744 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000745 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000746 return;
747 }
748 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000749
Anders Carlsson77091822008-10-05 18:05:59 +0000750 if (nullPos > 1 || nullPos < 0) {
751 // FIXME: This error message could be improved, it would be nice
752 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000753 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
754 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000755 return;
756 }
757 }
758
759 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000760 const FunctionType *FT = FD->getType()->getAsFunctionType();
761 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000762
Chris Lattner897cd902009-03-17 23:03:47 +0000763 if (isa<FunctionNoProtoType>(FT)) {
764 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
765 return;
766 }
Mike Stumpbf916502009-07-24 19:02:52 +0000767
Chris Lattner897cd902009-03-17 23:03:47 +0000768 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000769 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000770 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000771 }
Anders Carlsson77091822008-10-05 18:05:59 +0000772 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
773 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000774 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000775 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000776 }
777 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000778 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
779 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000780 ;
781 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000782 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000783 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000784 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Ted Kremenek6217b802009-07-29 21:53:49 +0000785 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000786 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000787 int m = Ty->isFunctionPointerType() ? 0 : 1;
788 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000789 return;
790 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000791 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000792 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000793 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000794 return;
795 }
Anders Carlsson77091822008-10-05 18:05:59 +0000796 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000797 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000798 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000799 return;
800 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000801 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000802}
803
Chris Lattner026dc962009-02-14 07:37:35 +0000804static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
805 // check the attribute arguments.
806 if (Attr.getNumArgs() != 0) {
807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
808 return;
809 }
810
811 // TODO: could also be applied to methods?
812 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
813 if (!Fn) {
814 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000815 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000816 return;
817 }
Mike Stumpbf916502009-07-24 19:02:52 +0000818
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000819 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000820}
821
822static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000823 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000824 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000825 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000826 return;
827 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000828
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000829 /* weak only applies to non-static declarations */
830 bool isStatic = false;
831 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
832 isStatic = VD->getStorageClass() == VarDecl::Static;
833 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
834 isStatic = FD->getStorageClass() == FunctionDecl::Static;
835 }
836 if (isStatic) {
837 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
838 dyn_cast<NamedDecl>(D)->getNameAsString();
839 return;
840 }
841
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000842 // TODO: could also be applied to methods?
843 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
844 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000845 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000846 return;
847 }
Mike Stumpbf916502009-07-24 19:02:52 +0000848
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000849 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000850}
851
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000852static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
853 // check the attribute arguments.
854 if (Attr.getNumArgs() != 0) {
855 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
856 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000857 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000858
859 // weak_import only applies to variable & function declarations.
860 bool isDef = false;
861 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
862 isDef = (!VD->hasExternalStorage() || VD->getInit());
863 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000864 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000865 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
866 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000867 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000868 } else {
869 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000870 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000871 return;
872 }
873
874 // Merge should handle any subsequent violations.
875 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000876 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000877 diag::warn_attribute_weak_import_invalid_on_definition)
878 << "weak_import" << 2 /*variable and function*/;
879 return;
880 }
881
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000882 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000883}
884
Chris Lattner026dc962009-02-14 07:37:35 +0000885static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000886 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000887 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000889 return;
890 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000891
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000892 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000893 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000894 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000895 return;
896 }
897
Chris Lattner026dc962009-02-14 07:37:35 +0000898 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000899 if (!FD) {
900 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000901 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000902 return;
903 }
904
905 // Currently, the dllimport attribute is ignored for inlined functions.
906 // Warning is emitted.
907 if (FD->isInline()) {
908 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
909 return;
910 }
911
912 // The attribute is also overridden by a subsequent declaration as dllexport.
913 // Warning is emitted.
914 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
915 nextAttr = nextAttr->getNext()) {
916 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
917 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
918 return;
919 }
920 }
921
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000922 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000923 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
924 return;
925 }
926
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000927 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000928}
929
Chris Lattner026dc962009-02-14 07:37:35 +0000930static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000931 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000932 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000934 return;
935 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000936
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000937 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000938 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000939 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000940 return;
941 }
942
Chris Lattner026dc962009-02-14 07:37:35 +0000943 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000944 if (!FD) {
945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000946 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000947 return;
948 }
949
Mike Stumpbf916502009-07-24 19:02:52 +0000950 // Currently, the dllexport attribute is ignored for inlined functions, unless
951 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000952 if (FD->isInline()) {
953 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
954 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
955 return;
956 }
957
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000958 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000959}
960
Nate Begeman6f3d8382009-06-26 06:32:41 +0000961static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
962 Sema &S) {
963 // Attribute has 3 arguments.
964 if (Attr.getNumArgs() != 3) {
965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
966 return;
967 }
968
969 unsigned WGSize[3];
970 for (unsigned i = 0; i < 3; ++i) {
971 Expr *E = static_cast<Expr *>(Attr.getArg(i));
972 llvm::APSInt ArgNum(32);
973 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
974 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
975 << "reqd_work_group_size" << E->getSourceRange();
976 return;
977 }
978 WGSize[i] = (unsigned) ArgNum.getZExtValue();
979 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000980 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000981 WGSize[2]));
982}
983
Chris Lattner026dc962009-02-14 07:37:35 +0000984static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000985 // Attribute has no arguments.
986 if (Attr.getNumArgs() != 1) {
987 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
988 return;
989 }
990
991 // Make sure that there is a string literal as the sections's single
992 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000993 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
994 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000995 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000996 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000997 return;
998 }
Chris Lattner797c3c42009-08-10 19:03:04 +0000999
1000 std::string SectionStr(SE->getStrData(), SE->getByteLength());
1001
1002 // If the target wants to validate the section specifier, make it happen.
1003 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
1004 if (Error.empty()) {
1005 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
1006 return;
1007 }
1008
1009 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1010 << Error;
1011
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001012}
1013
Chris Lattner803d0802008-06-29 00:43:07 +00001014static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001015 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001016 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001017 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001018 return;
1019 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001020
1021 // Attribute can be applied only to functions.
1022 if (!isa<FunctionDecl>(d)) {
1023 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001024 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001025 return;
1026 }
1027
1028 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001029 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001030 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1031 << "stdcall" << "fastcall";
1032 return;
1033 }
1034
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001035 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001036}
1037
Chris Lattner803d0802008-06-29 00:43:07 +00001038static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001039 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001040 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001041 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001042 return;
1043 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001044
1045 if (!isa<FunctionDecl>(d)) {
1046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001047 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001048 return;
1049 }
1050
1051 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001052 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001053 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1054 << "fastcall" << "stdcall";
1055 return;
1056 }
1057
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001058 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001059}
1060
Chris Lattner803d0802008-06-29 00:43:07 +00001061static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001062 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001063 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001065 return;
1066 }
Mike Stumpbf916502009-07-24 19:02:52 +00001067
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001068 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001069}
1070
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001071static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1072 // check the attribute arguments.
1073 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001074 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001075 return;
1076 }
Mike Stumpbf916502009-07-24 19:02:52 +00001077
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001078 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001079}
1080
1081static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1082 // check the attribute arguments.
1083 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001084 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001085 return;
1086 }
Mike Stumpbf916502009-07-24 19:02:52 +00001087
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001088 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001089}
1090
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001091static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001092 // Match gcc which ignores cleanup attrs when compiling C++.
1093 if (S.getLangOptions().CPlusPlus)
1094 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001095
1096 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001097 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1098 return;
1099 }
Mike Stumpbf916502009-07-24 19:02:52 +00001100
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001101 if (Attr.getNumArgs() != 0) {
1102 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1103 return;
1104 }
Mike Stumpbf916502009-07-24 19:02:52 +00001105
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001106 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001107
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001108 if (!VD || !VD->hasLocalStorage()) {
1109 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1110 return;
1111 }
Mike Stumpbf916502009-07-24 19:02:52 +00001112
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001113 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001114 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1115 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001116 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001117 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001118 Attr.getParameterName();
1119 return;
1120 }
Mike Stumpbf916502009-07-24 19:02:52 +00001121
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001122 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1123 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001124 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001125 Attr.getParameterName();
1126 return;
1127 }
1128
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001129 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001130 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001131 Attr.getParameterName();
1132 return;
1133 }
Mike Stumpbf916502009-07-24 19:02:52 +00001134
Anders Carlsson89941c12009-02-07 23:16:50 +00001135 // We're currently more strict than GCC about what function types we accept.
1136 // If this ever proves to be a problem it should be easy to fix.
1137 QualType Ty = S.Context.getPointerType(VD->getType());
1138 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001139 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001140 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001141 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1142 Attr.getParameterName() << ParamTy << Ty;
1143 return;
1144 }
Mike Stumpbf916502009-07-24 19:02:52 +00001145
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001146 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001147}
1148
Mike Stumpbf916502009-07-24 19:02:52 +00001149/// Handle __attribute__((format_arg((idx)))) attribute based on
1150/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1151static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001152 if (Attr.getNumArgs() != 1) {
1153 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1154 return;
1155 }
1156 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1157 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1158 << Attr.getName() << 0 /*function*/;
1159 return;
1160 }
Mike Stumpbf916502009-07-24 19:02:52 +00001161 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1162 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001163 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1164 unsigned FirstIdx = 1;
1165 // checks for the 2nd argument
1166 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1167 llvm::APSInt Idx(32);
1168 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1170 << "format" << 2 << IdxExpr->getSourceRange();
1171 return;
1172 }
Mike Stumpbf916502009-07-24 19:02:52 +00001173
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001174 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1175 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1176 << "format" << 2 << IdxExpr->getSourceRange();
1177 return;
1178 }
Mike Stumpbf916502009-07-24 19:02:52 +00001179
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001180 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001181
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001182 // make sure the format string is really a string
1183 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001184
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001185 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1186 if (not_nsstring_type &&
1187 !isCFStringType(Ty, S.Context) &&
1188 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001189 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001190 // FIXME: Should highlight the actual expression that has the wrong type.
1191 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001192 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001193 << IdxExpr->getSourceRange();
1194 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001195 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001196 Ty = getFunctionOrMethodResultType(d);
1197 if (!isNSStringType(Ty, S.Context) &&
1198 !isCFStringType(Ty, S.Context) &&
1199 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001200 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001201 // FIXME: Should highlight the actual expression that has the wrong type.
1202 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001203 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001204 << IdxExpr->getSourceRange();
1205 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001206 }
1207
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001208 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001209}
1210
Mike Stumpbf916502009-07-24 19:02:52 +00001211/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1212/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001213static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001214
Chris Lattner545dd342008-06-28 23:36:30 +00001215 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001216 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001217 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001218 return;
1219 }
1220
Chris Lattner545dd342008-06-28 23:36:30 +00001221 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001223 return;
1224 }
1225
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001226 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001227 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001228 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001229 return;
1230 }
1231
Mike Stumpbf916502009-07-24 19:02:52 +00001232 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1233 // needed in order to be compatible with GCC the index must start in 1 and the
1234 // limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001235 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001236 unsigned FirstIdx = 1;
1237
Chris Lattner545dd342008-06-28 23:36:30 +00001238 const char *Format = Attr.getParameterName()->getName();
1239 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001240
1241 // Normalize the argument, __foo__ becomes foo.
1242 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1243 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1244 Format += 2;
1245 FormatLen -= 4;
1246 }
1247
1248 bool Supported = false;
1249 bool is_NSString = false;
1250 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001251 bool is_CFString = false;
Mike Stumpbf916502009-07-24 19:02:52 +00001252
Chris Lattner6b6b5372008-06-26 18:38:35 +00001253 switch (FormatLen) {
1254 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001255 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1256 case 6: Supported = !memcmp(Format, "printf", 6); break;
Ryan Flynn4403a5e2009-08-06 03:00:50 +00001257 case 7: Supported = !memcmp(Format, "printf0", 7) ||
1258 !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001259 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001260 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1261 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1262 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263 break;
1264 }
Mike Stumpbf916502009-07-24 19:02:52 +00001265
Chris Lattner6b6b5372008-06-26 18:38:35 +00001266 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001267 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1268 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001269 return;
1270 }
1271
1272 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001273 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001274 llvm::APSInt Idx(32);
1275 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001276 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001277 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001278 return;
1279 }
1280
1281 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001282 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001283 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001284 return;
1285 }
1286
1287 // FIXME: Do we need to bounds check?
1288 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001289
Chris Lattner6b6b5372008-06-26 18:38:35 +00001290 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001291 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001292
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001293 if (is_CFString) {
1294 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001295 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1296 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001297 return;
1298 }
1299 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001300 // FIXME: do we need to check if the type is NSString*? What are the
1301 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001302 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001303 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001304 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1305 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001306 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001307 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001308 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001309 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001310 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001311 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1312 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313 return;
1314 }
1315
1316 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001317 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001318 llvm::APSInt FirstArg(32);
1319 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001321 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001322 return;
1323 }
1324
1325 // check if the function is variadic if the 3rd argument non-zero
1326 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001327 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001328 ++NumArgs; // +1 for ...
1329 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001330 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001331 return;
1332 }
1333 }
1334
Chris Lattner3c73c412008-11-19 08:23:25 +00001335 // strftime requires FirstArg to be 0 because it doesn't read from any
1336 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001337 if (is_strftime) {
1338 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001339 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1340 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001341 return;
1342 }
1343 // if 0 it disables parameter checking (to use with e.g. va_list)
1344 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001346 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347 return;
1348 }
1349
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001350 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001351 Idx.getZExtValue(), FirstArg.getZExtValue()));
1352}
1353
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001354static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1355 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001356 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001357 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001358 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001359 return;
1360 }
1361
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001362 // Try to find the underlying union declaration.
1363 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001364 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001365 if (TD && TD->getUnderlyingType()->isUnionType())
1366 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1367 else
1368 RD = dyn_cast<RecordDecl>(d);
1369
1370 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001371 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001372 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001373 return;
1374 }
1375
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001376 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001377 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001378 diag::warn_transparent_union_attribute_not_definition);
1379 return;
1380 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001381
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001382 RecordDecl::field_iterator Field = RD->field_begin(),
1383 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001384 if (Field == FieldEnd) {
1385 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1386 return;
1387 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001388
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001389 FieldDecl *FirstField = *Field;
1390 QualType FirstType = FirstField->getType();
1391 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001392 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001393 diag::warn_transparent_union_attribute_floating);
1394 return;
1395 }
1396
1397 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1398 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1399 for (; Field != FieldEnd; ++Field) {
1400 QualType FieldType = Field->getType();
1401 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1402 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1403 // Warn if we drop the attribute.
1404 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001405 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001406 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001407 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001408 diag::warn_transparent_union_attribute_field_size_align)
1409 << isSize << Field->getDeclName() << FieldBits;
1410 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001411 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001412 diag::note_transparent_union_first_field_size_align)
1413 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001414 return;
1415 }
1416 }
1417
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001418 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001419}
1420
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001421static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001422 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001423 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425 return;
1426 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001427 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1428 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001429
Chris Lattner6b6b5372008-06-26 18:38:35 +00001430 // Make sure that there is a string literal as the annotation's single
1431 // argument.
1432 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001433 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001434 return;
1435 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001436 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001437 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001438}
1439
Chris Lattner803d0802008-06-29 00:43:07 +00001440static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001441 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001442 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444 return;
1445 }
1446
1447 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001448 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001449 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001450 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001451 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001452 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001454 }
Mike Stumpbf916502009-07-24 19:02:52 +00001455
Chris Lattner49e2d342008-06-28 23:50:44 +00001456 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1457 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001458 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1460 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001461 return;
1462 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001463 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001464 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001465 << alignmentExpr->getSourceRange();
1466 return;
1467 }
1468
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001469 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001470}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001471
Mike Stumpbf916502009-07-24 19:02:52 +00001472/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1473/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001474///
Mike Stumpbf916502009-07-24 19:02:52 +00001475/// Despite what would be logical, the mode attribute is a decl attribute, not a
1476/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1477/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001478static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001479 // This attribute isn't documented, but glibc uses it. It changes
1480 // the width of an int or unsigned int to the specified size.
1481
1482 // Check that there aren't any arguments
1483 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001484 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001485 return;
1486 }
1487
1488 IdentifierInfo *Name = Attr.getParameterName();
1489 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001491 return;
1492 }
1493 const char *Str = Name->getName();
1494 unsigned Len = Name->getLength();
1495
1496 // Normalize the attribute name, __foo__ becomes foo.
1497 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1498 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1499 Str += 2;
1500 Len -= 4;
1501 }
1502
1503 unsigned DestWidth = 0;
1504 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001505 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001506 switch (Len) {
1507 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001508 switch (Str[0]) {
1509 case 'Q': DestWidth = 8; break;
1510 case 'H': DestWidth = 16; break;
1511 case 'S': DestWidth = 32; break;
1512 case 'D': DestWidth = 64; break;
1513 case 'X': DestWidth = 96; break;
1514 case 'T': DestWidth = 128; break;
1515 }
1516 if (Str[1] == 'F') {
1517 IntegerMode = false;
1518 } else if (Str[1] == 'C') {
1519 IntegerMode = false;
1520 ComplexMode = true;
1521 } else if (Str[1] != 'I') {
1522 DestWidth = 0;
1523 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001524 break;
1525 case 4:
1526 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1527 // pointer on PIC16 and other embedded platforms.
1528 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001529 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001530 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001531 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001532 break;
1533 case 7:
1534 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001535 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001536 break;
1537 }
1538
1539 QualType OldTy;
1540 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1541 OldTy = TD->getUnderlyingType();
1542 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1543 OldTy = VD->getType();
1544 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001545 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1546 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001547 return;
1548 }
Eli Friedman73397492009-03-03 06:41:03 +00001549
1550 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1551 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1552 else if (IntegerMode) {
1553 if (!OldTy->isIntegralType())
1554 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1555 } else if (ComplexMode) {
1556 if (!OldTy->isComplexType())
1557 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1558 } else {
1559 if (!OldTy->isFloatingType())
1560 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1561 }
1562
Mike Stump390b4cc2009-05-16 07:39:55 +00001563 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1564 // and friends, at least with glibc.
1565 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1566 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001567 // FIXME: Make sure floating-point mappings are accurate
1568 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001569 QualType NewTy;
1570 switch (DestWidth) {
1571 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001572 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001573 return;
1574 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001575 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001576 return;
1577 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001578 if (!IntegerMode) {
1579 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1580 return;
1581 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001582 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001583 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001584 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001585 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001586 break;
1587 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001588 if (!IntegerMode) {
1589 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1590 return;
1591 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001592 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001593 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001594 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001595 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001596 break;
1597 case 32:
1598 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001599 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001600 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001601 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001602 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001603 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001604 break;
1605 case 64:
1606 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001607 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001608 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001609 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001610 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001611 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001612 break;
Eli Friedman73397492009-03-03 06:41:03 +00001613 case 96:
1614 NewTy = S.Context.LongDoubleTy;
1615 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001616 case 128:
1617 if (!IntegerMode) {
1618 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1619 return;
1620 }
1621 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001622 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001623 }
1624
Eli Friedman73397492009-03-03 06:41:03 +00001625 if (ComplexMode) {
1626 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001627 }
1628
1629 // Install the new type.
1630 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1631 TD->setUnderlyingType(NewTy);
1632 else
1633 cast<ValueDecl>(D)->setType(NewTy);
1634}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001635
Anders Carlssond87df372009-02-13 06:46:13 +00001636static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1637 // check the attribute arguments.
1638 if (Attr.getNumArgs() > 0) {
1639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1640 return;
1641 }
Anders Carlssone896d982009-02-13 08:11:52 +00001642
Anders Carlsson5bab7882009-02-19 19:16:48 +00001643 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001644 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001645 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001646 return;
1647 }
Mike Stumpbf916502009-07-24 19:02:52 +00001648
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001649 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001650}
1651
Anders Carlsson5bab7882009-02-19 19:16:48 +00001652static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1653 // check the attribute arguments.
1654 if (Attr.getNumArgs() != 0) {
1655 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1656 return;
1657 }
Mike Stumpbf916502009-07-24 19:02:52 +00001658
Chris Lattnerc5197432009-04-14 17:02:11 +00001659 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001661 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001662 return;
1663 }
Mike Stumpbf916502009-07-24 19:02:52 +00001664
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001665 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001666}
1667
Chris Lattnercf2a7212009-04-20 19:12:28 +00001668static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001669 // check the attribute arguments.
1670 if (Attr.getNumArgs() != 0) {
1671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1672 return;
1673 }
Mike Stumpbf916502009-07-24 19:02:52 +00001674
Chris Lattnerc5197432009-04-14 17:02:11 +00001675 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1676 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001678 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001679 return;
1680 }
Mike Stumpbf916502009-07-24 19:02:52 +00001681
Chris Lattnerc5197432009-04-14 17:02:11 +00001682 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001683 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001684 return;
1685 }
Mike Stumpbf916502009-07-24 19:02:52 +00001686
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001687 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001688}
1689
Fariborz Jahanianee760332009-03-27 18:38:55 +00001690static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1691 // check the attribute arguments.
1692 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001693 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001694 return;
1695 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001696
Fariborz Jahanianee760332009-03-27 18:38:55 +00001697 if (!isFunctionOrMethod(d)) {
1698 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001699 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001700 return;
1701 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001702
1703 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1704 llvm::APSInt NumParams(32);
1705 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1706 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1707 << "regparm" << NumParamsExpr->getSourceRange();
1708 return;
1709 }
1710
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001711 if (S.Context.Target.getRegParmMax() == 0) {
1712 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001713 << NumParamsExpr->getSourceRange();
1714 return;
1715 }
1716
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001717 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001718 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1719 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001720 return;
1721 }
1722
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001723 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001724}
1725
Chris Lattner0744e5f2008-06-29 00:23:49 +00001726//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001727// Checker-specific attribute handlers.
1728//===----------------------------------------------------------------------===//
1729
1730static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1731 Sema &S) {
1732
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001733 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001734
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001735 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1736 RetTy = MD->getResultType();
1737 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1738 RetTy = FD->getResultType();
1739 else {
1740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1741 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001742 return;
1743 }
Mike Stumpbf916502009-07-24 19:02:52 +00001744
Ted Kremenek6217b802009-07-29 21:53:49 +00001745 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
Steve Naroff14108da2009-07-10 23:34:53 +00001746 || RetTy->getAsObjCObjectPointerType())) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001747 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1748 << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001749 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001750 }
Mike Stumpbf916502009-07-24 19:02:52 +00001751
Ted Kremenekb71368d2009-05-09 02:44:38 +00001752 switch (Attr.getKind()) {
1753 default:
1754 assert(0 && "invalid ownership attribute");
1755 return;
1756 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001757 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001758 return;
1759 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001760 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001761 return;
1762 };
1763}
1764
1765//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001766// Top Level Sema Entry Points
1767//===----------------------------------------------------------------------===//
1768
Sebastian Redla89d82c2008-12-21 19:24:58 +00001769/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001770/// the attribute applies to decls. If the attribute is a type attribute, just
1771/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001772static void ProcessDeclAttribute(Scope *scope, Decl *D,
1773 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001774 if (Attr.isDeclspecAttribute())
1775 // FIXME: Try to deal with __declspec attributes!
1776 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001777 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001778 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001779 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001780 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001781 // Ignore these, these are type attributes, handled by
1782 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001783 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001784 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1785 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001786 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001787 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001788 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001789 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001790 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1791 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1792 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1793 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1794 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1795 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001796 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001797 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001798 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001799 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1800 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001801 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001802 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001803 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001804 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001805 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1806 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1807 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001808
1809 // Checker-specific.
1810 case AttributeList::AT_ns_returns_retained:
1811 case AttributeList::AT_cf_returns_retained:
1812 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1813
Nate Begeman6f3d8382009-06-26 06:32:41 +00001814 case AttributeList::AT_reqd_wg_size:
1815 HandleReqdWorkGroupSize(D, Attr, S); break;
1816
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001817 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001818 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001819 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001820 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001821 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001822 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001823 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001824 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001825 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1826 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001827 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001828 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001829 case AttributeList::AT_transparent_union:
1830 HandleTransparentUnionAttr(D, Attr, S);
1831 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001832 case AttributeList::AT_objc_exception:
1833 HandleObjCExceptionAttr(D, Attr, S);
1834 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001835 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001836 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001837 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001838 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001839 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1840 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001841 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001842 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001843 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001844 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001845 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001846 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001847 // Just ignore
1848 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001849 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001850 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001851 break;
1852 }
1853}
1854
1855/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1856/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001857void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001858 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001859 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001860 AttrList = AttrList->getNext();
1861 }
1862}
1863
Ryan Flynne25ff832009-07-30 03:15:39 +00001864/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1865/// #pragma weak needs a non-definition decl and source may not have one
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001866NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II)
Ryan Flynne25ff832009-07-30 03:15:39 +00001867{
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001868 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001869 NamedDecl *NewD = 0;
1870 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1871 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1872 FD->getLocation(), DeclarationName(II),
1873 FD->getType());
1874 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1875 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1876 VD->getLocation(), II,
1877 VD->getType(), VD->getStorageClass());
1878 }
1879 return NewD;
1880}
1881
1882/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1883/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001884void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001885 if (!W.getUsed()) { // only do this once
1886 W.setUsed(true);
1887 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1888 IdentifierInfo *NDId = ND->getIdentifier();
1889 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1890 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1891 NewD->addAttr(::new (Context) WeakAttr());
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001892 WeakTopLevelDecl.push_back(NewD);
1893 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1894 // to insert Decl at TU scope, sorry.
1895 DeclContext *SavedContext = CurContext;
1896 CurContext = Context.getTranslationUnitDecl();
1897 PushOnScopeChains(NewD, S);
1898 CurContext = SavedContext;
Ryan Flynne25ff832009-07-30 03:15:39 +00001899 } else { // just add weak to existing
1900 ND->addAttr(::new (Context) WeakAttr());
1901 }
1902 }
1903}
1904
Chris Lattner0744e5f2008-06-29 00:23:49 +00001905/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1906/// it, apply them to D. This is a bit tricky because PD can have attributes
1907/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001908void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001909 // Handle #pragma weak
1910 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1911 if (ND->hasLinkage()) {
1912 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1913 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001914 // Identifier referenced by #pragma weak before it was declared
1915 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001916 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1917 }
1918 }
1919 }
1920
Chris Lattner0744e5f2008-06-29 00:23:49 +00001921 // Apply decl attributes from the DeclSpec if present.
1922 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001923 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001924
Chris Lattner0744e5f2008-06-29 00:23:49 +00001925 // Walk the declarator structure, applying decl attributes that were in a type
1926 // position to the decl itself. This handles cases like:
1927 // int *__attr__(x)** D;
1928 // when X is a decl attribute.
1929 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1930 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001931 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001932
Chris Lattner0744e5f2008-06-29 00:23:49 +00001933 // Finally, apply any attributes on the decl itself.
1934 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001935 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001936}