blob: e57111f6cd882208e918335ee2b6f111721c2b05 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Ted Kremeneka18d7d82009-08-14 20:49:40 +000027static const FunctionType *getFunctionType(const Decl *d,
28 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000030 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000031 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000032 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000033 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000034 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000035 Ty = decl->getUnderlyingType();
36 else
37 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000038
Chris Lattner6b6b5372008-06-26 18:38:35 +000039 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000040 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000041 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000042 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000043
John McCall183700f2009-09-21 23:43:11 +000044 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000045}
46
Daniel Dunbar35682492008-09-26 04:12:28 +000047// FIXME: We should provide an abstraction around a method or function
48// to provide the following bits of information.
49
Daniel Dunbard3f2c102008-10-19 02:04:16 +000050/// isFunctionOrMethod - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000051/// type (function or function-typed variable).
52static bool isFunction(const Decl *d) {
53 return getFunctionType(d, false) != NULL;
54}
55
56/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000057/// type (function or function-typed variable) or an Objective-C
58/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000059static bool isFunctionOrMethod(const Decl *d) {
60 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000061}
62
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000063/// isFunctionOrMethodOrBlock - Return true if the given decl has function
64/// type (function or function-typed variable) or an Objective-C
65/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000066static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000067 if (isFunctionOrMethod(d))
68 return true;
69 // check for block is more involved.
70 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
71 QualType Ty = V->getType();
72 return Ty->isBlockPointerType();
73 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000074 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000075}
76
Daniel Dunbard3f2c102008-10-19 02:04:16 +000077/// hasFunctionProto - Return true if the given decl has a argument
78/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000079/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000080static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000081 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000082 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000083 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000084 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000085 return true;
86 }
87}
88
89/// getFunctionOrMethodNumArgs - Return number of function or method
90/// arguments. It is an error to call this on a K&R function (use
91/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000092static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000093 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000094 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
96 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000097 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000098}
99
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000100static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000101 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000102 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000103 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
104 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000105
Chris Lattner89951a82009-02-20 18:43:26 +0000106 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000107}
108
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000109static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000110 if (const FunctionType *FnTy = getFunctionType(d))
111 return cast<FunctionProtoType>(FnTy)->getResultType();
112 return cast<ObjCMethodDecl>(d)->getResultType();
113}
114
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000115static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000116 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000118 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000119 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
120 return BD->IsVariadic();
121 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000122 return cast<ObjCMethodDecl>(d)->isVariadic();
123 }
124}
125
Chris Lattner6b6b5372008-06-26 18:38:35 +0000126static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000127 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000128 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000129 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000130
John McCall183700f2009-09-21 23:43:11 +0000131 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000132 if (!ClsT)
133 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000134
Chris Lattner6b6b5372008-06-26 18:38:35 +0000135 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000136
Chris Lattner6b6b5372008-06-26 18:38:35 +0000137 // FIXME: Should we walk the chain of classes?
138 return ClsName == &Ctx.Idents.get("NSString") ||
139 ClsName == &Ctx.Idents.get("NSMutableString");
140}
141
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000142static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000143 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000144 if (!PT)
145 return false;
146
Ted Kremenek6217b802009-07-29 21:53:49 +0000147 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000148 if (!RT)
149 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000150
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000151 const RecordDecl *RD = RT->getDecl();
152 if (RD->getTagKind() != TagDecl::TK_struct)
153 return false;
154
155 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
156}
157
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000158//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159// Attribute Implementations
160//===----------------------------------------------------------------------===//
161
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000162// FIXME: All this manual attribute parsing code is gross. At the
163// least add some helper functions to check most argument patterns (#
164// and types of args).
165
Mike Stumpbf916502009-07-24 19:02:52 +0000166static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000167 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000168 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
169 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000171 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 }
Mike Stumpbf916502009-07-24 19:02:52 +0000173
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000175
176 Expr *sizeExpr;
177
178 // Special case where the argument is a template id.
179 if (Attr.getParameterName()) {
180 sizeExpr = S.ActOnDeclarationNameExpr(scope, Attr.getLoc(),
181 Attr.getParameterName(),
182 false, 0, false).takeAs<Expr>();
183 } else {
184 // check the attribute arguments.
185 if (Attr.getNumArgs() != 1) {
186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
187 return;
188 }
189 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000190 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000191
192 // Instantiate/Install the vector type, and let Sema build the type for us.
193 // This will run the reguired checks.
194 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
195 if (!T.isNull()) {
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000441 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000442 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000443 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)) {
John McCall183700f2009-09-21 23:43:11 +0000753 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000754 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)
John McCall183700f2009-09-21 23:43:11 +0000778 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Chris Lattner797c3c42009-08-10 19:03:04 +0000993 std::string SectionStr(SE->getStrData(), SE->getByteLength());
994
995 // If the target wants to validate the section specifier, make it happen.
996 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
997 if (Error.empty()) {
998 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
999 return;
1000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattner797c3c42009-08-10 19:03:04 +00001002 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1003 << Error;
Mike Stump1eb44332009-09-09 15:08:12 +00001004
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
John McCallf36e02d2009-10-09 21:13:30 +00001107 NamedDecl *CleanupDecl
1108 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1109 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001110 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001111 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001112 Attr.getParameterName();
1113 return;
1114 }
Mike Stumpbf916502009-07-24 19:02:52 +00001115
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001116 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1117 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001118 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001119 Attr.getParameterName();
1120 return;
1121 }
1122
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001123 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001124 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001125 Attr.getParameterName();
1126 return;
1127 }
Mike Stumpbf916502009-07-24 19:02:52 +00001128
Anders Carlsson89941c12009-02-07 23:16:50 +00001129 // We're currently more strict than GCC about what function types we accept.
1130 // If this ever proves to be a problem it should be easy to fix.
1131 QualType Ty = S.Context.getPointerType(VD->getType());
1132 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001133 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001134 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001135 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1136 Attr.getParameterName() << ParamTy << Ty;
1137 return;
1138 }
Mike Stumpbf916502009-07-24 19:02:52 +00001139
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001140 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001141}
1142
Mike Stumpbf916502009-07-24 19:02:52 +00001143/// Handle __attribute__((format_arg((idx)))) attribute based on
1144/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1145static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001146 if (Attr.getNumArgs() != 1) {
1147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1148 return;
1149 }
1150 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1151 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1152 << Attr.getName() << 0 /*function*/;
1153 return;
1154 }
Mike Stumpbf916502009-07-24 19:02:52 +00001155 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1156 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001157 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1158 unsigned FirstIdx = 1;
1159 // checks for the 2nd argument
1160 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1161 llvm::APSInt Idx(32);
1162 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1163 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1164 << "format" << 2 << IdxExpr->getSourceRange();
1165 return;
1166 }
Mike Stumpbf916502009-07-24 19:02:52 +00001167
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001168 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1170 << "format" << 2 << IdxExpr->getSourceRange();
1171 return;
1172 }
Mike Stumpbf916502009-07-24 19:02:52 +00001173
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001174 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001175
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001176 // make sure the format string is really a string
1177 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001178
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001179 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1180 if (not_nsstring_type &&
1181 !isCFStringType(Ty, S.Context) &&
1182 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001183 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001184 // FIXME: Should highlight the actual expression that has the wrong type.
1185 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001186 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001187 << IdxExpr->getSourceRange();
1188 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001189 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001190 Ty = getFunctionOrMethodResultType(d);
1191 if (!isNSStringType(Ty, S.Context) &&
1192 !isCFStringType(Ty, S.Context) &&
1193 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001194 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001195 // FIXME: Should highlight the actual expression that has the wrong type.
1196 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001197 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001198 << IdxExpr->getSourceRange();
1199 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001200 }
1201
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001202 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001203}
1204
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001205enum FormatAttrKind {
1206 CFStringFormat,
1207 NSStringFormat,
1208 StrftimeFormat,
1209 SupportedFormat,
1210 InvalidFormat
1211};
1212
1213/// getFormatAttrKind - Map from format attribute names to supported format
1214/// types.
1215static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1216 // Check for formats that get handled specially.
1217 if (Format == "NSString")
1218 return NSStringFormat;
1219 if (Format == "CFString")
1220 return CFStringFormat;
1221 if (Format == "strftime")
1222 return StrftimeFormat;
1223
1224 // Otherwise, check for supported formats.
1225 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1226 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1227 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1228 Format == "zcmn_err")
1229 return SupportedFormat;
1230
1231 return InvalidFormat;
1232}
1233
Mike Stumpbf916502009-07-24 19:02:52 +00001234/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1235/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001236static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001237
Chris Lattner545dd342008-06-28 23:36:30 +00001238 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001240 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 return;
1242 }
1243
Chris Lattner545dd342008-06-28 23:36:30 +00001244 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001246 return;
1247 }
1248
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001249 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001251 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001252 return;
1253 }
1254
Daniel Dunbar35682492008-09-26 04:12:28 +00001255 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001256 unsigned FirstIdx = 1;
1257
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001258 llvm::StringRef Format = Attr.getParameterName()->getNameStr();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001259
1260 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001261 if (Format.startswith("__") && Format.endswith("__"))
1262 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001264 // Check for supported formats.
1265 FormatAttrKind Kind = getFormatAttrKind(Format);
1266 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001267 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001268 << "format" << Attr.getParameterName()->getNameStr();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001269 return;
1270 }
1271
1272 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001273 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001274 llvm::APSInt Idx(32);
1275 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001276 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001277 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001278 return;
1279 }
1280
Anders Carlsson4fb77202009-08-25 14:12:34 +00001281 // FIXME: We should handle the implicit 'this' parameter in a more generic
1282 // way that can be used for other arguments.
1283 bool HasImplicitThisParam = false;
1284 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1285 if (MD->isInstance()) {
1286 HasImplicitThisParam = true;
1287 NumArgs++;
1288 }
1289 }
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Chris Lattner6b6b5372008-06-26 18:38:35 +00001291 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001293 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001294 return;
1295 }
1296
1297 // FIXME: Do we need to bounds check?
1298 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001299
Anders Carlsson4fb77202009-08-25 14:12:34 +00001300 if (HasImplicitThisParam) ArgIdx--;
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Chris Lattner6b6b5372008-06-26 18:38:35 +00001302 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001303 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001304
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001305 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001306 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001307 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1308 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001309 return;
1310 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001311 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001312 // FIXME: do we need to check if the type is NSString*? What are the
1313 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001314 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001315 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001316 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1317 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001318 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001319 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001320 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001321 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001322 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001323 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1324 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001325 return;
1326 }
1327
1328 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001329 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001330 llvm::APSInt FirstArg(32);
1331 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001333 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001334 return;
1335 }
1336
1337 // check if the function is variadic if the 3rd argument non-zero
1338 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001339 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001340 ++NumArgs; // +1 for ...
1341 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001342 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001343 return;
1344 }
1345 }
1346
Chris Lattner3c73c412008-11-19 08:23:25 +00001347 // strftime requires FirstArg to be 0 because it doesn't read from any
1348 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001349 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001351 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1352 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001353 return;
1354 }
1355 // if 0 it disables parameter checking (to use with e.g. va_list)
1356 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001358 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001359 return;
1360 }
1361
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001362 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1363 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001364}
1365
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001366static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1367 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001368 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001369 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001371 return;
1372 }
1373
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001374 // Try to find the underlying union declaration.
1375 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001376 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001377 if (TD && TD->getUnderlyingType()->isUnionType())
1378 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1379 else
1380 RD = dyn_cast<RecordDecl>(d);
1381
1382 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001384 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001385 return;
1386 }
1387
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001388 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001389 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001390 diag::warn_transparent_union_attribute_not_definition);
1391 return;
1392 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001393
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001394 RecordDecl::field_iterator Field = RD->field_begin(),
1395 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001396 if (Field == FieldEnd) {
1397 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1398 return;
1399 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001400
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001401 FieldDecl *FirstField = *Field;
1402 QualType FirstType = FirstField->getType();
1403 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001404 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001405 diag::warn_transparent_union_attribute_floating);
1406 return;
1407 }
1408
1409 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1410 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1411 for (; Field != FieldEnd; ++Field) {
1412 QualType FieldType = Field->getType();
1413 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1414 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1415 // Warn if we drop the attribute.
1416 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001417 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001418 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001419 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001420 diag::warn_transparent_union_attribute_field_size_align)
1421 << isSize << Field->getDeclName() << FieldBits;
1422 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001423 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001424 diag::note_transparent_union_first_field_size_align)
1425 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001426 return;
1427 }
1428 }
1429
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001430 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001431}
1432
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001433static void HandleAnnotateAttr(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 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001439 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1440 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001441
Chris Lattner6b6b5372008-06-26 18:38:35 +00001442 // Make sure that there is a string literal as the annotation's single
1443 // argument.
1444 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001445 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001446 return;
1447 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001448 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001449 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001450}
1451
Chris Lattner803d0802008-06-29 00:43:07 +00001452static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001454 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001455 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001456 return;
1457 }
1458
1459 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001460 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001461 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001462 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001463 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001464 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001465 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001466 }
Mike Stumpbf916502009-07-24 19:02:52 +00001467
Chris Lattner49e2d342008-06-28 23:50:44 +00001468 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1469 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001470 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001471 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1472 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001473 return;
1474 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001475 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001476 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001477 << alignmentExpr->getSourceRange();
1478 return;
1479 }
1480
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001481 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001482}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001483
Mike Stumpbf916502009-07-24 19:02:52 +00001484/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1485/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001486///
Mike Stumpbf916502009-07-24 19:02:52 +00001487/// Despite what would be logical, the mode attribute is a decl attribute, not a
1488/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1489/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001490static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001491 // This attribute isn't documented, but glibc uses it. It changes
1492 // the width of an int or unsigned int to the specified size.
1493
1494 // Check that there aren't any arguments
1495 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001496 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001497 return;
1498 }
1499
1500 IdentifierInfo *Name = Attr.getParameterName();
1501 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001502 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001503 return;
1504 }
1505 const char *Str = Name->getName();
1506 unsigned Len = Name->getLength();
1507
1508 // Normalize the attribute name, __foo__ becomes foo.
1509 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1510 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1511 Str += 2;
1512 Len -= 4;
1513 }
1514
1515 unsigned DestWidth = 0;
1516 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001517 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001518 switch (Len) {
1519 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001520 switch (Str[0]) {
1521 case 'Q': DestWidth = 8; break;
1522 case 'H': DestWidth = 16; break;
1523 case 'S': DestWidth = 32; break;
1524 case 'D': DestWidth = 64; break;
1525 case 'X': DestWidth = 96; break;
1526 case 'T': DestWidth = 128; break;
1527 }
1528 if (Str[1] == 'F') {
1529 IntegerMode = false;
1530 } else if (Str[1] == 'C') {
1531 IntegerMode = false;
1532 ComplexMode = true;
1533 } else if (Str[1] != 'I') {
1534 DestWidth = 0;
1535 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001536 break;
1537 case 4:
1538 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1539 // pointer on PIC16 and other embedded platforms.
1540 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001541 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001542 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001543 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001544 break;
1545 case 7:
1546 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001547 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001548 break;
1549 }
1550
1551 QualType OldTy;
1552 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1553 OldTy = TD->getUnderlyingType();
1554 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1555 OldTy = VD->getType();
1556 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001557 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1558 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001559 return;
1560 }
Eli Friedman73397492009-03-03 06:41:03 +00001561
John McCall183700f2009-09-21 23:43:11 +00001562 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001563 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1564 else if (IntegerMode) {
1565 if (!OldTy->isIntegralType())
1566 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1567 } else if (ComplexMode) {
1568 if (!OldTy->isComplexType())
1569 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1570 } else {
1571 if (!OldTy->isFloatingType())
1572 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1573 }
1574
Mike Stump390b4cc2009-05-16 07:39:55 +00001575 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1576 // and friends, at least with glibc.
1577 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1578 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001579 // FIXME: Make sure floating-point mappings are accurate
1580 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001581 QualType NewTy;
1582 switch (DestWidth) {
1583 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001584 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001585 return;
1586 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001587 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001588 return;
1589 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001590 if (!IntegerMode) {
1591 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1592 return;
1593 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001594 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001595 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001596 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001597 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001598 break;
1599 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001600 if (!IntegerMode) {
1601 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1602 return;
1603 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001604 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001605 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001606 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001607 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001608 break;
1609 case 32:
1610 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001611 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001612 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001613 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001614 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001615 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001616 break;
1617 case 64:
1618 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001619 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001620 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001621 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001622 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001623 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001624 break;
Eli Friedman73397492009-03-03 06:41:03 +00001625 case 96:
1626 NewTy = S.Context.LongDoubleTy;
1627 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001628 case 128:
1629 if (!IntegerMode) {
1630 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1631 return;
1632 }
1633 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001634 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001635 }
1636
Eli Friedman73397492009-03-03 06:41:03 +00001637 if (ComplexMode) {
1638 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001639 }
1640
1641 // Install the new type.
1642 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1643 TD->setUnderlyingType(NewTy);
1644 else
1645 cast<ValueDecl>(D)->setType(NewTy);
1646}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001647
Mike Stump1feade82009-08-26 22:31:08 +00001648static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001649 // check the attribute arguments.
1650 if (Attr.getNumArgs() > 0) {
1651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1652 return;
1653 }
Anders Carlssone896d982009-02-13 08:11:52 +00001654
Anders Carlsson5bab7882009-02-19 19:16:48 +00001655 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001656 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001657 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001658 return;
1659 }
Mike Stumpbf916502009-07-24 19:02:52 +00001660
Mike Stump1feade82009-08-26 22:31:08 +00001661 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001662}
1663
Mike Stump1feade82009-08-26 22:31:08 +00001664static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001665 // check the attribute arguments.
1666 if (Attr.getNumArgs() != 0) {
1667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1668 return;
1669 }
Mike Stumpbf916502009-07-24 19:02:52 +00001670
Chris Lattnerc5197432009-04-14 17:02:11 +00001671 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001673 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001674 return;
1675 }
Mike Stumpbf916502009-07-24 19:02:52 +00001676
Mike Stump1feade82009-08-26 22:31:08 +00001677 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001678}
1679
Chris Lattnercf2a7212009-04-20 19:12:28 +00001680static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001681 // check the attribute arguments.
1682 if (Attr.getNumArgs() != 0) {
1683 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1684 return;
1685 }
Mike Stumpbf916502009-07-24 19:02:52 +00001686
Chris Lattnerc5197432009-04-14 17:02:11 +00001687 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1688 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001690 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001691 return;
1692 }
Mike Stumpbf916502009-07-24 19:02:52 +00001693
Chris Lattnerc5197432009-04-14 17:02:11 +00001694 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001695 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001696 return;
1697 }
Mike Stumpbf916502009-07-24 19:02:52 +00001698
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001699 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001700}
1701
Fariborz Jahanianee760332009-03-27 18:38:55 +00001702static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1703 // check the attribute arguments.
1704 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001705 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001706 return;
1707 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001708
Fariborz Jahanianee760332009-03-27 18:38:55 +00001709 if (!isFunctionOrMethod(d)) {
1710 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001711 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001712 return;
1713 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001714
1715 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1716 llvm::APSInt NumParams(32);
1717 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1718 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1719 << "regparm" << NumParamsExpr->getSourceRange();
1720 return;
1721 }
1722
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001723 if (S.Context.Target.getRegParmMax() == 0) {
1724 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001725 << NumParamsExpr->getSourceRange();
1726 return;
1727 }
1728
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001729 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001730 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1731 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001732 return;
1733 }
1734
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001735 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001736}
1737
Chris Lattner0744e5f2008-06-29 00:23:49 +00001738//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001739// Checker-specific attribute handlers.
1740//===----------------------------------------------------------------------===//
1741
1742static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1743 Sema &S) {
1744
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001745 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001746
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001747 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1748 RetTy = MD->getResultType();
1749 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1750 RetTy = FD->getResultType();
1751 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001752 SourceLocation L = Attr.getLoc();
1753 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1754 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001755 return;
1756 }
Mike Stumpbf916502009-07-24 19:02:52 +00001757
Ted Kremenek6217b802009-07-29 21:53:49 +00001758 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001759 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001760 SourceLocation L = Attr.getLoc();
1761 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1762 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001763 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001764 }
Mike Stumpbf916502009-07-24 19:02:52 +00001765
Ted Kremenekb71368d2009-05-09 02:44:38 +00001766 switch (Attr.getKind()) {
1767 default:
1768 assert(0 && "invalid ownership attribute");
1769 return;
1770 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001771 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001772 return;
1773 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001774 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001775 return;
1776 };
1777}
1778
1779//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001780// Top Level Sema Entry Points
1781//===----------------------------------------------------------------------===//
1782
Sebastian Redla89d82c2008-12-21 19:24:58 +00001783/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001784/// the attribute applies to decls. If the attribute is a type attribute, just
1785/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001786static void ProcessDeclAttribute(Scope *scope, Decl *D,
1787 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001788 if (Attr.isDeclspecAttribute())
1789 // FIXME: Try to deal with __declspec attributes!
1790 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001791 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001792 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001793 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001794 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001795 // Ignore these, these are type attributes, handled by
1796 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001797 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001798 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1799 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001800 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001801 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001802 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001803 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001804 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1805 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1806 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1807 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1808 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1809 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001810 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001811 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001812 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001813 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1814 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001815 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001816 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001817 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001818 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001819 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1820 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1821 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001822
1823 // Checker-specific.
1824 case AttributeList::AT_ns_returns_retained:
1825 case AttributeList::AT_cf_returns_retained:
1826 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1827
Nate Begeman6f3d8382009-06-26 06:32:41 +00001828 case AttributeList::AT_reqd_wg_size:
1829 HandleReqdWorkGroupSize(D, Attr, S); break;
1830
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001831 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001832 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001833 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001834 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001835 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001836 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001837 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001838 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001839 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1840 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001841 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001842 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001843 case AttributeList::AT_transparent_union:
1844 HandleTransparentUnionAttr(D, Attr, S);
1845 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001846 case AttributeList::AT_objc_exception:
1847 HandleObjCExceptionAttr(D, Attr, S);
1848 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001849 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001850 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001851 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001852 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001853 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1854 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001855 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Mike Stump1feade82009-08-26 22:31:08 +00001856 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1857 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001858 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001859 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001860 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001861 // Just ignore
1862 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001863 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001864 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001865 break;
1866 }
1867}
1868
1869/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1870/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001871void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001872 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001873 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001874 AttrList = AttrList->getNext();
1875 }
1876}
1877
Ryan Flynne25ff832009-07-30 03:15:39 +00001878/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1879/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00001880NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001881 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001882 NamedDecl *NewD = 0;
1883 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1884 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1885 FD->getLocation(), DeclarationName(II),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001886 FD->getType(), FD->getDeclaratorInfo());
Ryan Flynne25ff832009-07-30 03:15:39 +00001887 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1888 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1889 VD->getLocation(), II,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001890 VD->getType(), VD->getDeclaratorInfo(),
1891 VD->getStorageClass());
Ryan Flynne25ff832009-07-30 03:15:39 +00001892 }
1893 return NewD;
1894}
1895
1896/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1897/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001898void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00001899 if (W.getUsed()) return; // only do this once
1900 W.setUsed(true);
1901 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1902 IdentifierInfo *NDId = ND->getIdentifier();
1903 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1904 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1905 NewD->addAttr(::new (Context) WeakAttr());
1906 WeakTopLevelDecl.push_back(NewD);
1907 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1908 // to insert Decl at TU scope, sorry.
1909 DeclContext *SavedContext = CurContext;
1910 CurContext = Context.getTranslationUnitDecl();
1911 PushOnScopeChains(NewD, S);
1912 CurContext = SavedContext;
1913 } else { // just add weak to existing
1914 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00001915 }
1916}
1917
Chris Lattner0744e5f2008-06-29 00:23:49 +00001918/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1919/// it, apply them to D. This is a bit tricky because PD can have attributes
1920/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001921void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001922 // Handle #pragma weak
1923 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1924 if (ND->hasLinkage()) {
1925 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1926 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001927 // Identifier referenced by #pragma weak before it was declared
1928 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001929 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1930 }
1931 }
1932 }
1933
Chris Lattner0744e5f2008-06-29 00:23:49 +00001934 // Apply decl attributes from the DeclSpec if present.
1935 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001936 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001937
Chris Lattner0744e5f2008-06-29 00:23:49 +00001938 // Walk the declarator structure, applying decl attributes that were in a type
1939 // position to the decl itself. This handles cases like:
1940 // int *__attr__(x)** D;
1941 // when X is a decl attribute.
1942 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1943 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001944 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001945
Chris Lattner0744e5f2008-06-29 00:23:49 +00001946 // Finally, apply any attributes on the decl itself.
1947 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001948 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001949}