blob: 239f9951c5118e9c9ee8457f5f47871d48574aa0 [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"
Ted Kremenek6e1eb872008-07-22 16:56:21 +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
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000027static const FunctionType *getFunctionType(Decl *d, bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30 Ty = decl->getType();
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32 Ty = decl->getType();
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
35 else
36 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000037
Chris Lattner6b6b5372008-06-26 18:38:35 +000038 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000039 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000040 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000042
43 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000044}
45
Daniel Dunbar35682492008-09-26 04:12:28 +000046// FIXME: We should provide an abstraction around a method or function
47// to provide the following bits of information.
48
Daniel Dunbard3f2c102008-10-19 02:04:16 +000049/// isFunctionOrMethod - Return true if the given decl has function
50/// type (function or function-typed variable) or an Objective-C
51/// method.
Daniel Dunbar35682492008-09-26 04:12:28 +000052static bool isFunctionOrMethod(Decl *d) {
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000053 return getFunctionType(d, false) || isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000054}
55
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000056/// isFunctionOrMethodOrBlock - Return true if the given decl has function
57/// type (function or function-typed variable) or an Objective-C
58/// method or a block.
59static bool isFunctionOrMethodOrBlock(Decl *d) {
60 if (isFunctionOrMethod(d))
61 return true;
62 // check for block is more involved.
63 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
64 QualType Ty = V->getType();
65 return Ty->isBlockPointerType();
66 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000067 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068}
69
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070/// hasFunctionProto - Return true if the given decl has a argument
71/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000072/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Daniel Dunbard3f2c102008-10-19 02:04:16 +000073static bool hasFunctionProto(Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000074 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000075 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000077 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078 return true;
79 }
80}
81
82/// getFunctionOrMethodNumArgs - Return number of function or method
83/// arguments. It is an error to call this on a K&R function (use
84/// hasFunctionProto first).
Daniel Dunbar35682492008-09-26 04:12:28 +000085static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000086 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000087 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000088 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
89 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000090 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000091}
92
93static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +000098
Chris Lattner89951a82009-02-20 18:43:26 +000099 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000100}
101
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000102static QualType getFunctionOrMethodResultType(Decl *d) {
103 if (const FunctionType *FnTy = getFunctionType(d))
104 return cast<FunctionProtoType>(FnTy)->getResultType();
105 return cast<ObjCMethodDecl>(d)->getResultType();
106}
107
Daniel Dunbar35682492008-09-26 04:12:28 +0000108static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000109 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000110 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000111 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000112 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
113 return BD->IsVariadic();
114 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000115 return cast<ObjCMethodDecl>(d)->isVariadic();
116 }
117}
118
Chris Lattner6b6b5372008-06-26 18:38:35 +0000119static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Steve Naroff14108da2009-07-10 23:34:53 +0000120 const ObjCObjectPointerType *PT = T->getAsObjCObjectPointerType();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000121 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000122 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000123
Chris Lattnerb77792e2008-07-26 22:17:49 +0000124 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000125 if (!ClsT)
126 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000127
Chris Lattner6b6b5372008-06-26 18:38:35 +0000128 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000129
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 // FIXME: Should we walk the chain of classes?
131 return ClsName == &Ctx.Idents.get("NSString") ||
132 ClsName == &Ctx.Idents.get("NSMutableString");
133}
134
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000135static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000136 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000137 if (!PT)
138 return false;
139
Ted Kremenek6217b802009-07-29 21:53:49 +0000140 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000141 if (!RT)
142 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000143
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000144 const RecordDecl *RD = RT->getDecl();
145 if (RD->getTagKind() != TagDecl::TK_struct)
146 return false;
147
148 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
149}
150
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000151//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000152// Attribute Implementations
153//===----------------------------------------------------------------------===//
154
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000155// FIXME: All this manual attribute parsing code is gross. At the
156// least add some helper functions to check most argument patterns (#
157// and types of args).
158
Mike Stumpbf916502009-07-24 19:02:52 +0000159static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000160 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000161 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
162 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000163 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000164 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165 }
Mike Stumpbf916502009-07-24 19:02:52 +0000166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168
169 Expr *sizeExpr;
170
171 // Special case where the argument is a template id.
172 if (Attr.getParameterName()) {
173 sizeExpr = S.ActOnDeclarationNameExpr(scope, Attr.getLoc(),
174 Attr.getParameterName(),
175 false, 0, false).takeAs<Expr>();
176 } else {
177 // check the attribute arguments.
178 if (Attr.getNumArgs() != 1) {
179 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
180 return;
181 }
182 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000184
185 // Instantiate/Install the vector type, and let Sema build the type for us.
186 // This will run the reguired checks.
187 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
188 if (!T.isNull()) {
189 tDecl->setUnderlyingType(T);
Mike Stumpbf916502009-07-24 19:02:52 +0000190
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000191 // Remember this typedef decl, we will need it later for diagnostics.
192 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000194}
195
Chris Lattner065c5a82008-06-28 23:48:25 +0000196
Mike Stumpbf916502009-07-24 19:02:52 +0000197/// HandleVectorSizeAttribute - this attribute is only applicable to integral
198/// and float scalars, although arrays, pointers, and function return values are
199/// allowed in conjunction with this construct. Aggregates with this attribute
200/// are invalid, even if they are of the same size as a corresponding scalar.
201/// The raw attribute should contain precisely 1 argument, the vector size for
202/// the variable, measured in bytes. If curType and rawAttr are well formed,
203/// this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000204static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000205 QualType CurType;
206 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
207 CurType = VD->getType();
208 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
209 CurType = TD->getUnderlyingType();
210 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000211 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
212 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000213 return;
214 }
Mike Stumpbf916502009-07-24 19:02:52 +0000215
Chris Lattner065c5a82008-06-28 23:48:25 +0000216 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000217 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000218 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000219 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000220 }
Chris Lattner545dd342008-06-28 23:36:30 +0000221 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000223 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
225 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000226 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000227 }
Mike Stumpbf916502009-07-24 19:02:52 +0000228 // navigate to the base type - we need to provide for vector pointers, vector
229 // arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000230 if (CurType->isPointerType() || CurType->isArrayType() ||
231 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000232 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
233 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000234 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
235 do {
236 if (PointerType *PT = dyn_cast<PointerType>(canonType))
237 canonType = PT->getPointeeType().getTypePtr();
238 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
239 canonType = AT->getElementType().getTypePtr();
240 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
241 canonType = FT->getResultType().getTypePtr();
242 } while (canonType->isPointerType() || canonType->isArrayType() ||
243 canonType->isFunctionType());
244 */
245 }
Chris Lattner82afa2d2009-05-13 05:13:44 +0000246 // the base type must be integer or float, and can't already be a vector.
247 if (CurType->isVectorType() ||
248 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000249 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000250 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000251 }
Chris Lattner803d0802008-06-29 00:43:07 +0000252 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000253 // vecSize is specified in bytes - convert to bits.
Mike Stumpbf916502009-07-24 19:02:52 +0000254 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
255
Chris Lattner6b6b5372008-06-26 18:38:35 +0000256 // the vector size needs to be an integral multiple of the type size.
257 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000258 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
259 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000260 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000261 }
262 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000263 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
264 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000265 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000266 }
Mike Stumpbf916502009-07-24 19:02:52 +0000267
Chris Lattner065c5a82008-06-28 23:48:25 +0000268 // Success! Instantiate the vector type, the number of elements is > 0, and
269 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000270 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Mike Stumpbf916502009-07-24 19:02:52 +0000271
Chris Lattner065c5a82008-06-28 23:48:25 +0000272 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
273 VD->setType(CurType);
Mike Stumpbf916502009-07-24 19:02:52 +0000274 else
Chris Lattner065c5a82008-06-28 23:48:25 +0000275 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000276}
277
Chris Lattner803d0802008-06-29 00:43:07 +0000278static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000279 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000280 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000282 return;
283 }
Mike Stumpbf916502009-07-24 19:02:52 +0000284
Chris Lattner6b6b5372008-06-26 18:38:35 +0000285 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000286 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000287 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
288 // If the alignment is less than or equal to 8 bits, the packed attribute
289 // has no effect.
290 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000291 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000292 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000293 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000294 else
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000295 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000296 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000297 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000298}
299
Ted Kremenek96329d42008-07-15 22:26:48 +0000300static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
301 // check the attribute arguments.
302 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000304 return;
305 }
Mike Stumpbf916502009-07-24 19:02:52 +0000306
Ted Kremenek96329d42008-07-15 22:26:48 +0000307 // The IBOutlet attribute only applies to instance variables of Objective-C
308 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000309 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000310 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000311 else
Ted Kremenek32742602009-02-17 22:20:20 +0000312 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000313}
314
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000315static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000316 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
317 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000318 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000319 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000320 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000321 return;
322 }
Mike Stumpbf916502009-07-24 19:02:52 +0000323
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000324 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000325
326 // The nonnull attribute only applies to pointers.
327 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000328
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329 for (AttributeList::arg_iterator I=Attr.arg_begin(),
330 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000331
332
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000333 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000334 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000335 llvm::APSInt ArgNum(32);
336 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000339 return;
340 }
Mike Stumpbf916502009-07-24 19:02:52 +0000341
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000342 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000343
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000344 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000346 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000347 return;
348 }
Mike Stumpbf916502009-07-24 19:02:52 +0000349
Ted Kremenek465172f2008-07-21 22:09:15 +0000350 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351
352 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000353 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000354 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000355 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000356 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
357 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000358 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000359 }
Mike Stumpbf916502009-07-24 19:02:52 +0000360
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361 NonNullArgs.push_back(x);
362 }
Mike Stumpbf916502009-07-24 19:02:52 +0000363
364 // If no arguments were specified to __attribute__((nonnull)) then all pointer
365 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000366 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000367 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
368 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000369 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000370 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000371 }
Mike Stumpbf916502009-07-24 19:02:52 +0000372
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000373 if (NonNullArgs.empty()) {
374 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
375 return;
376 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000377 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000378
379 unsigned* start = &NonNullArgs[0];
380 unsigned size = NonNullArgs.size();
381 std::sort(start, start + size);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000382 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000383}
384
Chris Lattner803d0802008-06-29 00:43:07 +0000385static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000386 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000387 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000388 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000389 return;
390 }
Mike Stumpbf916502009-07-24 19:02:52 +0000391
Chris Lattner545dd342008-06-28 23:36:30 +0000392 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000393 Arg = Arg->IgnoreParenCasts();
394 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000395
Chris Lattner6b6b5372008-06-26 18:38:35 +0000396 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000397 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000398 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000399 return;
400 }
Mike Stumpbf916502009-07-24 19:02:52 +0000401
Chris Lattner6b6b5372008-06-26 18:38:35 +0000402 const char *Alias = Str->getStrData();
403 unsigned AliasLen = Str->getByteLength();
Mike Stumpbf916502009-07-24 19:02:52 +0000404
Chris Lattner6b6b5372008-06-26 18:38:35 +0000405 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000406
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000407 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000408}
409
Mike Stumpbf916502009-07-24 19:02:52 +0000410static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000411 Sema &S) {
412 // check the attribute arguments.
413 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000415 return;
416 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000417
Chris Lattnerc5197432009-04-14 17:02:11 +0000418 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000420 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000421 return;
422 }
Mike Stumpbf916502009-07-24 19:02:52 +0000423
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000424 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000425}
426
Ted Kremenekb7252322009-04-10 00:01:14 +0000427static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000428 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000429 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000430 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000432 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000433 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000434
Mike Stump19c30c02009-04-29 19:03:13 +0000435 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
436 ValueDecl *VD = dyn_cast<ValueDecl>(d);
437 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000439 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000440 return false;
441 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000442 }
Mike Stumpbf916502009-07-24 19:02:52 +0000443
Ted Kremenekb7252322009-04-10 00:01:14 +0000444 return true;
445}
446
447static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000448 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000449 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000450}
451
452static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
453 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000454 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000455 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000456}
457
Ted Kremenek73798892008-07-25 04:39:19 +0000458static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
459 // check the attribute arguments.
460 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000462 return;
463 }
Mike Stumpbf916502009-07-24 19:02:52 +0000464
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000465 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000467 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000468 return;
469 }
Mike Stumpbf916502009-07-24 19:02:52 +0000470
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000471 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000472}
473
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000474static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
475 // check the attribute arguments.
476 if (Attr.getNumArgs() != 0) {
477 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
478 return;
479 }
Mike Stumpbf916502009-07-24 19:02:52 +0000480
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000481 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000482 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000483 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
484 return;
485 }
486 } else if (!isFunctionOrMethod(d)) {
487 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000488 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000489 return;
490 }
Mike Stumpbf916502009-07-24 19:02:52 +0000491
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000492 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000493}
494
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000495static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
496 // check the attribute arguments.
497 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000498 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
499 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000500 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000501 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000502
503 int priority = 65535; // FIXME: Do not hardcode such constants.
504 if (Attr.getNumArgs() > 0) {
505 Expr *E = static_cast<Expr *>(Attr.getArg(0));
506 llvm::APSInt Idx(32);
507 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000509 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000510 return;
511 }
512 priority = Idx.getZExtValue();
513 }
Mike Stumpbf916502009-07-24 19:02:52 +0000514
Chris Lattnerc5197432009-04-14 17:02:11 +0000515 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000516 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000517 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000518 return;
519 }
520
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000521 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000522}
523
524static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
525 // check the attribute arguments.
526 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
528 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000529 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000530 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000531
532 int priority = 65535; // FIXME: Do not hardcode such constants.
533 if (Attr.getNumArgs() > 0) {
534 Expr *E = static_cast<Expr *>(Attr.getArg(0));
535 llvm::APSInt Idx(32);
536 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000537 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000538 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000539 return;
540 }
541 priority = Idx.getZExtValue();
542 }
Mike Stumpbf916502009-07-24 19:02:52 +0000543
Anders Carlsson6782fc62008-08-22 22:10:48 +0000544 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000545 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000546 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000547 return;
548 }
549
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000550 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000551}
552
Chris Lattner803d0802008-06-29 00:43:07 +0000553static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000554 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000555 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000557 return;
558 }
Mike Stumpbf916502009-07-24 19:02:52 +0000559
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000560 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000561}
562
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000563static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
564 // check the attribute arguments.
565 if (Attr.getNumArgs() != 0) {
566 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
567 return;
568 }
Mike Stumpbf916502009-07-24 19:02:52 +0000569
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000570 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000571}
572
Chris Lattner803d0802008-06-29 00:43:07 +0000573static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000574 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000575 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000576 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000577 return;
578 }
Mike Stumpbf916502009-07-24 19:02:52 +0000579
Chris Lattner545dd342008-06-28 23:36:30 +0000580 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000581 Arg = Arg->IgnoreParenCasts();
582 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000583
Chris Lattner6b6b5372008-06-26 18:38:35 +0000584 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000585 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000586 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000587 return;
588 }
Mike Stumpbf916502009-07-24 19:02:52 +0000589
Chris Lattner6b6b5372008-06-26 18:38:35 +0000590 const char *TypeStr = Str->getStrData();
591 unsigned TypeLen = Str->getByteLength();
592 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000593
Chris Lattner6b6b5372008-06-26 18:38:35 +0000594 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
595 type = VisibilityAttr::DefaultVisibility;
596 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
597 type = VisibilityAttr::HiddenVisibility;
598 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
599 type = VisibilityAttr::HiddenVisibility; // FIXME
600 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
601 type = VisibilityAttr::ProtectedVisibility;
602 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000603 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000604 return;
605 }
Mike Stumpbf916502009-07-24 19:02:52 +0000606
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000607 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000608}
609
Chris Lattner0db29ec2009-02-14 08:09:34 +0000610static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
611 Sema &S) {
612 if (Attr.getNumArgs() != 0) {
613 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
614 return;
615 }
Mike Stumpbf916502009-07-24 19:02:52 +0000616
Chris Lattner0db29ec2009-02-14 08:09:34 +0000617 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
618 if (OCI == 0) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
620 return;
621 }
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000623 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000624}
625
626static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000627 if (Attr.getNumArgs() != 0) {
628 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
629 return;
630 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000631 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000632 QualType T = TD->getUnderlyingType();
633 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000634 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000635 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
636 return;
637 }
638 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000639 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000640}
641
Mike Stumpbf916502009-07-24 19:02:52 +0000642static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000643HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
644 if (Attr.getNumArgs() != 0) {
645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
646 return;
647 }
648
649 if (!isa<FunctionDecl>(D)) {
650 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
651 return;
652 }
653
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000654 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000655}
656
Steve Naroff9eae5762008-09-18 16:44:58 +0000657static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000658 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000659 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000661 return;
662 }
Mike Stumpbf916502009-07-24 19:02:52 +0000663
Steve Naroff9eae5762008-09-18 16:44:58 +0000664 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000666 return;
667 }
Mike Stumpbf916502009-07-24 19:02:52 +0000668
Steve Naroff9eae5762008-09-18 16:44:58 +0000669 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000670 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000671 type = BlocksAttr::ByRef;
672 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000673 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000674 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000675 return;
676 }
Mike Stumpbf916502009-07-24 19:02:52 +0000677
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000678 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000679}
680
Anders Carlsson77091822008-10-05 18:05:59 +0000681static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
682 // check the attribute arguments.
683 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
685 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000686 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000687 }
688
Anders Carlsson77091822008-10-05 18:05:59 +0000689 int sentinel = 0;
690 if (Attr.getNumArgs() > 0) {
691 Expr *E = static_cast<Expr *>(Attr.getArg(0));
692 llvm::APSInt Idx(32);
693 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000694 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000695 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000696 return;
697 }
698 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000699
Anders Carlsson77091822008-10-05 18:05:59 +0000700 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000701 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
702 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000703 return;
704 }
705 }
706
707 int nullPos = 0;
708 if (Attr.getNumArgs() > 1) {
709 Expr *E = static_cast<Expr *>(Attr.getArg(1));
710 llvm::APSInt Idx(32);
711 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000712 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000713 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000714 return;
715 }
716 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000717
Anders Carlsson77091822008-10-05 18:05:59 +0000718 if (nullPos > 1 || nullPos < 0) {
719 // FIXME: This error message could be improved, it would be nice
720 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000721 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
722 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000723 return;
724 }
725 }
726
727 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000728 const FunctionType *FT = FD->getType()->getAsFunctionType();
729 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000730
Chris Lattner897cd902009-03-17 23:03:47 +0000731 if (isa<FunctionNoProtoType>(FT)) {
732 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
733 return;
734 }
Mike Stumpbf916502009-07-24 19:02:52 +0000735
Chris Lattner897cd902009-03-17 23:03:47 +0000736 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000737 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000738 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000739 }
Anders Carlsson77091822008-10-05 18:05:59 +0000740 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
741 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000742 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000743 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000744 }
745 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000746 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
747 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000748 ;
749 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000750 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000751 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000752 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Ted Kremenek6217b802009-07-29 21:53:49 +0000753 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000754 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000755 int m = Ty->isFunctionPointerType() ? 0 : 1;
756 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000757 return;
758 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000759 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000760 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000761 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000762 return;
763 }
Anders Carlsson77091822008-10-05 18:05:59 +0000764 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000765 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000766 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000767 return;
768 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000769 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000770}
771
Chris Lattner026dc962009-02-14 07:37:35 +0000772static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
773 // check the attribute arguments.
774 if (Attr.getNumArgs() != 0) {
775 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
776 return;
777 }
778
779 // TODO: could also be applied to methods?
780 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
781 if (!Fn) {
782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000783 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000784 return;
785 }
Mike Stumpbf916502009-07-24 19:02:52 +0000786
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000787 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000788}
789
790static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000791 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000792 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000793 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000794 return;
795 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000796
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000797 /* weak only applies to non-static declarations */
798 bool isStatic = false;
799 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
800 isStatic = VD->getStorageClass() == VarDecl::Static;
801 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
802 isStatic = FD->getStorageClass() == FunctionDecl::Static;
803 }
804 if (isStatic) {
805 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
806 dyn_cast<NamedDecl>(D)->getNameAsString();
807 return;
808 }
809
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000810 // TODO: could also be applied to methods?
811 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
812 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000813 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000814 return;
815 }
Mike Stumpbf916502009-07-24 19:02:52 +0000816
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000817 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000818}
819
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000820static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
821 // check the attribute arguments.
822 if (Attr.getNumArgs() != 0) {
823 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
824 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000825 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000826
827 // weak_import only applies to variable & function declarations.
828 bool isDef = false;
829 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
830 isDef = (!VD->hasExternalStorage() || VD->getInit());
831 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000832 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000833 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
834 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000835 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000836 } else {
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 }
841
842 // Merge should handle any subsequent violations.
843 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000844 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000845 diag::warn_attribute_weak_import_invalid_on_definition)
846 << "weak_import" << 2 /*variable and function*/;
847 return;
848 }
849
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000850 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000851}
852
Chris Lattner026dc962009-02-14 07:37:35 +0000853static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000854 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000855 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000856 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000857 return;
858 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000859
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000860 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000861 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000862 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000863 return;
864 }
865
Chris Lattner026dc962009-02-14 07:37:35 +0000866 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000867 if (!FD) {
868 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000869 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000870 return;
871 }
872
873 // Currently, the dllimport attribute is ignored for inlined functions.
874 // Warning is emitted.
875 if (FD->isInline()) {
876 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
877 return;
878 }
879
880 // The attribute is also overridden by a subsequent declaration as dllexport.
881 // Warning is emitted.
882 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
883 nextAttr = nextAttr->getNext()) {
884 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
886 return;
887 }
888 }
889
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000890 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000891 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
892 return;
893 }
894
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000895 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000896}
897
Chris Lattner026dc962009-02-14 07:37:35 +0000898static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000899 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000900 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000901 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000902 return;
903 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000904
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000905 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000906 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000907 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000908 return;
909 }
910
Chris Lattner026dc962009-02-14 07:37:35 +0000911 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000912 if (!FD) {
913 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000914 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000915 return;
916 }
917
Mike Stumpbf916502009-07-24 19:02:52 +0000918 // Currently, the dllexport attribute is ignored for inlined functions, unless
919 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000920 if (FD->isInline()) {
921 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
922 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
923 return;
924 }
925
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000926 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000927}
928
Nate Begeman6f3d8382009-06-26 06:32:41 +0000929static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
930 Sema &S) {
931 // Attribute has 3 arguments.
932 if (Attr.getNumArgs() != 3) {
933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
934 return;
935 }
936
937 unsigned WGSize[3];
938 for (unsigned i = 0; i < 3; ++i) {
939 Expr *E = static_cast<Expr *>(Attr.getArg(i));
940 llvm::APSInt ArgNum(32);
941 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
942 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
943 << "reqd_work_group_size" << E->getSourceRange();
944 return;
945 }
946 WGSize[i] = (unsigned) ArgNum.getZExtValue();
947 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000948 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000949 WGSize[2]));
950}
951
Chris Lattner026dc962009-02-14 07:37:35 +0000952static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000953 // Attribute has no arguments.
954 if (Attr.getNumArgs() != 1) {
955 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
956 return;
957 }
958
959 // Make sure that there is a string literal as the sections's single
960 // argument.
Mike Stumpbf916502009-07-24 19:02:52 +0000961 StringLiteral *SE =
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000962 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
963 if (!SE) {
964 // FIXME
965 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
966 return;
967 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000968 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000969 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000970}
971
Chris Lattner803d0802008-06-29 00:43:07 +0000972static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000973 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000974 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000976 return;
977 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000978
979 // Attribute can be applied only to functions.
980 if (!isa<FunctionDecl>(d)) {
981 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000982 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000983 return;
984 }
985
986 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000987 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000988 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
989 << "stdcall" << "fastcall";
990 return;
991 }
992
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000993 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000994}
995
Chris Lattner803d0802008-06-29 00:43:07 +0000996static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000997 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000998 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000999 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001000 return;
1001 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001002
1003 if (!isa<FunctionDecl>(d)) {
1004 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001005 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001006 return;
1007 }
1008
1009 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001010 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001011 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1012 << "fastcall" << "stdcall";
1013 return;
1014 }
1015
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001016 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001017}
1018
Chris Lattner803d0802008-06-29 00:43:07 +00001019static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001020 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001021 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001022 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001023 return;
1024 }
Mike Stumpbf916502009-07-24 19:02:52 +00001025
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001026 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027}
1028
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001029static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1030 // check the attribute arguments.
1031 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001032 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001033 return;
1034 }
Mike Stumpbf916502009-07-24 19:02:52 +00001035
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001036 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001037}
1038
1039static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1040 // check the attribute arguments.
1041 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001042 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001043 return;
1044 }
Mike Stumpbf916502009-07-24 19:02:52 +00001045
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001046 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001047}
1048
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001049static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001050 // Match gcc which ignores cleanup attrs when compiling C++.
1051 if (S.getLangOptions().CPlusPlus)
1052 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001053
1054 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001055 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1056 return;
1057 }
Mike Stumpbf916502009-07-24 19:02:52 +00001058
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001059 if (Attr.getNumArgs() != 0) {
1060 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1061 return;
1062 }
Mike Stumpbf916502009-07-24 19:02:52 +00001063
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001064 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001065
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001066 if (!VD || !VD->hasLocalStorage()) {
1067 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1068 return;
1069 }
Mike Stumpbf916502009-07-24 19:02:52 +00001070
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001071 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001072 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1073 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001074 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001075 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001076 Attr.getParameterName();
1077 return;
1078 }
Mike Stumpbf916502009-07-24 19:02:52 +00001079
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001080 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1081 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001082 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001083 Attr.getParameterName();
1084 return;
1085 }
1086
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001087 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001088 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001089 Attr.getParameterName();
1090 return;
1091 }
Mike Stumpbf916502009-07-24 19:02:52 +00001092
Anders Carlsson89941c12009-02-07 23:16:50 +00001093 // We're currently more strict than GCC about what function types we accept.
1094 // If this ever proves to be a problem it should be easy to fix.
1095 QualType Ty = S.Context.getPointerType(VD->getType());
1096 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001097 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001098 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001099 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1100 Attr.getParameterName() << ParamTy << Ty;
1101 return;
1102 }
Mike Stumpbf916502009-07-24 19:02:52 +00001103
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001104 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001105}
1106
Mike Stumpbf916502009-07-24 19:02:52 +00001107/// Handle __attribute__((format_arg((idx)))) attribute based on
1108/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1109static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001110 if (Attr.getNumArgs() != 1) {
1111 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1112 return;
1113 }
1114 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1115 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1116 << Attr.getName() << 0 /*function*/;
1117 return;
1118 }
Mike Stumpbf916502009-07-24 19:02:52 +00001119 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1120 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001121 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1122 unsigned FirstIdx = 1;
1123 // checks for the 2nd argument
1124 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1125 llvm::APSInt Idx(32);
1126 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1127 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1128 << "format" << 2 << IdxExpr->getSourceRange();
1129 return;
1130 }
Mike Stumpbf916502009-07-24 19:02:52 +00001131
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001132 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1133 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1134 << "format" << 2 << IdxExpr->getSourceRange();
1135 return;
1136 }
Mike Stumpbf916502009-07-24 19:02:52 +00001137
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001138 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001139
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001140 // make sure the format string is really a string
1141 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001142
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001143 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1144 if (not_nsstring_type &&
1145 !isCFStringType(Ty, S.Context) &&
1146 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001147 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001148 // FIXME: Should highlight the actual expression that has the wrong type.
1149 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001150 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001151 << IdxExpr->getSourceRange();
1152 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001153 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001154 Ty = getFunctionOrMethodResultType(d);
1155 if (!isNSStringType(Ty, S.Context) &&
1156 !isCFStringType(Ty, S.Context) &&
1157 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001158 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001159 // FIXME: Should highlight the actual expression that has the wrong type.
1160 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001161 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001162 << IdxExpr->getSourceRange();
1163 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001164 }
1165
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001166 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001167}
1168
Mike Stumpbf916502009-07-24 19:02:52 +00001169/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1170/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001171static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001172
Chris Lattner545dd342008-06-28 23:36:30 +00001173 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001174 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001175 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001176 return;
1177 }
1178
Chris Lattner545dd342008-06-28 23:36:30 +00001179 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001180 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001181 return;
1182 }
1183
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001184 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001185 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001186 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001187 return;
1188 }
1189
Mike Stumpbf916502009-07-24 19:02:52 +00001190 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1191 // needed in order to be compatible with GCC the index must start in 1 and the
1192 // limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001193 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001194 unsigned FirstIdx = 1;
1195
Chris Lattner545dd342008-06-28 23:36:30 +00001196 const char *Format = Attr.getParameterName()->getName();
1197 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001198
1199 // Normalize the argument, __foo__ becomes foo.
1200 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1201 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1202 Format += 2;
1203 FormatLen -= 4;
1204 }
1205
1206 bool Supported = false;
1207 bool is_NSString = false;
1208 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001209 bool is_CFString = false;
Mike Stumpbf916502009-07-24 19:02:52 +00001210
Chris Lattner6b6b5372008-06-26 18:38:35 +00001211 switch (FormatLen) {
1212 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001213 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1214 case 6: Supported = !memcmp(Format, "printf", 6); break;
1215 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001216 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001217 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1218 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1219 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001220 break;
1221 }
Mike Stumpbf916502009-07-24 19:02:52 +00001222
Chris Lattner6b6b5372008-06-26 18:38:35 +00001223 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001224 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1225 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001226 return;
1227 }
1228
1229 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001230 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001231 llvm::APSInt Idx(32);
1232 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001234 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001235 return;
1236 }
1237
1238 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001240 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 return;
1242 }
1243
1244 // FIXME: Do we need to bounds check?
1245 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001246
Chris Lattner6b6b5372008-06-26 18:38:35 +00001247 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001248 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001249
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001250 if (is_CFString) {
1251 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001252 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1253 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001254 return;
1255 }
1256 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001257 // FIXME: do we need to check if the type is NSString*? What are the
1258 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001259 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001260 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001261 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1262 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001264 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001265 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001266 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001267 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001268 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1269 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001270 return;
1271 }
1272
1273 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001274 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001275 llvm::APSInt FirstArg(32);
1276 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001278 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001279 return;
1280 }
1281
1282 // check if the function is variadic if the 3rd argument non-zero
1283 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001284 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001285 ++NumArgs; // +1 for ...
1286 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001287 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001288 return;
1289 }
1290 }
1291
Chris Lattner3c73c412008-11-19 08:23:25 +00001292 // strftime requires FirstArg to be 0 because it doesn't read from any
1293 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001294 if (is_strftime) {
1295 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001296 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1297 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001298 return;
1299 }
1300 // if 0 it disables parameter checking (to use with e.g. va_list)
1301 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001302 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001303 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001304 return;
1305 }
1306
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001307 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001308 Idx.getZExtValue(), FirstArg.getZExtValue()));
1309}
1310
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001311static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1312 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001314 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001316 return;
1317 }
1318
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001319 // Try to find the underlying union declaration.
1320 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001321 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001322 if (TD && TD->getUnderlyingType()->isUnionType())
1323 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1324 else
1325 RD = dyn_cast<RecordDecl>(d);
1326
1327 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001328 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001329 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001330 return;
1331 }
1332
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001333 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001334 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001335 diag::warn_transparent_union_attribute_not_definition);
1336 return;
1337 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001338
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001339 RecordDecl::field_iterator Field = RD->field_begin(),
1340 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001341 if (Field == FieldEnd) {
1342 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1343 return;
1344 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001345
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001346 FieldDecl *FirstField = *Field;
1347 QualType FirstType = FirstField->getType();
1348 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001349 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001350 diag::warn_transparent_union_attribute_floating);
1351 return;
1352 }
1353
1354 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1355 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1356 for (; Field != FieldEnd; ++Field) {
1357 QualType FieldType = Field->getType();
1358 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1359 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1360 // Warn if we drop the attribute.
1361 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001362 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001363 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001364 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001365 diag::warn_transparent_union_attribute_field_size_align)
1366 << isSize << Field->getDeclName() << FieldBits;
1367 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001368 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001369 diag::note_transparent_union_first_field_size_align)
1370 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001371 return;
1372 }
1373 }
1374
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001375 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001376}
1377
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001378static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001379 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001380 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001381 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001382 return;
1383 }
Chris Lattner545dd342008-06-28 23:36:30 +00001384 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001385 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001386
Chris Lattner6b6b5372008-06-26 18:38:35 +00001387 // Make sure that there is a string literal as the annotation's single
1388 // argument.
1389 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001390 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001391 return;
1392 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001393 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001394 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001395}
1396
Chris Lattner803d0802008-06-29 00:43:07 +00001397static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001398 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001399 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001401 return;
1402 }
1403
1404 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001405 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001406 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001407 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001408 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001409 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001410 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001411 }
Mike Stumpbf916502009-07-24 19:02:52 +00001412
Chris Lattner49e2d342008-06-28 23:50:44 +00001413 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1414 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001415 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001416 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1417 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001418 return;
1419 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001420 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001421 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001422 << alignmentExpr->getSourceRange();
1423 return;
1424 }
1425
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001426 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001427}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001428
Mike Stumpbf916502009-07-24 19:02:52 +00001429/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1430/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001431///
Mike Stumpbf916502009-07-24 19:02:52 +00001432/// Despite what would be logical, the mode attribute is a decl attribute, not a
1433/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1434/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001435static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001436 // This attribute isn't documented, but glibc uses it. It changes
1437 // the width of an int or unsigned int to the specified size.
1438
1439 // Check that there aren't any arguments
1440 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001442 return;
1443 }
1444
1445 IdentifierInfo *Name = Attr.getParameterName();
1446 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001447 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001448 return;
1449 }
1450 const char *Str = Name->getName();
1451 unsigned Len = Name->getLength();
1452
1453 // Normalize the attribute name, __foo__ becomes foo.
1454 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1455 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1456 Str += 2;
1457 Len -= 4;
1458 }
1459
1460 unsigned DestWidth = 0;
1461 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001462 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001463 switch (Len) {
1464 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001465 switch (Str[0]) {
1466 case 'Q': DestWidth = 8; break;
1467 case 'H': DestWidth = 16; break;
1468 case 'S': DestWidth = 32; break;
1469 case 'D': DestWidth = 64; break;
1470 case 'X': DestWidth = 96; break;
1471 case 'T': DestWidth = 128; break;
1472 }
1473 if (Str[1] == 'F') {
1474 IntegerMode = false;
1475 } else if (Str[1] == 'C') {
1476 IntegerMode = false;
1477 ComplexMode = true;
1478 } else if (Str[1] != 'I') {
1479 DestWidth = 0;
1480 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001481 break;
1482 case 4:
1483 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1484 // pointer on PIC16 and other embedded platforms.
1485 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001486 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001487 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001488 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001489 break;
1490 case 7:
1491 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001492 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001493 break;
1494 }
1495
1496 QualType OldTy;
1497 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1498 OldTy = TD->getUnderlyingType();
1499 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1500 OldTy = VD->getType();
1501 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001502 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1503 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001504 return;
1505 }
Eli Friedman73397492009-03-03 06:41:03 +00001506
1507 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1508 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1509 else if (IntegerMode) {
1510 if (!OldTy->isIntegralType())
1511 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1512 } else if (ComplexMode) {
1513 if (!OldTy->isComplexType())
1514 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1515 } else {
1516 if (!OldTy->isFloatingType())
1517 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1518 }
1519
Mike Stump390b4cc2009-05-16 07:39:55 +00001520 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1521 // and friends, at least with glibc.
1522 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1523 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001524 // FIXME: Make sure floating-point mappings are accurate
1525 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001526 QualType NewTy;
1527 switch (DestWidth) {
1528 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001529 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001530 return;
1531 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001532 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001533 return;
1534 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001535 if (!IntegerMode) {
1536 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1537 return;
1538 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001539 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001540 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001541 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001542 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001543 break;
1544 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001545 if (!IntegerMode) {
1546 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1547 return;
1548 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001549 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001550 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001551 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001552 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001553 break;
1554 case 32:
1555 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001556 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001557 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001558 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001559 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001560 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001561 break;
1562 case 64:
1563 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001564 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001565 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001566 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001567 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001568 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001569 break;
Eli Friedman73397492009-03-03 06:41:03 +00001570 case 96:
1571 NewTy = S.Context.LongDoubleTy;
1572 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001573 case 128:
1574 if (!IntegerMode) {
1575 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1576 return;
1577 }
1578 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001579 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001580 }
1581
Eli Friedman73397492009-03-03 06:41:03 +00001582 if (ComplexMode) {
1583 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001584 }
1585
1586 // Install the new type.
1587 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1588 TD->setUnderlyingType(NewTy);
1589 else
1590 cast<ValueDecl>(D)->setType(NewTy);
1591}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001592
Anders Carlssond87df372009-02-13 06:46:13 +00001593static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1594 // check the attribute arguments.
1595 if (Attr.getNumArgs() > 0) {
1596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1597 return;
1598 }
Anders Carlssone896d982009-02-13 08:11:52 +00001599
Anders Carlsson5bab7882009-02-19 19:16:48 +00001600 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001601 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001602 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001603 return;
1604 }
Mike Stumpbf916502009-07-24 19:02:52 +00001605
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001606 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001607}
1608
Anders Carlsson5bab7882009-02-19 19:16:48 +00001609static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1610 // check the attribute arguments.
1611 if (Attr.getNumArgs() != 0) {
1612 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1613 return;
1614 }
Mike Stumpbf916502009-07-24 19:02:52 +00001615
Chris Lattnerc5197432009-04-14 17:02:11 +00001616 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001617 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001618 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001619 return;
1620 }
Mike Stumpbf916502009-07-24 19:02:52 +00001621
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001622 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001623}
1624
Chris Lattnercf2a7212009-04-20 19:12:28 +00001625static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001626 // check the attribute arguments.
1627 if (Attr.getNumArgs() != 0) {
1628 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1629 return;
1630 }
Mike Stumpbf916502009-07-24 19:02:52 +00001631
Chris Lattnerc5197432009-04-14 17:02:11 +00001632 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1633 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001634 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001635 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001636 return;
1637 }
Mike Stumpbf916502009-07-24 19:02:52 +00001638
Chris Lattnerc5197432009-04-14 17:02:11 +00001639 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001640 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001641 return;
1642 }
Mike Stumpbf916502009-07-24 19:02:52 +00001643
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001644 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001645}
1646
Fariborz Jahanianee760332009-03-27 18:38:55 +00001647static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1648 // check the attribute arguments.
1649 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001651 return;
1652 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001653
Fariborz Jahanianee760332009-03-27 18:38:55 +00001654 if (!isFunctionOrMethod(d)) {
1655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001656 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001657 return;
1658 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001659
1660 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1661 llvm::APSInt NumParams(32);
1662 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1663 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1664 << "regparm" << NumParamsExpr->getSourceRange();
1665 return;
1666 }
1667
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001668 if (S.Context.Target.getRegParmMax() == 0) {
1669 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001670 << NumParamsExpr->getSourceRange();
1671 return;
1672 }
1673
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001674 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001675 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1676 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001677 return;
1678 }
1679
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001680 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001681}
1682
Chris Lattner0744e5f2008-06-29 00:23:49 +00001683//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001684// Checker-specific attribute handlers.
1685//===----------------------------------------------------------------------===//
1686
1687static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1688 Sema &S) {
1689
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001690 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001691
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001692 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1693 RetTy = MD->getResultType();
1694 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1695 RetTy = FD->getResultType();
1696 else {
1697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1698 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001699 return;
1700 }
Mike Stumpbf916502009-07-24 19:02:52 +00001701
Ted Kremenek6217b802009-07-29 21:53:49 +00001702 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
Steve Naroff14108da2009-07-10 23:34:53 +00001703 || RetTy->getAsObjCObjectPointerType())) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001704 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1705 << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001706 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001707 }
Mike Stumpbf916502009-07-24 19:02:52 +00001708
Ted Kremenekb71368d2009-05-09 02:44:38 +00001709 switch (Attr.getKind()) {
1710 default:
1711 assert(0 && "invalid ownership attribute");
1712 return;
1713 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001714 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001715 return;
1716 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001717 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001718 return;
1719 };
1720}
1721
1722//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001723// Top Level Sema Entry Points
1724//===----------------------------------------------------------------------===//
1725
Sebastian Redla89d82c2008-12-21 19:24:58 +00001726/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001727/// the attribute applies to decls. If the attribute is a type attribute, just
1728/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001729static void ProcessDeclAttribute(Scope *scope, Decl *D,
1730 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001731 if (Attr.isDeclspecAttribute())
1732 // FIXME: Try to deal with __declspec attributes!
1733 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001734 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001735 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001736 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001737 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001738 // Ignore these, these are type attributes, handled by
1739 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001740 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001741 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1742 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001743 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001744 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001745 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001746 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001747 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1748 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1749 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1750 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1751 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1752 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001753 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001754 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001755 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001756 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1757 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001758 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001759 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001760 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001761 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1762 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1763 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001764
1765 // Checker-specific.
1766 case AttributeList::AT_ns_returns_retained:
1767 case AttributeList::AT_cf_returns_retained:
1768 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1769
Nate Begeman6f3d8382009-06-26 06:32:41 +00001770 case AttributeList::AT_reqd_wg_size:
1771 HandleReqdWorkGroupSize(D, Attr, S); break;
1772
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001773 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001774 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001775 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001776 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001777 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001778 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001779 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001780 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001781 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1782 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001783 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001784 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001785 case AttributeList::AT_transparent_union:
1786 HandleTransparentUnionAttr(D, Attr, S);
1787 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001788 case AttributeList::AT_objc_exception:
1789 HandleObjCExceptionAttr(D, Attr, S);
1790 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001791 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001792 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001793 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001794 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001795 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1796 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001797 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001798 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001799 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001800 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001801 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001802 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001803 // Just ignore
1804 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001805 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001806 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001807 break;
1808 }
1809}
1810
1811/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1812/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001813void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001814 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001815 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001816 AttrList = AttrList->getNext();
1817 }
1818}
1819
Ryan Flynne25ff832009-07-30 03:15:39 +00001820/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1821/// #pragma weak needs a non-definition decl and source may not have one
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001822NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II)
Ryan Flynne25ff832009-07-30 03:15:39 +00001823{
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001824 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001825 NamedDecl *NewD = 0;
1826 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1827 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1828 FD->getLocation(), DeclarationName(II),
1829 FD->getType());
1830 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1831 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1832 VD->getLocation(), II,
1833 VD->getType(), VD->getStorageClass());
1834 }
1835 return NewD;
1836}
1837
1838/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1839/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001840void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001841 if (!W.getUsed()) { // only do this once
1842 W.setUsed(true);
1843 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1844 IdentifierInfo *NDId = ND->getIdentifier();
1845 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1846 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1847 NewD->addAttr(::new (Context) WeakAttr());
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001848 WeakTopLevelDecl.push_back(NewD);
1849 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1850 // to insert Decl at TU scope, sorry.
1851 DeclContext *SavedContext = CurContext;
1852 CurContext = Context.getTranslationUnitDecl();
1853 PushOnScopeChains(NewD, S);
1854 CurContext = SavedContext;
Ryan Flynne25ff832009-07-30 03:15:39 +00001855 } else { // just add weak to existing
1856 ND->addAttr(::new (Context) WeakAttr());
1857 }
1858 }
1859}
1860
Chris Lattner0744e5f2008-06-29 00:23:49 +00001861/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1862/// it, apply them to D. This is a bit tricky because PD can have attributes
1863/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001864void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001865 // Handle #pragma weak
1866 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1867 if (ND->hasLinkage()) {
1868 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1869 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001870 // Identifier referenced by #pragma weak before it was declared
1871 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001872 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1873 }
1874 }
1875 }
1876
Chris Lattner0744e5f2008-06-29 00:23:49 +00001877 // Apply decl attributes from the DeclSpec if present.
1878 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001879 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001880
Chris Lattner0744e5f2008-06-29 00:23:49 +00001881 // Walk the declarator structure, applying decl attributes that were in a type
1882 // position to the decl itself. This handles cases like:
1883 // int *__attr__(x)** D;
1884 // when X is a decl attribute.
1885 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1886 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001887 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001888
Chris Lattner0744e5f2008-06-29 00:23:49 +00001889 // Finally, apply any attributes on the decl itself.
1890 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001891 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001892}