blob: d5d3840569bf62c8737cb103152eb287f370bead [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
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))
Anders Carlssona860e752009-08-08 18:23:56 +0000286 TD->addAttr(::new (S.Context) PackedAttr);
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
Anders Carlssona860e752009-08-08 18:23:56 +0000295 FD->addAttr(::new (S.Context) PackedAttr);
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
Ryan Flynn76168e22009-08-09 20:07:29 +0000427static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
428 // check the attribute arguments.
429 if (Attr.getNumArgs() != 0) {
430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
431 return;
432 }
433
434 if (!isFunctionOrMethod(d)) {
435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
436 << Attr.getName() << 0 /*function*/;
437 return;
438 }
439
Ryan Flynnfd6ad3c2009-08-09 22:36:29 +0000440 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
441 if (!FD->getResultType()->isPointerType()) {
442 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
443 return;
444 }
445 }
446
Ryan Flynn76168e22009-08-09 20:07:29 +0000447 d->addAttr(::new (S.Context) MallocAttr());
448}
449
Ted Kremenekb7252322009-04-10 00:01:14 +0000450static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000451 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000452 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000453 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000454 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000455 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000456 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000457
Mike Stump19c30c02009-04-29 19:03:13 +0000458 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
459 ValueDecl *VD = dyn_cast<ValueDecl>(d);
460 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
461 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000462 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000463 return false;
464 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000465 }
Mike Stumpbf916502009-07-24 19:02:52 +0000466
Ted Kremenekb7252322009-04-10 00:01:14 +0000467 return true;
468}
469
470static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000471 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000472 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000473}
474
475static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
476 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000477 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000478 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000479}
480
Ted Kremenek73798892008-07-25 04:39:19 +0000481static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
482 // check the attribute arguments.
483 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000484 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000485 return;
486 }
Mike Stumpbf916502009-07-24 19:02:52 +0000487
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000488 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000489 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000490 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000491 return;
492 }
Mike Stumpbf916502009-07-24 19:02:52 +0000493
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000494 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000495}
496
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000497static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
498 // check the attribute arguments.
499 if (Attr.getNumArgs() != 0) {
500 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
501 return;
502 }
Mike Stumpbf916502009-07-24 19:02:52 +0000503
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000504 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000505 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000506 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
507 return;
508 }
509 } else if (!isFunctionOrMethod(d)) {
510 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000511 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000512 return;
513 }
Mike Stumpbf916502009-07-24 19:02:52 +0000514
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000515 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000516}
517
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000518static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
519 // check the attribute arguments.
520 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000521 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
522 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000523 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000524 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000525
526 int priority = 65535; // FIXME: Do not hardcode such constants.
527 if (Attr.getNumArgs() > 0) {
528 Expr *E = static_cast<Expr *>(Attr.getArg(0));
529 llvm::APSInt Idx(32);
530 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000531 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000532 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000533 return;
534 }
535 priority = Idx.getZExtValue();
536 }
Mike Stumpbf916502009-07-24 19:02:52 +0000537
Chris Lattnerc5197432009-04-14 17:02:11 +0000538 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000539 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000540 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000541 return;
542 }
543
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000544 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000545}
546
547static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
548 // check the attribute arguments.
549 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000550 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
551 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000552 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000553 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000554
555 int priority = 65535; // FIXME: Do not hardcode such constants.
556 if (Attr.getNumArgs() > 0) {
557 Expr *E = static_cast<Expr *>(Attr.getArg(0));
558 llvm::APSInt Idx(32);
559 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000560 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000561 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000562 return;
563 }
564 priority = Idx.getZExtValue();
565 }
Mike Stumpbf916502009-07-24 19:02:52 +0000566
Anders Carlsson6782fc62008-08-22 22:10:48 +0000567 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000568 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000569 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000570 return;
571 }
572
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000573 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000574}
575
Chris Lattner803d0802008-06-29 00:43:07 +0000576static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000577 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000578 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000579 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000580 return;
581 }
Mike Stumpbf916502009-07-24 19:02:52 +0000582
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000583 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000584}
585
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000586static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
587 // check the attribute arguments.
588 if (Attr.getNumArgs() != 0) {
589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
590 return;
591 }
Mike Stumpbf916502009-07-24 19:02:52 +0000592
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000593 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000594}
595
Chris Lattner803d0802008-06-29 00:43:07 +0000596static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000597 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000598 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000600 return;
601 }
Mike Stumpbf916502009-07-24 19:02:52 +0000602
Chris Lattner545dd342008-06-28 23:36:30 +0000603 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000604 Arg = Arg->IgnoreParenCasts();
605 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000606
Chris Lattner6b6b5372008-06-26 18:38:35 +0000607 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000608 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000609 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000610 return;
611 }
Mike Stumpbf916502009-07-24 19:02:52 +0000612
Chris Lattner6b6b5372008-06-26 18:38:35 +0000613 const char *TypeStr = Str->getStrData();
614 unsigned TypeLen = Str->getByteLength();
615 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000616
Chris Lattner6b6b5372008-06-26 18:38:35 +0000617 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
618 type = VisibilityAttr::DefaultVisibility;
619 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
620 type = VisibilityAttr::HiddenVisibility;
621 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
622 type = VisibilityAttr::HiddenVisibility; // FIXME
623 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
624 type = VisibilityAttr::ProtectedVisibility;
625 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000626 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000627 return;
628 }
Mike Stumpbf916502009-07-24 19:02:52 +0000629
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000630 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000631}
632
Chris Lattner0db29ec2009-02-14 08:09:34 +0000633static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
634 Sema &S) {
635 if (Attr.getNumArgs() != 0) {
636 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
637 return;
638 }
Mike Stumpbf916502009-07-24 19:02:52 +0000639
Chris Lattner0db29ec2009-02-14 08:09:34 +0000640 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
641 if (OCI == 0) {
642 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
643 return;
644 }
Mike Stumpbf916502009-07-24 19:02:52 +0000645
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000646 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000647}
648
649static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000650 if (Attr.getNumArgs() != 0) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
652 return;
653 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000654 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000655 QualType T = TD->getUnderlyingType();
656 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000657 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000658 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
659 return;
660 }
661 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000662 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000663}
664
Mike Stumpbf916502009-07-24 19:02:52 +0000665static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000666HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
667 if (Attr.getNumArgs() != 0) {
668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
669 return;
670 }
671
672 if (!isa<FunctionDecl>(D)) {
673 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
674 return;
675 }
676
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000677 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000678}
679
Steve Naroff9eae5762008-09-18 16:44:58 +0000680static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000681 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000682 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000683 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000684 return;
685 }
Mike Stumpbf916502009-07-24 19:02:52 +0000686
Steve Naroff9eae5762008-09-18 16:44:58 +0000687 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000689 return;
690 }
Mike Stumpbf916502009-07-24 19:02:52 +0000691
Steve Naroff9eae5762008-09-18 16:44:58 +0000692 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000693 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000694 type = BlocksAttr::ByRef;
695 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000696 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000697 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000698 return;
699 }
Mike Stumpbf916502009-07-24 19:02:52 +0000700
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000701 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000702}
703
Anders Carlsson77091822008-10-05 18:05:59 +0000704static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
705 // check the attribute arguments.
706 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
708 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000709 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000710 }
711
Anders Carlsson77091822008-10-05 18:05:59 +0000712 int sentinel = 0;
713 if (Attr.getNumArgs() > 0) {
714 Expr *E = static_cast<Expr *>(Attr.getArg(0));
715 llvm::APSInt Idx(32);
716 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000717 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000718 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000719 return;
720 }
721 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000722
Anders Carlsson77091822008-10-05 18:05:59 +0000723 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000724 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
725 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000726 return;
727 }
728 }
729
730 int nullPos = 0;
731 if (Attr.getNumArgs() > 1) {
732 Expr *E = static_cast<Expr *>(Attr.getArg(1));
733 llvm::APSInt Idx(32);
734 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000736 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000737 return;
738 }
739 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000740
Anders Carlsson77091822008-10-05 18:05:59 +0000741 if (nullPos > 1 || nullPos < 0) {
742 // FIXME: This error message could be improved, it would be nice
743 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000744 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
745 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000746 return;
747 }
748 }
749
750 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000751 const FunctionType *FT = FD->getType()->getAsFunctionType();
752 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000753
Chris Lattner897cd902009-03-17 23:03:47 +0000754 if (isa<FunctionNoProtoType>(FT)) {
755 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
756 return;
757 }
Mike Stumpbf916502009-07-24 19:02:52 +0000758
Chris Lattner897cd902009-03-17 23:03:47 +0000759 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000760 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000761 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000762 }
Anders Carlsson77091822008-10-05 18:05:59 +0000763 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
764 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000765 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000766 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000767 }
768 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000769 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
770 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000771 ;
772 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000773 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000774 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000775 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Ted Kremenek6217b802009-07-29 21:53:49 +0000776 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000777 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000778 int m = Ty->isFunctionPointerType() ? 0 : 1;
779 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000780 return;
781 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000782 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000783 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000784 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000785 return;
786 }
Anders Carlsson77091822008-10-05 18:05:59 +0000787 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000788 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000789 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000790 return;
791 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000792 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000793}
794
Chris Lattner026dc962009-02-14 07:37:35 +0000795static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
796 // check the attribute arguments.
797 if (Attr.getNumArgs() != 0) {
798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
799 return;
800 }
801
802 // TODO: could also be applied to methods?
803 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
804 if (!Fn) {
805 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000806 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000807 return;
808 }
Mike Stumpbf916502009-07-24 19:02:52 +0000809
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000810 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000811}
812
813static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000814 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000815 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000816 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000817 return;
818 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000819
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000820 /* weak only applies to non-static declarations */
821 bool isStatic = false;
822 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
823 isStatic = VD->getStorageClass() == VarDecl::Static;
824 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
825 isStatic = FD->getStorageClass() == FunctionDecl::Static;
826 }
827 if (isStatic) {
828 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
829 dyn_cast<NamedDecl>(D)->getNameAsString();
830 return;
831 }
832
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000833 // TODO: could also be applied to methods?
834 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000836 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000837 return;
838 }
Mike Stumpbf916502009-07-24 19:02:52 +0000839
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000840 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000841}
842
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000843static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
844 // check the attribute arguments.
845 if (Attr.getNumArgs() != 0) {
846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
847 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000848 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000849
850 // weak_import only applies to variable & function declarations.
851 bool isDef = false;
852 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
853 isDef = (!VD->hasExternalStorage() || VD->getInit());
854 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000855 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000856 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
857 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000858 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000859 } else {
860 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000861 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000862 return;
863 }
864
865 // Merge should handle any subsequent violations.
866 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000867 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000868 diag::warn_attribute_weak_import_invalid_on_definition)
869 << "weak_import" << 2 /*variable and function*/;
870 return;
871 }
872
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000873 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000874}
875
Chris Lattner026dc962009-02-14 07:37:35 +0000876static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000877 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000878 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000879 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000880 return;
881 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000882
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000883 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000884 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000885 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000886 return;
887 }
888
Chris Lattner026dc962009-02-14 07:37:35 +0000889 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000890 if (!FD) {
891 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000892 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000893 return;
894 }
895
896 // Currently, the dllimport attribute is ignored for inlined functions.
897 // Warning is emitted.
898 if (FD->isInline()) {
899 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
900 return;
901 }
902
903 // The attribute is also overridden by a subsequent declaration as dllexport.
904 // Warning is emitted.
905 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
906 nextAttr = nextAttr->getNext()) {
907 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
908 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
909 return;
910 }
911 }
912
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000913 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000914 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
915 return;
916 }
917
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000918 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000919}
920
Chris Lattner026dc962009-02-14 07:37:35 +0000921static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000922 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000923 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000924 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000925 return;
926 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000927
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000928 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000929 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000930 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000931 return;
932 }
933
Chris Lattner026dc962009-02-14 07:37:35 +0000934 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000935 if (!FD) {
936 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000937 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000938 return;
939 }
940
Mike Stumpbf916502009-07-24 19:02:52 +0000941 // Currently, the dllexport attribute is ignored for inlined functions, unless
942 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000943 if (FD->isInline()) {
944 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
945 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
946 return;
947 }
948
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000949 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000950}
951
Nate Begeman6f3d8382009-06-26 06:32:41 +0000952static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
953 Sema &S) {
954 // Attribute has 3 arguments.
955 if (Attr.getNumArgs() != 3) {
956 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
957 return;
958 }
959
960 unsigned WGSize[3];
961 for (unsigned i = 0; i < 3; ++i) {
962 Expr *E = static_cast<Expr *>(Attr.getArg(i));
963 llvm::APSInt ArgNum(32);
964 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
965 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
966 << "reqd_work_group_size" << E->getSourceRange();
967 return;
968 }
969 WGSize[i] = (unsigned) ArgNum.getZExtValue();
970 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000971 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000972 WGSize[2]));
973}
974
Chris Lattner026dc962009-02-14 07:37:35 +0000975static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000976 // Attribute has no arguments.
977 if (Attr.getNumArgs() != 1) {
978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
979 return;
980 }
981
982 // Make sure that there is a string literal as the sections's single
983 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000984 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
985 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000986 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000987 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000988 return;
989 }
Chris Lattner797c3c42009-08-10 19:03:04 +0000990
991 std::string SectionStr(SE->getStrData(), SE->getByteLength());
992
993 // If the target wants to validate the section specifier, make it happen.
994 std::string Error = S.Context.Target.isValidSectionSpecifier(SectionStr);
995 if (Error.empty()) {
996 D->addAttr(::new (S.Context) SectionAttr(SectionStr));
997 return;
998 }
999
1000 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1001 << Error;
1002
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001003}
1004
Chris Lattner803d0802008-06-29 00:43:07 +00001005static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001006 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001007 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001008 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001009 return;
1010 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001011
1012 // Attribute can be applied only to functions.
1013 if (!isa<FunctionDecl>(d)) {
1014 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001015 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001016 return;
1017 }
1018
1019 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001020 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001021 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1022 << "stdcall" << "fastcall";
1023 return;
1024 }
1025
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001026 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027}
1028
Chris Lattner803d0802008-06-29 00:43:07 +00001029static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001030 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001031 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001032 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001033 return;
1034 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001035
1036 if (!isa<FunctionDecl>(d)) {
1037 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001038 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001039 return;
1040 }
1041
1042 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001043 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001044 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1045 << "fastcall" << "stdcall";
1046 return;
1047 }
1048
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001049 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001050}
1051
Chris Lattner803d0802008-06-29 00:43:07 +00001052static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001053 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001054 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001055 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001056 return;
1057 }
Mike Stumpbf916502009-07-24 19:02:52 +00001058
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001059 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001060}
1061
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001062static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1063 // check the attribute arguments.
1064 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001065 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001066 return;
1067 }
Mike Stumpbf916502009-07-24 19:02:52 +00001068
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001069 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001070}
1071
1072static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1073 // check the attribute arguments.
1074 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001075 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001076 return;
1077 }
Mike Stumpbf916502009-07-24 19:02:52 +00001078
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001079 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001080}
1081
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001082static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001083 // Match gcc which ignores cleanup attrs when compiling C++.
1084 if (S.getLangOptions().CPlusPlus)
1085 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001086
1087 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001088 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1089 return;
1090 }
Mike Stumpbf916502009-07-24 19:02:52 +00001091
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001092 if (Attr.getNumArgs() != 0) {
1093 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1094 return;
1095 }
Mike Stumpbf916502009-07-24 19:02:52 +00001096
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001097 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001098
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001099 if (!VD || !VD->hasLocalStorage()) {
1100 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1101 return;
1102 }
Mike Stumpbf916502009-07-24 19:02:52 +00001103
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001104 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001105 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1106 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001107 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001108 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001109 Attr.getParameterName();
1110 return;
1111 }
Mike Stumpbf916502009-07-24 19:02:52 +00001112
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001113 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1114 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001115 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001116 Attr.getParameterName();
1117 return;
1118 }
1119
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001120 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001121 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001122 Attr.getParameterName();
1123 return;
1124 }
Mike Stumpbf916502009-07-24 19:02:52 +00001125
Anders Carlsson89941c12009-02-07 23:16:50 +00001126 // We're currently more strict than GCC about what function types we accept.
1127 // If this ever proves to be a problem it should be easy to fix.
1128 QualType Ty = S.Context.getPointerType(VD->getType());
1129 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001130 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001131 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001132 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1133 Attr.getParameterName() << ParamTy << Ty;
1134 return;
1135 }
Mike Stumpbf916502009-07-24 19:02:52 +00001136
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001137 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001138}
1139
Mike Stumpbf916502009-07-24 19:02:52 +00001140/// Handle __attribute__((format_arg((idx)))) attribute based on
1141/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1142static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001143 if (Attr.getNumArgs() != 1) {
1144 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1145 return;
1146 }
1147 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1148 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1149 << Attr.getName() << 0 /*function*/;
1150 return;
1151 }
Mike Stumpbf916502009-07-24 19:02:52 +00001152 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1153 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001154 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1155 unsigned FirstIdx = 1;
1156 // checks for the 2nd argument
1157 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1158 llvm::APSInt Idx(32);
1159 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1161 << "format" << 2 << IdxExpr->getSourceRange();
1162 return;
1163 }
Mike Stumpbf916502009-07-24 19:02:52 +00001164
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001165 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1166 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1167 << "format" << 2 << IdxExpr->getSourceRange();
1168 return;
1169 }
Mike Stumpbf916502009-07-24 19:02:52 +00001170
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001171 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001172
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001173 // make sure the format string is really a string
1174 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001175
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001176 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1177 if (not_nsstring_type &&
1178 !isCFStringType(Ty, S.Context) &&
1179 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001180 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001181 // FIXME: Should highlight the actual expression that has the wrong type.
1182 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001183 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001184 << IdxExpr->getSourceRange();
1185 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001186 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001187 Ty = getFunctionOrMethodResultType(d);
1188 if (!isNSStringType(Ty, S.Context) &&
1189 !isCFStringType(Ty, S.Context) &&
1190 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001191 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001192 // FIXME: Should highlight the actual expression that has the wrong type.
1193 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001194 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001195 << IdxExpr->getSourceRange();
1196 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001197 }
1198
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001199 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001200}
1201
Mike Stumpbf916502009-07-24 19:02:52 +00001202/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1203/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001204static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001205
Chris Lattner545dd342008-06-28 23:36:30 +00001206 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001208 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001209 return;
1210 }
1211
Chris Lattner545dd342008-06-28 23:36:30 +00001212 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001214 return;
1215 }
1216
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001217 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001218 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001219 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001220 return;
1221 }
1222
Mike Stumpbf916502009-07-24 19:02:52 +00001223 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1224 // needed in order to be compatible with GCC the index must start in 1 and the
1225 // limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001226 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001227 unsigned FirstIdx = 1;
1228
Chris Lattner545dd342008-06-28 23:36:30 +00001229 const char *Format = Attr.getParameterName()->getName();
1230 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001231
1232 // Normalize the argument, __foo__ becomes foo.
1233 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1234 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1235 Format += 2;
1236 FormatLen -= 4;
1237 }
1238
1239 bool Supported = false;
1240 bool is_NSString = false;
1241 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001242 bool is_CFString = false;
Mike Stumpbf916502009-07-24 19:02:52 +00001243
Chris Lattner6b6b5372008-06-26 18:38:35 +00001244 switch (FormatLen) {
1245 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001246 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1247 case 6: Supported = !memcmp(Format, "printf", 6); break;
Ryan Flynn4403a5e2009-08-06 03:00:50 +00001248 case 7: Supported = !memcmp(Format, "printf0", 7) ||
1249 !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001250 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001251 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1252 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1253 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001254 break;
1255 }
Mike Stumpbf916502009-07-24 19:02:52 +00001256
Chris Lattner6b6b5372008-06-26 18:38:35 +00001257 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001258 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1259 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260 return;
1261 }
1262
1263 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001264 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001265 llvm::APSInt Idx(32);
1266 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001267 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001268 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001269 return;
1270 }
1271
1272 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001274 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001275 return;
1276 }
1277
1278 // FIXME: Do we need to bounds check?
1279 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001280
Chris Lattner6b6b5372008-06-26 18:38:35 +00001281 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001282 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001284 if (is_CFString) {
1285 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001286 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1287 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001288 return;
1289 }
1290 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001291 // FIXME: do we need to check if the type is NSString*? What are the
1292 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001293 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001294 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001295 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1296 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001297 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001298 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001299 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001300 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001301 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001302 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1303 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001304 return;
1305 }
1306
1307 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001308 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001309 llvm::APSInt FirstArg(32);
1310 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001312 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313 return;
1314 }
1315
1316 // check if the function is variadic if the 3rd argument non-zero
1317 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001318 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001319 ++NumArgs; // +1 for ...
1320 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001321 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001322 return;
1323 }
1324 }
1325
Chris Lattner3c73c412008-11-19 08:23:25 +00001326 // strftime requires FirstArg to be 0 because it doesn't read from any
1327 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001328 if (is_strftime) {
1329 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001330 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1331 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001332 return;
1333 }
1334 // if 0 it disables parameter checking (to use with e.g. va_list)
1335 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001337 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001338 return;
1339 }
1340
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001341 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001342 Idx.getZExtValue(), FirstArg.getZExtValue()));
1343}
1344
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001345static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1346 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001348 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 return;
1351 }
1352
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001353 // Try to find the underlying union declaration.
1354 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001355 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001356 if (TD && TD->getUnderlyingType()->isUnionType())
1357 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1358 else
1359 RD = dyn_cast<RecordDecl>(d);
1360
1361 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001362 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001363 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001364 return;
1365 }
1366
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001367 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001368 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001369 diag::warn_transparent_union_attribute_not_definition);
1370 return;
1371 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001372
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001373 RecordDecl::field_iterator Field = RD->field_begin(),
1374 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001375 if (Field == FieldEnd) {
1376 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1377 return;
1378 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001379
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001380 FieldDecl *FirstField = *Field;
1381 QualType FirstType = FirstField->getType();
1382 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001383 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001384 diag::warn_transparent_union_attribute_floating);
1385 return;
1386 }
1387
1388 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1389 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1390 for (; Field != FieldEnd; ++Field) {
1391 QualType FieldType = Field->getType();
1392 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1393 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1394 // Warn if we drop the attribute.
1395 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001396 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001397 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001398 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001399 diag::warn_transparent_union_attribute_field_size_align)
1400 << isSize << Field->getDeclName() << FieldBits;
1401 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001402 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001403 diag::note_transparent_union_first_field_size_align)
1404 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001405 return;
1406 }
1407 }
1408
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001409 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001410}
1411
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001412static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001413 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001414 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001416 return;
1417 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001418 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1419 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001420
Chris Lattner6b6b5372008-06-26 18:38:35 +00001421 // Make sure that there is a string literal as the annotation's single
1422 // argument.
1423 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001424 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425 return;
1426 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001427 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001428 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001429}
1430
Chris Lattner803d0802008-06-29 00:43:07 +00001431static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001432 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001433 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001435 return;
1436 }
1437
1438 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001439 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001440 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001441 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001442 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001443 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001445 }
Mike Stumpbf916502009-07-24 19:02:52 +00001446
Chris Lattner49e2d342008-06-28 23:50:44 +00001447 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1448 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001449 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001450 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1451 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001452 return;
1453 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001454 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001455 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001456 << alignmentExpr->getSourceRange();
1457 return;
1458 }
1459
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001460 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001461}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001462
Mike Stumpbf916502009-07-24 19:02:52 +00001463/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1464/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001465///
Mike Stumpbf916502009-07-24 19:02:52 +00001466/// Despite what would be logical, the mode attribute is a decl attribute, not a
1467/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1468/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001469static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001470 // This attribute isn't documented, but glibc uses it. It changes
1471 // the width of an int or unsigned int to the specified size.
1472
1473 // Check that there aren't any arguments
1474 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001476 return;
1477 }
1478
1479 IdentifierInfo *Name = Attr.getParameterName();
1480 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001482 return;
1483 }
1484 const char *Str = Name->getName();
1485 unsigned Len = Name->getLength();
1486
1487 // Normalize the attribute name, __foo__ becomes foo.
1488 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1489 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1490 Str += 2;
1491 Len -= 4;
1492 }
1493
1494 unsigned DestWidth = 0;
1495 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001496 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001497 switch (Len) {
1498 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001499 switch (Str[0]) {
1500 case 'Q': DestWidth = 8; break;
1501 case 'H': DestWidth = 16; break;
1502 case 'S': DestWidth = 32; break;
1503 case 'D': DestWidth = 64; break;
1504 case 'X': DestWidth = 96; break;
1505 case 'T': DestWidth = 128; break;
1506 }
1507 if (Str[1] == 'F') {
1508 IntegerMode = false;
1509 } else if (Str[1] == 'C') {
1510 IntegerMode = false;
1511 ComplexMode = true;
1512 } else if (Str[1] != 'I') {
1513 DestWidth = 0;
1514 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001515 break;
1516 case 4:
1517 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1518 // pointer on PIC16 and other embedded platforms.
1519 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001520 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001521 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001522 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001523 break;
1524 case 7:
1525 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001526 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001527 break;
1528 }
1529
1530 QualType OldTy;
1531 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1532 OldTy = TD->getUnderlyingType();
1533 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1534 OldTy = VD->getType();
1535 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001536 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1537 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001538 return;
1539 }
Eli Friedman73397492009-03-03 06:41:03 +00001540
1541 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1542 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1543 else if (IntegerMode) {
1544 if (!OldTy->isIntegralType())
1545 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1546 } else if (ComplexMode) {
1547 if (!OldTy->isComplexType())
1548 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1549 } else {
1550 if (!OldTy->isFloatingType())
1551 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1552 }
1553
Mike Stump390b4cc2009-05-16 07:39:55 +00001554 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1555 // and friends, at least with glibc.
1556 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1557 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001558 // FIXME: Make sure floating-point mappings are accurate
1559 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001560 QualType NewTy;
1561 switch (DestWidth) {
1562 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001563 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001564 return;
1565 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001566 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001567 return;
1568 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001569 if (!IntegerMode) {
1570 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1571 return;
1572 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001573 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001574 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001575 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001576 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001577 break;
1578 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001579 if (!IntegerMode) {
1580 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1581 return;
1582 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001583 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001584 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001585 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001586 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001587 break;
1588 case 32:
1589 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001590 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001591 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001592 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001593 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001594 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001595 break;
1596 case 64:
1597 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001598 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001599 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001600 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001601 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001602 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001603 break;
Eli Friedman73397492009-03-03 06:41:03 +00001604 case 96:
1605 NewTy = S.Context.LongDoubleTy;
1606 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001607 case 128:
1608 if (!IntegerMode) {
1609 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1610 return;
1611 }
1612 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001613 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001614 }
1615
Eli Friedman73397492009-03-03 06:41:03 +00001616 if (ComplexMode) {
1617 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001618 }
1619
1620 // Install the new type.
1621 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1622 TD->setUnderlyingType(NewTy);
1623 else
1624 cast<ValueDecl>(D)->setType(NewTy);
1625}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001626
Anders Carlssond87df372009-02-13 06:46:13 +00001627static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1628 // check the attribute arguments.
1629 if (Attr.getNumArgs() > 0) {
1630 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1631 return;
1632 }
Anders Carlssone896d982009-02-13 08:11:52 +00001633
Anders Carlsson5bab7882009-02-19 19:16:48 +00001634 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001635 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001636 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001637 return;
1638 }
Mike Stumpbf916502009-07-24 19:02:52 +00001639
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001640 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001641}
1642
Anders Carlsson5bab7882009-02-19 19:16:48 +00001643static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1644 // check the attribute arguments.
1645 if (Attr.getNumArgs() != 0) {
1646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1647 return;
1648 }
Mike Stumpbf916502009-07-24 19:02:52 +00001649
Chris Lattnerc5197432009-04-14 17:02:11 +00001650 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001651 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001652 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001653 return;
1654 }
Mike Stumpbf916502009-07-24 19:02:52 +00001655
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001656 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001657}
1658
Chris Lattnercf2a7212009-04-20 19:12:28 +00001659static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001660 // check the attribute arguments.
1661 if (Attr.getNumArgs() != 0) {
1662 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1663 return;
1664 }
Mike Stumpbf916502009-07-24 19:02:52 +00001665
Chris Lattnerc5197432009-04-14 17:02:11 +00001666 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1667 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001669 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001670 return;
1671 }
Mike Stumpbf916502009-07-24 19:02:52 +00001672
Chris Lattnerc5197432009-04-14 17:02:11 +00001673 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001674 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001675 return;
1676 }
Mike Stumpbf916502009-07-24 19:02:52 +00001677
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001678 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001679}
1680
Fariborz Jahanianee760332009-03-27 18:38:55 +00001681static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1682 // check the attribute arguments.
1683 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001685 return;
1686 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001687
Fariborz Jahanianee760332009-03-27 18:38:55 +00001688 if (!isFunctionOrMethod(d)) {
1689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001690 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001691 return;
1692 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001693
1694 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1695 llvm::APSInt NumParams(32);
1696 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1697 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1698 << "regparm" << NumParamsExpr->getSourceRange();
1699 return;
1700 }
1701
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001702 if (S.Context.Target.getRegParmMax() == 0) {
1703 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001704 << NumParamsExpr->getSourceRange();
1705 return;
1706 }
1707
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001708 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001709 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1710 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001711 return;
1712 }
1713
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001714 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001715}
1716
Chris Lattner0744e5f2008-06-29 00:23:49 +00001717//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001718// Checker-specific attribute handlers.
1719//===----------------------------------------------------------------------===//
1720
1721static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1722 Sema &S) {
1723
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001724 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001725
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001726 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1727 RetTy = MD->getResultType();
1728 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1729 RetTy = FD->getResultType();
1730 else {
1731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1732 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001733 return;
1734 }
Mike Stumpbf916502009-07-24 19:02:52 +00001735
Ted Kremenek6217b802009-07-29 21:53:49 +00001736 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
Steve Naroff14108da2009-07-10 23:34:53 +00001737 || RetTy->getAsObjCObjectPointerType())) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001738 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1739 << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001740 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001741 }
Mike Stumpbf916502009-07-24 19:02:52 +00001742
Ted Kremenekb71368d2009-05-09 02:44:38 +00001743 switch (Attr.getKind()) {
1744 default:
1745 assert(0 && "invalid ownership attribute");
1746 return;
1747 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001748 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001749 return;
1750 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001751 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001752 return;
1753 };
1754}
1755
1756//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001757// Top Level Sema Entry Points
1758//===----------------------------------------------------------------------===//
1759
Sebastian Redla89d82c2008-12-21 19:24:58 +00001760/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001761/// the attribute applies to decls. If the attribute is a type attribute, just
1762/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001763static void ProcessDeclAttribute(Scope *scope, Decl *D,
1764 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001765 if (Attr.isDeclspecAttribute())
1766 // FIXME: Try to deal with __declspec attributes!
1767 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001768 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001769 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001770 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001771 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001772 // Ignore these, these are type attributes, handled by
1773 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001774 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001775 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1776 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001777 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001778 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001779 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001780 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001781 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1782 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1783 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1784 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1785 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1786 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001787 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001788 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001789 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001790 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1791 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001792 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001793 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001794 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001795 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001796 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1797 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1798 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001799
1800 // Checker-specific.
1801 case AttributeList::AT_ns_returns_retained:
1802 case AttributeList::AT_cf_returns_retained:
1803 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1804
Nate Begeman6f3d8382009-06-26 06:32:41 +00001805 case AttributeList::AT_reqd_wg_size:
1806 HandleReqdWorkGroupSize(D, Attr, S); break;
1807
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001808 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001809 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001810 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001811 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001812 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001813 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001814 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001815 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001816 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1817 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001818 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001819 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001820 case AttributeList::AT_transparent_union:
1821 HandleTransparentUnionAttr(D, Attr, S);
1822 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001823 case AttributeList::AT_objc_exception:
1824 HandleObjCExceptionAttr(D, Attr, S);
1825 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001826 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001827 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001828 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001829 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001830 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1831 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001832 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001833 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001834 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001835 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001836 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001837 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001838 // Just ignore
1839 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001840 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001841 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001842 break;
1843 }
1844}
1845
1846/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1847/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001848void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001849 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001850 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001851 AttrList = AttrList->getNext();
1852 }
1853}
1854
Ryan Flynne25ff832009-07-30 03:15:39 +00001855/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1856/// #pragma weak needs a non-definition decl and source may not have one
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001857NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II)
Ryan Flynne25ff832009-07-30 03:15:39 +00001858{
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001859 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001860 NamedDecl *NewD = 0;
1861 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1862 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1863 FD->getLocation(), DeclarationName(II),
1864 FD->getType());
1865 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1866 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1867 VD->getLocation(), II,
1868 VD->getType(), VD->getStorageClass());
1869 }
1870 return NewD;
1871}
1872
1873/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1874/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001875void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001876 if (!W.getUsed()) { // only do this once
1877 W.setUsed(true);
1878 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1879 IdentifierInfo *NDId = ND->getIdentifier();
1880 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1881 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1882 NewD->addAttr(::new (Context) WeakAttr());
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001883 WeakTopLevelDecl.push_back(NewD);
1884 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1885 // to insert Decl at TU scope, sorry.
1886 DeclContext *SavedContext = CurContext;
1887 CurContext = Context.getTranslationUnitDecl();
1888 PushOnScopeChains(NewD, S);
1889 CurContext = SavedContext;
Ryan Flynne25ff832009-07-30 03:15:39 +00001890 } else { // just add weak to existing
1891 ND->addAttr(::new (Context) WeakAttr());
1892 }
1893 }
1894}
1895
Chris Lattner0744e5f2008-06-29 00:23:49 +00001896/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1897/// it, apply them to D. This is a bit tricky because PD can have attributes
1898/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001899void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001900 // Handle #pragma weak
1901 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1902 if (ND->hasLinkage()) {
1903 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1904 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001905 // Identifier referenced by #pragma weak before it was declared
1906 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001907 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1908 }
1909 }
1910 }
1911
Chris Lattner0744e5f2008-06-29 00:23:49 +00001912 // Apply decl attributes from the DeclSpec if present.
1913 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001914 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001915
Chris Lattner0744e5f2008-06-29 00:23:49 +00001916 // Walk the declarator structure, applying decl attributes that were in a type
1917 // position to the decl itself. This handles cases like:
1918 // int *__attr__(x)** D;
1919 // when X is a decl attribute.
1920 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1921 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001922 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001923
Chris Lattner0744e5f2008-06-29 00:23:49 +00001924 // Finally, apply any attributes on the decl itself.
1925 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001926 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001927}