blob: 76b99bb33f13ec56b84ff8afa7907c76e959435d [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
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000441 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
442 QualType RetTy = FD->getResultType();
443 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
444 d->addAttr(::new (S.Context) MallocAttr());
445 return;
446 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000447 }
448
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000449 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000450}
451
Ted Kremenekb7252322009-04-10 00:01:14 +0000452static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000453 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000454 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000455 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000456 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000457 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000458 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000459
Mike Stump19c30c02009-04-29 19:03:13 +0000460 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
461 ValueDecl *VD = dyn_cast<ValueDecl>(d);
462 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000464 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000465 return false;
466 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000467 }
Mike Stumpbf916502009-07-24 19:02:52 +0000468
Ted Kremenekb7252322009-04-10 00:01:14 +0000469 return true;
470}
471
472static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000473 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000474 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000475}
476
477static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
478 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000479 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000480 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000481}
482
Ted Kremenek73798892008-07-25 04:39:19 +0000483static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
484 // check the attribute arguments.
485 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000486 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000487 return;
488 }
Mike Stumpbf916502009-07-24 19:02:52 +0000489
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000490 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000491 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000492 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000493 return;
494 }
Mike Stumpbf916502009-07-24 19:02:52 +0000495
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000496 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000497}
498
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000499static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
500 // check the attribute arguments.
501 if (Attr.getNumArgs() != 0) {
502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
503 return;
504 }
Mike Stumpbf916502009-07-24 19:02:52 +0000505
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000506 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000507 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000508 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
509 return;
510 }
511 } else if (!isFunctionOrMethod(d)) {
512 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000513 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000514 return;
515 }
Mike Stumpbf916502009-07-24 19:02:52 +0000516
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000517 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000518}
519
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000520static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
521 // check the attribute arguments.
522 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
524 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000525 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000526 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000527
528 int priority = 65535; // FIXME: Do not hardcode such constants.
529 if (Attr.getNumArgs() > 0) {
530 Expr *E = static_cast<Expr *>(Attr.getArg(0));
531 llvm::APSInt Idx(32);
532 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000533 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000534 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000535 return;
536 }
537 priority = Idx.getZExtValue();
538 }
Mike Stumpbf916502009-07-24 19:02:52 +0000539
Chris Lattnerc5197432009-04-14 17:02:11 +0000540 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000541 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000542 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000543 return;
544 }
545
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000546 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000547}
548
549static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
550 // check the attribute arguments.
551 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
553 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000554 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000555 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000556
557 int priority = 65535; // FIXME: Do not hardcode such constants.
558 if (Attr.getNumArgs() > 0) {
559 Expr *E = static_cast<Expr *>(Attr.getArg(0));
560 llvm::APSInt Idx(32);
561 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000562 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000563 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000564 return;
565 }
566 priority = Idx.getZExtValue();
567 }
Mike Stumpbf916502009-07-24 19:02:52 +0000568
Anders Carlsson6782fc62008-08-22 22:10:48 +0000569 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000570 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000571 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000572 return;
573 }
574
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000575 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000576}
577
Chris Lattner803d0802008-06-29 00:43:07 +0000578static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000579 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000580 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000582 return;
583 }
Mike Stumpbf916502009-07-24 19:02:52 +0000584
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000585 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000586}
587
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000588static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
589 // check the attribute arguments.
590 if (Attr.getNumArgs() != 0) {
591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
592 return;
593 }
Mike Stumpbf916502009-07-24 19:02:52 +0000594
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000595 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000596}
597
Chris Lattner803d0802008-06-29 00:43:07 +0000598static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000599 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000600 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000602 return;
603 }
Mike Stumpbf916502009-07-24 19:02:52 +0000604
Chris Lattner545dd342008-06-28 23:36:30 +0000605 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 Arg = Arg->IgnoreParenCasts();
607 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000608
Chris Lattner6b6b5372008-06-26 18:38:35 +0000609 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000611 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000612 return;
613 }
Mike Stumpbf916502009-07-24 19:02:52 +0000614
Chris Lattner6b6b5372008-06-26 18:38:35 +0000615 const char *TypeStr = Str->getStrData();
616 unsigned TypeLen = Str->getByteLength();
617 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000618
Chris Lattner6b6b5372008-06-26 18:38:35 +0000619 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
620 type = VisibilityAttr::DefaultVisibility;
621 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
622 type = VisibilityAttr::HiddenVisibility;
623 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
624 type = VisibilityAttr::HiddenVisibility; // FIXME
625 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
626 type = VisibilityAttr::ProtectedVisibility;
627 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000628 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000629 return;
630 }
Mike Stumpbf916502009-07-24 19:02:52 +0000631
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000632 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000633}
634
Chris Lattner0db29ec2009-02-14 08:09:34 +0000635static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
636 Sema &S) {
637 if (Attr.getNumArgs() != 0) {
638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
639 return;
640 }
Mike Stumpbf916502009-07-24 19:02:52 +0000641
Chris Lattner0db29ec2009-02-14 08:09:34 +0000642 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
643 if (OCI == 0) {
644 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
645 return;
646 }
Mike Stumpbf916502009-07-24 19:02:52 +0000647
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000648 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000649}
650
651static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000652 if (Attr.getNumArgs() != 0) {
653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
654 return;
655 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000656 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000657 QualType T = TD->getUnderlyingType();
658 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000659 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000660 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
661 return;
662 }
663 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000664 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000665}
666
Mike Stumpbf916502009-07-24 19:02:52 +0000667static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000668HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
669 if (Attr.getNumArgs() != 0) {
670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
671 return;
672 }
673
674 if (!isa<FunctionDecl>(D)) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
676 return;
677 }
678
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000679 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000680}
681
Steve Naroff9eae5762008-09-18 16:44:58 +0000682static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000683 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000684 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000685 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000686 return;
687 }
Mike Stumpbf916502009-07-24 19:02:52 +0000688
Steve Naroff9eae5762008-09-18 16:44:58 +0000689 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000691 return;
692 }
Mike Stumpbf916502009-07-24 19:02:52 +0000693
Steve Naroff9eae5762008-09-18 16:44:58 +0000694 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000695 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000696 type = BlocksAttr::ByRef;
697 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000698 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000699 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000700 return;
701 }
Mike Stumpbf916502009-07-24 19:02:52 +0000702
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000703 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000704}
705
Anders Carlsson77091822008-10-05 18:05:59 +0000706static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
707 // check the attribute arguments.
708 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000709 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
710 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000711 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000712 }
713
Anders Carlsson77091822008-10-05 18:05:59 +0000714 int sentinel = 0;
715 if (Attr.getNumArgs() > 0) {
716 Expr *E = static_cast<Expr *>(Attr.getArg(0));
717 llvm::APSInt Idx(32);
718 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000720 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000721 return;
722 }
723 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000724
Anders Carlsson77091822008-10-05 18:05:59 +0000725 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000726 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
727 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000728 return;
729 }
730 }
731
732 int nullPos = 0;
733 if (Attr.getNumArgs() > 1) {
734 Expr *E = static_cast<Expr *>(Attr.getArg(1));
735 llvm::APSInt Idx(32);
736 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000738 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000739 return;
740 }
741 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000742
Anders Carlsson77091822008-10-05 18:05:59 +0000743 if (nullPos > 1 || nullPos < 0) {
744 // FIXME: This error message could be improved, it would be nice
745 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
747 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000748 return;
749 }
750 }
751
752 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000753 const FunctionType *FT = FD->getType()->getAsFunctionType();
754 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000755
Chris Lattner897cd902009-03-17 23:03:47 +0000756 if (isa<FunctionNoProtoType>(FT)) {
757 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
758 return;
759 }
Mike Stumpbf916502009-07-24 19:02:52 +0000760
Chris Lattner897cd902009-03-17 23:03:47 +0000761 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000762 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000763 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000764 }
Anders Carlsson77091822008-10-05 18:05:59 +0000765 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
766 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000767 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000768 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000769 }
770 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000771 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
772 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000773 ;
774 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000775 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000776 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000777 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Ted Kremenek6217b802009-07-29 21:53:49 +0000778 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000779 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000780 int m = Ty->isFunctionPointerType() ? 0 : 1;
781 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000782 return;
783 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000784 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000785 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000786 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000787 return;
788 }
Anders Carlsson77091822008-10-05 18:05:59 +0000789 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000790 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000791 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000792 return;
793 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000794 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000795}
796
Chris Lattner026dc962009-02-14 07:37:35 +0000797static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
798 // check the attribute arguments.
799 if (Attr.getNumArgs() != 0) {
800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
801 return;
802 }
803
804 // TODO: could also be applied to methods?
805 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
806 if (!Fn) {
807 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000808 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000809 return;
810 }
Mike Stumpbf916502009-07-24 19:02:52 +0000811
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000812 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000813}
814
815static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000816 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000817 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000818 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000819 return;
820 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000821
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000822 /* weak only applies to non-static declarations */
823 bool isStatic = false;
824 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
825 isStatic = VD->getStorageClass() == VarDecl::Static;
826 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
827 isStatic = FD->getStorageClass() == FunctionDecl::Static;
828 }
829 if (isStatic) {
830 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
831 dyn_cast<NamedDecl>(D)->getNameAsString();
832 return;
833 }
834
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000835 // TODO: could also be applied to methods?
836 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000838 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000839 return;
840 }
Mike Stumpbf916502009-07-24 19:02:52 +0000841
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000842 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000843}
844
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000845static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
846 // check the attribute arguments.
847 if (Attr.getNumArgs() != 0) {
848 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
849 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000850 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000851
852 // weak_import only applies to variable & function declarations.
853 bool isDef = false;
854 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
855 isDef = (!VD->hasExternalStorage() || VD->getInit());
856 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000857 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000858 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
859 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000860 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000861 } else {
862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000863 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000864 return;
865 }
866
867 // Merge should handle any subsequent violations.
868 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000869 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000870 diag::warn_attribute_weak_import_invalid_on_definition)
871 << "weak_import" << 2 /*variable and function*/;
872 return;
873 }
874
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000875 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000876}
877
Chris Lattner026dc962009-02-14 07:37:35 +0000878static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000879 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000880 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000881 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000882 return;
883 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000884
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000885 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000886 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000887 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000888 return;
889 }
890
Chris Lattner026dc962009-02-14 07:37:35 +0000891 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000892 if (!FD) {
893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000894 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000895 return;
896 }
897
898 // Currently, the dllimport attribute is ignored for inlined functions.
899 // Warning is emitted.
900 if (FD->isInline()) {
901 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
902 return;
903 }
904
905 // The attribute is also overridden by a subsequent declaration as dllexport.
906 // Warning is emitted.
907 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
908 nextAttr = nextAttr->getNext()) {
909 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
910 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
911 return;
912 }
913 }
914
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000915 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
917 return;
918 }
919
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000920 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921}
922
Chris Lattner026dc962009-02-14 07:37:35 +0000923static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000924 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000925 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000927 return;
928 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000929
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000930 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000931 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000932 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000933 return;
934 }
935
Chris Lattner026dc962009-02-14 07:37:35 +0000936 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000937 if (!FD) {
938 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000939 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000940 return;
941 }
942
Mike Stumpbf916502009-07-24 19:02:52 +0000943 // Currently, the dllexport attribute is ignored for inlined functions, unless
944 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000945 if (FD->isInline()) {
946 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
947 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
948 return;
949 }
950
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000951 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000952}
953
Nate Begeman6f3d8382009-06-26 06:32:41 +0000954static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
955 Sema &S) {
956 // Attribute has 3 arguments.
957 if (Attr.getNumArgs() != 3) {
958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
959 return;
960 }
961
962 unsigned WGSize[3];
963 for (unsigned i = 0; i < 3; ++i) {
964 Expr *E = static_cast<Expr *>(Attr.getArg(i));
965 llvm::APSInt ArgNum(32);
966 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
968 << "reqd_work_group_size" << E->getSourceRange();
969 return;
970 }
971 WGSize[i] = (unsigned) ArgNum.getZExtValue();
972 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000973 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000974 WGSize[2]));
975}
976
Chris Lattner026dc962009-02-14 07:37:35 +0000977static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000978 // Attribute has no arguments.
979 if (Attr.getNumArgs() != 1) {
980 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
981 return;
982 }
983
984 // Make sure that there is a string literal as the sections's single
985 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000986 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
987 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000988 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000989 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000990 return;
991 }
Chris Lattner797c3c42009-08-10 19:03:04 +0000992
993 std::string SectionStr(SE->getStrData(), SE->getByteLength());
994
995 // If the target wants to validate the section specifier, make it happen.
996 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
997 if (Error.empty()) {
998 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
999 return;
1000 }
1001
1002 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1003 << Error;
1004
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001005}
1006
Chris Lattner803d0802008-06-29 00:43:07 +00001007static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001008 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001009 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001011 return;
1012 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001013
1014 // Attribute can be applied only to functions.
1015 if (!isa<FunctionDecl>(d)) {
1016 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001017 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001018 return;
1019 }
1020
1021 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001022 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001023 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1024 << "stdcall" << "fastcall";
1025 return;
1026 }
1027
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001028 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001029}
1030
Chris Lattner803d0802008-06-29 00:43:07 +00001031static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001032 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001033 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001035 return;
1036 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001037
1038 if (!isa<FunctionDecl>(d)) {
1039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001040 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001041 return;
1042 }
1043
1044 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001045 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001046 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1047 << "fastcall" << "stdcall";
1048 return;
1049 }
1050
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001051 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001052}
1053
Chris Lattner803d0802008-06-29 00:43:07 +00001054static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001055 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001056 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001057 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001058 return;
1059 }
Mike Stumpbf916502009-07-24 19:02:52 +00001060
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001061 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001062}
1063
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001064static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1065 // check the attribute arguments.
1066 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001067 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001068 return;
1069 }
Mike Stumpbf916502009-07-24 19:02:52 +00001070
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001071 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001072}
1073
1074static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1075 // check the attribute arguments.
1076 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001078 return;
1079 }
Mike Stumpbf916502009-07-24 19:02:52 +00001080
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001081 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001082}
1083
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001084static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001085 // Match gcc which ignores cleanup attrs when compiling C++.
1086 if (S.getLangOptions().CPlusPlus)
1087 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001088
1089 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1091 return;
1092 }
Mike Stumpbf916502009-07-24 19:02:52 +00001093
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001094 if (Attr.getNumArgs() != 0) {
1095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1096 return;
1097 }
Mike Stumpbf916502009-07-24 19:02:52 +00001098
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001099 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001100
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001101 if (!VD || !VD->hasLocalStorage()) {
1102 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1103 return;
1104 }
Mike Stumpbf916502009-07-24 19:02:52 +00001105
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001106 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001107 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1108 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001109 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001110 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001111 Attr.getParameterName();
1112 return;
1113 }
Mike Stumpbf916502009-07-24 19:02:52 +00001114
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001115 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1116 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001117 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001118 Attr.getParameterName();
1119 return;
1120 }
1121
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001122 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001123 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001124 Attr.getParameterName();
1125 return;
1126 }
Mike Stumpbf916502009-07-24 19:02:52 +00001127
Anders Carlsson89941c12009-02-07 23:16:50 +00001128 // We're currently more strict than GCC about what function types we accept.
1129 // If this ever proves to be a problem it should be easy to fix.
1130 QualType Ty = S.Context.getPointerType(VD->getType());
1131 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001132 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001133 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001134 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1135 Attr.getParameterName() << ParamTy << Ty;
1136 return;
1137 }
Mike Stumpbf916502009-07-24 19:02:52 +00001138
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001139 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001140}
1141
Mike Stumpbf916502009-07-24 19:02:52 +00001142/// Handle __attribute__((format_arg((idx)))) attribute based on
1143/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1144static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001145 if (Attr.getNumArgs() != 1) {
1146 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1147 return;
1148 }
1149 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1150 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1151 << Attr.getName() << 0 /*function*/;
1152 return;
1153 }
Mike Stumpbf916502009-07-24 19:02:52 +00001154 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1155 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001156 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1157 unsigned FirstIdx = 1;
1158 // checks for the 2nd argument
1159 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1160 llvm::APSInt Idx(32);
1161 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1162 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1163 << "format" << 2 << IdxExpr->getSourceRange();
1164 return;
1165 }
Mike Stumpbf916502009-07-24 19:02:52 +00001166
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001167 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1169 << "format" << 2 << IdxExpr->getSourceRange();
1170 return;
1171 }
Mike Stumpbf916502009-07-24 19:02:52 +00001172
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001173 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001174
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001175 // make sure the format string is really a string
1176 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001177
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001178 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1179 if (not_nsstring_type &&
1180 !isCFStringType(Ty, S.Context) &&
1181 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001182 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001183 // FIXME: Should highlight the actual expression that has the wrong type.
1184 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001185 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001186 << IdxExpr->getSourceRange();
1187 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001188 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001189 Ty = getFunctionOrMethodResultType(d);
1190 if (!isNSStringType(Ty, S.Context) &&
1191 !isCFStringType(Ty, S.Context) &&
1192 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001193 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001194 // FIXME: Should highlight the actual expression that has the wrong type.
1195 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001196 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001197 << IdxExpr->getSourceRange();
1198 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001199 }
1200
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001201 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001202}
1203
Mike Stumpbf916502009-07-24 19:02:52 +00001204/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1205/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001206static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001207
Chris Lattner545dd342008-06-28 23:36:30 +00001208 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001209 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001210 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001211 return;
1212 }
1213
Chris Lattner545dd342008-06-28 23:36:30 +00001214 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001215 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001216 return;
1217 }
1218
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001219 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001220 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001221 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001222 return;
1223 }
1224
Mike Stumpbf916502009-07-24 19:02:52 +00001225 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1226 // needed in order to be compatible with GCC the index must start in 1 and the
1227 // limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001228 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001229 unsigned FirstIdx = 1;
1230
Chris Lattner545dd342008-06-28 23:36:30 +00001231 const char *Format = Attr.getParameterName()->getName();
1232 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001233
1234 // Normalize the argument, __foo__ becomes foo.
1235 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1236 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1237 Format += 2;
1238 FormatLen -= 4;
1239 }
1240
1241 bool Supported = false;
1242 bool is_NSString = false;
1243 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001244 bool is_CFString = false;
Mike Stumpbf916502009-07-24 19:02:52 +00001245
Chris Lattner6b6b5372008-06-26 18:38:35 +00001246 switch (FormatLen) {
1247 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001248 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1249 case 6: Supported = !memcmp(Format, "printf", 6); break;
Ryan Flynn4403a5e2009-08-06 03:00:50 +00001250 case 7: Supported = !memcmp(Format, "printf0", 7) ||
1251 !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001252 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001253 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1254 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1255 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001256 break;
1257 }
Mike Stumpbf916502009-07-24 19:02:52 +00001258
Chris Lattner6b6b5372008-06-26 18:38:35 +00001259 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001260 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1261 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001262 return;
1263 }
1264
1265 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001266 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001267 llvm::APSInt Idx(32);
1268 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001269 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001270 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001271 return;
1272 }
1273
1274 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001276 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001277 return;
1278 }
1279
1280 // FIXME: Do we need to bounds check?
1281 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001282
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001284 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001285
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001286 if (is_CFString) {
1287 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001288 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1289 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001290 return;
1291 }
1292 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001293 // FIXME: do we need to check if the type is NSString*? What are the
1294 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001295 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001296 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001297 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1298 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001299 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001300 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001301 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001302 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
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 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001306 return;
1307 }
1308
1309 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001310 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001311 llvm::APSInt FirstArg(32);
1312 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001314 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001315 return;
1316 }
1317
1318 // check if the function is variadic if the 3rd argument non-zero
1319 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001320 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001321 ++NumArgs; // +1 for ...
1322 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001323 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001324 return;
1325 }
1326 }
1327
Chris Lattner3c73c412008-11-19 08:23:25 +00001328 // strftime requires FirstArg to be 0 because it doesn't read from any
1329 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001330 if (is_strftime) {
1331 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001332 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1333 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001334 return;
1335 }
1336 // if 0 it disables parameter checking (to use with e.g. va_list)
1337 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001338 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001339 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001340 return;
1341 }
1342
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001343 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001344 Idx.getZExtValue(), FirstArg.getZExtValue()));
1345}
1346
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001347static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1348 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001349 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001350 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001352 return;
1353 }
1354
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001355 // Try to find the underlying union declaration.
1356 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001357 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001358 if (TD && TD->getUnderlyingType()->isUnionType())
1359 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1360 else
1361 RD = dyn_cast<RecordDecl>(d);
1362
1363 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001364 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001365 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001366 return;
1367 }
1368
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001369 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001370 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001371 diag::warn_transparent_union_attribute_not_definition);
1372 return;
1373 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001374
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001375 RecordDecl::field_iterator Field = RD->field_begin(),
1376 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001377 if (Field == FieldEnd) {
1378 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1379 return;
1380 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001381
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001382 FieldDecl *FirstField = *Field;
1383 QualType FirstType = FirstField->getType();
1384 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001385 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001386 diag::warn_transparent_union_attribute_floating);
1387 return;
1388 }
1389
1390 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1391 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1392 for (; Field != FieldEnd; ++Field) {
1393 QualType FieldType = Field->getType();
1394 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1395 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1396 // Warn if we drop the attribute.
1397 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001398 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001399 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001400 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001401 diag::warn_transparent_union_attribute_field_size_align)
1402 << isSize << Field->getDeclName() << FieldBits;
1403 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001404 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001405 diag::note_transparent_union_first_field_size_align)
1406 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001407 return;
1408 }
1409 }
1410
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001411 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001412}
1413
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001414static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001415 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001416 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001417 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001418 return;
1419 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001420 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1421 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001422
Chris Lattner6b6b5372008-06-26 18:38:35 +00001423 // Make sure that there is a string literal as the annotation's single
1424 // argument.
1425 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001426 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001427 return;
1428 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001429 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001430 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001431}
1432
Chris Lattner803d0802008-06-29 00:43:07 +00001433static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001434 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001435 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001436 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001437 return;
1438 }
1439
1440 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001441 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001442 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001443 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001445 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001446 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001447 }
Mike Stumpbf916502009-07-24 19:02:52 +00001448
Chris Lattner49e2d342008-06-28 23:50:44 +00001449 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1450 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001451 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001452 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1453 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001454 return;
1455 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001456 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001458 << alignmentExpr->getSourceRange();
1459 return;
1460 }
1461
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001462 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001463}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001464
Mike Stumpbf916502009-07-24 19:02:52 +00001465/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1466/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001467///
Mike Stumpbf916502009-07-24 19:02:52 +00001468/// Despite what would be logical, the mode attribute is a decl attribute, not a
1469/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1470/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001471static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001472 // This attribute isn't documented, but glibc uses it. It changes
1473 // the width of an int or unsigned int to the specified size.
1474
1475 // Check that there aren't any arguments
1476 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001477 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001478 return;
1479 }
1480
1481 IdentifierInfo *Name = Attr.getParameterName();
1482 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001484 return;
1485 }
1486 const char *Str = Name->getName();
1487 unsigned Len = Name->getLength();
1488
1489 // Normalize the attribute name, __foo__ becomes foo.
1490 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1491 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1492 Str += 2;
1493 Len -= 4;
1494 }
1495
1496 unsigned DestWidth = 0;
1497 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001498 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001499 switch (Len) {
1500 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001501 switch (Str[0]) {
1502 case 'Q': DestWidth = 8; break;
1503 case 'H': DestWidth = 16; break;
1504 case 'S': DestWidth = 32; break;
1505 case 'D': DestWidth = 64; break;
1506 case 'X': DestWidth = 96; break;
1507 case 'T': DestWidth = 128; break;
1508 }
1509 if (Str[1] == 'F') {
1510 IntegerMode = false;
1511 } else if (Str[1] == 'C') {
1512 IntegerMode = false;
1513 ComplexMode = true;
1514 } else if (Str[1] != 'I') {
1515 DestWidth = 0;
1516 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001517 break;
1518 case 4:
1519 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1520 // pointer on PIC16 and other embedded platforms.
1521 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001522 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001523 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001524 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001525 break;
1526 case 7:
1527 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001528 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001529 break;
1530 }
1531
1532 QualType OldTy;
1533 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1534 OldTy = TD->getUnderlyingType();
1535 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1536 OldTy = VD->getType();
1537 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001538 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1539 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001540 return;
1541 }
Eli Friedman73397492009-03-03 06:41:03 +00001542
1543 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1544 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1545 else if (IntegerMode) {
1546 if (!OldTy->isIntegralType())
1547 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1548 } else if (ComplexMode) {
1549 if (!OldTy->isComplexType())
1550 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1551 } else {
1552 if (!OldTy->isFloatingType())
1553 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1554 }
1555
Mike Stump390b4cc2009-05-16 07:39:55 +00001556 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1557 // and friends, at least with glibc.
1558 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1559 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001560 // FIXME: Make sure floating-point mappings are accurate
1561 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001562 QualType NewTy;
1563 switch (DestWidth) {
1564 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001565 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001566 return;
1567 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001568 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001569 return;
1570 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001571 if (!IntegerMode) {
1572 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1573 return;
1574 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001575 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001576 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001577 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001578 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001579 break;
1580 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001581 if (!IntegerMode) {
1582 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1583 return;
1584 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001585 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001586 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001588 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001589 break;
1590 case 32:
1591 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001592 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001593 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001594 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001596 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001597 break;
1598 case 64:
1599 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001600 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001601 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001602 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001603 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001604 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001605 break;
Eli Friedman73397492009-03-03 06:41:03 +00001606 case 96:
1607 NewTy = S.Context.LongDoubleTy;
1608 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001609 case 128:
1610 if (!IntegerMode) {
1611 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1612 return;
1613 }
1614 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001615 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001616 }
1617
Eli Friedman73397492009-03-03 06:41:03 +00001618 if (ComplexMode) {
1619 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001620 }
1621
1622 // Install the new type.
1623 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1624 TD->setUnderlyingType(NewTy);
1625 else
1626 cast<ValueDecl>(D)->setType(NewTy);
1627}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001628
Anders Carlssond87df372009-02-13 06:46:13 +00001629static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1630 // check the attribute arguments.
1631 if (Attr.getNumArgs() > 0) {
1632 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1633 return;
1634 }
Anders Carlssone896d982009-02-13 08:11:52 +00001635
Anders Carlsson5bab7882009-02-19 19:16:48 +00001636 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001638 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001639 return;
1640 }
Mike Stumpbf916502009-07-24 19:02:52 +00001641
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001642 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001643}
1644
Anders Carlsson5bab7882009-02-19 19:16:48 +00001645static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1646 // check the attribute arguments.
1647 if (Attr.getNumArgs() != 0) {
1648 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1649 return;
1650 }
Mike Stumpbf916502009-07-24 19:02:52 +00001651
Chris Lattnerc5197432009-04-14 17:02:11 +00001652 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001654 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001655 return;
1656 }
Mike Stumpbf916502009-07-24 19:02:52 +00001657
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001658 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001659}
1660
Chris Lattnercf2a7212009-04-20 19:12:28 +00001661static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001662 // check the attribute arguments.
1663 if (Attr.getNumArgs() != 0) {
1664 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1665 return;
1666 }
Mike Stumpbf916502009-07-24 19:02:52 +00001667
Chris Lattnerc5197432009-04-14 17:02:11 +00001668 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1669 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001671 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001672 return;
1673 }
Mike Stumpbf916502009-07-24 19:02:52 +00001674
Chris Lattnerc5197432009-04-14 17:02:11 +00001675 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001676 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001677 return;
1678 }
Mike Stumpbf916502009-07-24 19:02:52 +00001679
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001680 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001681}
1682
Fariborz Jahanianee760332009-03-27 18:38:55 +00001683static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1684 // check the attribute arguments.
1685 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001686 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001687 return;
1688 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001689
Fariborz Jahanianee760332009-03-27 18:38:55 +00001690 if (!isFunctionOrMethod(d)) {
1691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001692 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001693 return;
1694 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001695
1696 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1697 llvm::APSInt NumParams(32);
1698 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1699 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1700 << "regparm" << NumParamsExpr->getSourceRange();
1701 return;
1702 }
1703
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001704 if (S.Context.Target.getRegParmMax() == 0) {
1705 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001706 << NumParamsExpr->getSourceRange();
1707 return;
1708 }
1709
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001710 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001711 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1712 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001713 return;
1714 }
1715
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001716 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001717}
1718
Chris Lattner0744e5f2008-06-29 00:23:49 +00001719//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001720// Checker-specific attribute handlers.
1721//===----------------------------------------------------------------------===//
1722
1723static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1724 Sema &S) {
1725
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001726 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001727
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001728 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1729 RetTy = MD->getResultType();
1730 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1731 RetTy = FD->getResultType();
1732 else {
1733 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1734 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001735 return;
1736 }
Mike Stumpbf916502009-07-24 19:02:52 +00001737
Ted Kremenek6217b802009-07-29 21:53:49 +00001738 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
Steve Naroff14108da2009-07-10 23:34:53 +00001739 || RetTy->getAsObjCObjectPointerType())) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001740 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1741 << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001742 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001743 }
Mike Stumpbf916502009-07-24 19:02:52 +00001744
Ted Kremenekb71368d2009-05-09 02:44:38 +00001745 switch (Attr.getKind()) {
1746 default:
1747 assert(0 && "invalid ownership attribute");
1748 return;
1749 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001750 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001751 return;
1752 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001753 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001754 return;
1755 };
1756}
1757
1758//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001759// Top Level Sema Entry Points
1760//===----------------------------------------------------------------------===//
1761
Sebastian Redla89d82c2008-12-21 19:24:58 +00001762/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001763/// the attribute applies to decls. If the attribute is a type attribute, just
1764/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001765static void ProcessDeclAttribute(Scope *scope, Decl *D,
1766 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001767 if (Attr.isDeclspecAttribute())
1768 // FIXME: Try to deal with __declspec attributes!
1769 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001770 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001771 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001772 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001773 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001774 // Ignore these, these are type attributes, handled by
1775 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001776 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001777 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1778 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001779 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001780 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001781 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001782 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001783 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1784 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1785 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1786 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1787 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1788 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001789 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001790 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001791 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001792 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1793 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001794 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001795 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001796 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001797 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001798 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1799 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1800 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001801
1802 // Checker-specific.
1803 case AttributeList::AT_ns_returns_retained:
1804 case AttributeList::AT_cf_returns_retained:
1805 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1806
Nate Begeman6f3d8382009-06-26 06:32:41 +00001807 case AttributeList::AT_reqd_wg_size:
1808 HandleReqdWorkGroupSize(D, Attr, S); break;
1809
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001810 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001811 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001812 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001813 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001814 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001815 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001816 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001817 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001818 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1819 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001820 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001821 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001822 case AttributeList::AT_transparent_union:
1823 HandleTransparentUnionAttr(D, Attr, S);
1824 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001825 case AttributeList::AT_objc_exception:
1826 HandleObjCExceptionAttr(D, Attr, S);
1827 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001828 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001829 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001830 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001831 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001832 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1833 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001834 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001835 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001836 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001837 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001838 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001839 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001840 // Just ignore
1841 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001842 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001844 break;
1845 }
1846}
1847
1848/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1849/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001850void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001851 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001852 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001853 AttrList = AttrList->getNext();
1854 }
1855}
1856
Ryan Flynne25ff832009-07-30 03:15:39 +00001857/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1858/// #pragma weak needs a non-definition decl and source may not have one
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001859NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II)
Ryan Flynne25ff832009-07-30 03:15:39 +00001860{
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001861 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001862 NamedDecl *NewD = 0;
1863 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1864 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1865 FD->getLocation(), DeclarationName(II),
1866 FD->getType());
1867 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1868 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1869 VD->getLocation(), II,
1870 VD->getType(), VD->getStorageClass());
1871 }
1872 return NewD;
1873}
1874
1875/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1876/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001877void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001878 if (!W.getUsed()) { // only do this once
1879 W.setUsed(true);
1880 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1881 IdentifierInfo *NDId = ND->getIdentifier();
1882 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1883 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1884 NewD->addAttr(::new (Context) WeakAttr());
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001885 WeakTopLevelDecl.push_back(NewD);
1886 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1887 // to insert Decl at TU scope, sorry.
1888 DeclContext *SavedContext = CurContext;
1889 CurContext = Context.getTranslationUnitDecl();
1890 PushOnScopeChains(NewD, S);
1891 CurContext = SavedContext;
Ryan Flynne25ff832009-07-30 03:15:39 +00001892 } else { // just add weak to existing
1893 ND->addAttr(::new (Context) WeakAttr());
1894 }
1895 }
1896}
1897
Chris Lattner0744e5f2008-06-29 00:23:49 +00001898/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1899/// it, apply them to D. This is a bit tricky because PD can have attributes
1900/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001901void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001902 // Handle #pragma weak
1903 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1904 if (ND->hasLinkage()) {
1905 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1906 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001907 // Identifier referenced by #pragma weak before it was declared
1908 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001909 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1910 }
1911 }
1912 }
1913
Chris Lattner0744e5f2008-06-29 00:23:49 +00001914 // Apply decl attributes from the DeclSpec if present.
1915 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001916 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001917
Chris Lattner0744e5f2008-06-29 00:23:49 +00001918 // Walk the declarator structure, applying decl attributes that were in a type
1919 // position to the decl itself. This handles cases like:
1920 // int *__attr__(x)** D;
1921 // when X is a decl attribute.
1922 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1923 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001924 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001925
Chris Lattner0744e5f2008-06-29 00:23:49 +00001926 // Finally, apply any attributes on the decl itself.
1927 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001928 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001929}