blob: 2f86fe1ffe702d79ab9612a0a1ea651632499054 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000027static const FunctionType *getFunctionType(Decl *d, bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30 Ty = decl->getType();
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32 Ty = decl->getType();
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
35 else
36 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000037
Chris Lattner6b6b5372008-06-26 18:38:35 +000038 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000039 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000040 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000042
43 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000044}
45
Daniel Dunbar35682492008-09-26 04:12:28 +000046// FIXME: We should provide an abstraction around a method or function
47// to provide the following bits of information.
48
Daniel Dunbard3f2c102008-10-19 02:04:16 +000049/// isFunctionOrMethod - Return true if the given decl has function
50/// type (function or function-typed variable) or an Objective-C
51/// method.
Daniel Dunbar35682492008-09-26 04:12:28 +000052static bool isFunctionOrMethod(Decl *d) {
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000053 return getFunctionType(d, false) || isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000054}
55
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000056/// isFunctionOrMethodOrBlock - Return true if the given decl has function
57/// type (function or function-typed variable) or an Objective-C
58/// method or a block.
59static bool isFunctionOrMethodOrBlock(Decl *d) {
60 if (isFunctionOrMethod(d))
61 return true;
62 // check for block is more involved.
63 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
64 QualType Ty = V->getType();
65 return Ty->isBlockPointerType();
66 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000067 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068}
69
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070/// hasFunctionProto - Return true if the given decl has a argument
71/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000072/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Daniel Dunbard3f2c102008-10-19 02:04:16 +000073static bool hasFunctionProto(Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000074 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000075 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000077 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078 return true;
79 }
80}
81
82/// getFunctionOrMethodNumArgs - Return number of function or method
83/// arguments. It is an error to call this on a K&R function (use
84/// hasFunctionProto first).
Daniel Dunbar35682492008-09-26 04:12:28 +000085static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000086 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000087 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000088 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
89 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000090 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000091}
92
93static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +000098
Chris Lattner89951a82009-02-20 18:43:26 +000099 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000100}
101
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000102static QualType getFunctionOrMethodResultType(Decl *d) {
103 if (const FunctionType *FnTy = getFunctionType(d))
104 return cast<FunctionProtoType>(FnTy)->getResultType();
105 return cast<ObjCMethodDecl>(d)->getResultType();
106}
107
Daniel Dunbar35682492008-09-26 04:12:28 +0000108static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000109 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000110 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000111 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000112 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
113 return BD->IsVariadic();
114 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000115 return cast<ObjCMethodDecl>(d)->isVariadic();
116 }
117}
118
Chris Lattner6b6b5372008-06-26 18:38:35 +0000119static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Steve Naroff14108da2009-07-10 23:34:53 +0000120 const ObjCObjectPointerType *PT = T->getAsObjCObjectPointerType();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000121 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000122 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000123
Chris Lattnerb77792e2008-07-26 22:17:49 +0000124 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000125 if (!ClsT)
126 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000127
Chris Lattner6b6b5372008-06-26 18:38:35 +0000128 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000129
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 // FIXME: Should we walk the chain of classes?
131 return ClsName == &Ctx.Idents.get("NSString") ||
132 ClsName == &Ctx.Idents.get("NSMutableString");
133}
134
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000135static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000136 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000137 if (!PT)
138 return false;
139
Ted Kremenek6217b802009-07-29 21:53:49 +0000140 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000141 if (!RT)
142 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000143
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000144 const RecordDecl *RD = RT->getDecl();
145 if (RD->getTagKind() != TagDecl::TK_struct)
146 return false;
147
148 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
149}
150
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000151//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000152// Attribute Implementations
153//===----------------------------------------------------------------------===//
154
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000155// FIXME: All this manual attribute parsing code is gross. At the
156// least add some helper functions to check most argument patterns (#
157// and types of args).
158
Mike Stumpbf916502009-07-24 19:02:52 +0000159static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000160 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000161 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
162 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000163 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000164 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165 }
Mike Stumpbf916502009-07-24 19:02:52 +0000166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168
169 Expr *sizeExpr;
170
171 // Special case where the argument is a template id.
172 if (Attr.getParameterName()) {
173 sizeExpr = S.ActOnDeclarationNameExpr(scope, Attr.getLoc(),
174 Attr.getParameterName(),
175 false, 0, false).takeAs<Expr>();
176 } else {
177 // check the attribute arguments.
178 if (Attr.getNumArgs() != 1) {
179 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
180 return;
181 }
182 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000184
185 // Instantiate/Install the vector type, and let Sema build the type for us.
186 // This will run the reguired checks.
187 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
188 if (!T.isNull()) {
189 tDecl->setUnderlyingType(T);
Mike Stumpbf916502009-07-24 19:02:52 +0000190
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000191 // Remember this typedef decl, we will need it later for diagnostics.
192 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000194}
195
Chris Lattner065c5a82008-06-28 23:48:25 +0000196
Mike Stumpbf916502009-07-24 19:02:52 +0000197/// HandleVectorSizeAttribute - this attribute is only applicable to integral
198/// and float scalars, although arrays, pointers, and function return values are
199/// allowed in conjunction with this construct. Aggregates with this attribute
200/// are invalid, even if they are of the same size as a corresponding scalar.
201/// The raw attribute should contain precisely 1 argument, the vector size for
202/// the variable, measured in bytes. If curType and rawAttr are well formed,
203/// this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000204static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000205 QualType CurType;
206 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
207 CurType = VD->getType();
208 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
209 CurType = TD->getUnderlyingType();
210 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000211 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
212 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000213 return;
214 }
Mike Stumpbf916502009-07-24 19:02:52 +0000215
Chris Lattner065c5a82008-06-28 23:48:25 +0000216 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000217 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000218 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000219 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000220 }
Chris Lattner545dd342008-06-28 23:36:30 +0000221 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000223 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
225 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000226 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000227 }
Mike Stumpbf916502009-07-24 19:02:52 +0000228 // navigate to the base type - we need to provide for vector pointers, vector
229 // arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000230 if (CurType->isPointerType() || CurType->isArrayType() ||
231 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000232 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
233 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000234 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
235 do {
236 if (PointerType *PT = dyn_cast<PointerType>(canonType))
237 canonType = PT->getPointeeType().getTypePtr();
238 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
239 canonType = AT->getElementType().getTypePtr();
240 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
241 canonType = FT->getResultType().getTypePtr();
242 } while (canonType->isPointerType() || canonType->isArrayType() ||
243 canonType->isFunctionType());
244 */
245 }
Chris Lattner82afa2d2009-05-13 05:13:44 +0000246 // the base type must be integer or float, and can't already be a vector.
247 if (CurType->isVectorType() ||
248 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000249 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000250 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000251 }
Chris Lattner803d0802008-06-29 00:43:07 +0000252 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000253 // vecSize is specified in bytes - convert to bits.
Mike Stumpbf916502009-07-24 19:02:52 +0000254 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
255
Chris Lattner6b6b5372008-06-26 18:38:35 +0000256 // the vector size needs to be an integral multiple of the type size.
257 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000258 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
259 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000260 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000261 }
262 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000263 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
264 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000265 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000266 }
Mike Stumpbf916502009-07-24 19:02:52 +0000267
Chris Lattner065c5a82008-06-28 23:48:25 +0000268 // Success! Instantiate the vector type, the number of elements is > 0, and
269 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000270 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Mike Stumpbf916502009-07-24 19:02:52 +0000271
Chris Lattner065c5a82008-06-28 23:48:25 +0000272 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
273 VD->setType(CurType);
Mike Stumpbf916502009-07-24 19:02:52 +0000274 else
Chris Lattner065c5a82008-06-28 23:48:25 +0000275 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000276}
277
Chris Lattner803d0802008-06-29 00:43:07 +0000278static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000279 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000280 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000282 return;
283 }
Mike Stumpbf916502009-07-24 19:02:52 +0000284
Chris Lattner6b6b5372008-06-26 18:38:35 +0000285 if (TagDecl *TD = dyn_cast<TagDecl>(d))
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
440 d->addAttr(::new (S.Context) MallocAttr());
441}
442
Ted Kremenekb7252322009-04-10 00:01:14 +0000443static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000444 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000445 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000446 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000448 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000449 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000450
Mike Stump19c30c02009-04-29 19:03:13 +0000451 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
452 ValueDecl *VD = dyn_cast<ValueDecl>(d);
453 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
454 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000455 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000456 return false;
457 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000458 }
Mike Stumpbf916502009-07-24 19:02:52 +0000459
Ted Kremenekb7252322009-04-10 00:01:14 +0000460 return true;
461}
462
463static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000464 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000465 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000466}
467
468static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
469 Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000470 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000471 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000472}
473
Ted Kremenek73798892008-07-25 04:39:19 +0000474static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
475 // check the attribute arguments.
476 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000477 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000478 return;
479 }
Mike Stumpbf916502009-07-24 19:02:52 +0000480
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000481 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000482 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000483 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000484 return;
485 }
Mike Stumpbf916502009-07-24 19:02:52 +0000486
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000487 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000488}
489
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000490static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
491 // check the attribute arguments.
492 if (Attr.getNumArgs() != 0) {
493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
494 return;
495 }
Mike Stumpbf916502009-07-24 19:02:52 +0000496
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000497 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000498 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000499 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
500 return;
501 }
502 } else if (!isFunctionOrMethod(d)) {
503 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000504 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000505 return;
506 }
Mike Stumpbf916502009-07-24 19:02:52 +0000507
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000508 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000509}
510
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000511static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
512 // check the attribute arguments.
513 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000514 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
515 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000516 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000517 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000518
519 int priority = 65535; // FIXME: Do not hardcode such constants.
520 if (Attr.getNumArgs() > 0) {
521 Expr *E = static_cast<Expr *>(Attr.getArg(0));
522 llvm::APSInt Idx(32);
523 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000524 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000525 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000526 return;
527 }
528 priority = Idx.getZExtValue();
529 }
Mike Stumpbf916502009-07-24 19:02:52 +0000530
Chris Lattnerc5197432009-04-14 17:02:11 +0000531 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000532 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000533 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000534 return;
535 }
536
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000537 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000538}
539
540static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
541 // check the attribute arguments.
542 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
544 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000545 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000546 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000547
548 int priority = 65535; // FIXME: Do not hardcode such constants.
549 if (Attr.getNumArgs() > 0) {
550 Expr *E = static_cast<Expr *>(Attr.getArg(0));
551 llvm::APSInt Idx(32);
552 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000553 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000554 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000555 return;
556 }
557 priority = Idx.getZExtValue();
558 }
Mike Stumpbf916502009-07-24 19:02:52 +0000559
Anders Carlsson6782fc62008-08-22 22:10:48 +0000560 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000561 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000562 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000563 return;
564 }
565
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000566 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000567}
568
Chris Lattner803d0802008-06-29 00:43:07 +0000569static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000570 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000571 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000573 return;
574 }
Mike Stumpbf916502009-07-24 19:02:52 +0000575
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000576 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000577}
578
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000579static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
580 // check the attribute arguments.
581 if (Attr.getNumArgs() != 0) {
582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
583 return;
584 }
Mike Stumpbf916502009-07-24 19:02:52 +0000585
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000586 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000587}
588
Chris Lattner803d0802008-06-29 00:43:07 +0000589static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000590 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000591 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000592 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000593 return;
594 }
Mike Stumpbf916502009-07-24 19:02:52 +0000595
Chris Lattner545dd342008-06-28 23:36:30 +0000596 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000597 Arg = Arg->IgnoreParenCasts();
598 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000599
Chris Lattner6b6b5372008-06-26 18:38:35 +0000600 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000601 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000602 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000603 return;
604 }
Mike Stumpbf916502009-07-24 19:02:52 +0000605
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 const char *TypeStr = Str->getStrData();
607 unsigned TypeLen = Str->getByteLength();
608 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000609
Chris Lattner6b6b5372008-06-26 18:38:35 +0000610 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
611 type = VisibilityAttr::DefaultVisibility;
612 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
613 type = VisibilityAttr::HiddenVisibility;
614 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
615 type = VisibilityAttr::HiddenVisibility; // FIXME
616 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
617 type = VisibilityAttr::ProtectedVisibility;
618 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000619 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000620 return;
621 }
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000623 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000624}
625
Chris Lattner0db29ec2009-02-14 08:09:34 +0000626static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
627 Sema &S) {
628 if (Attr.getNumArgs() != 0) {
629 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
630 return;
631 }
Mike Stumpbf916502009-07-24 19:02:52 +0000632
Chris Lattner0db29ec2009-02-14 08:09:34 +0000633 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
634 if (OCI == 0) {
635 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
636 return;
637 }
Mike Stumpbf916502009-07-24 19:02:52 +0000638
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000639 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000640}
641
642static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000643 if (Attr.getNumArgs() != 0) {
644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
645 return;
646 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000647 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000648 QualType T = TD->getUnderlyingType();
649 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000650 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000651 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
652 return;
653 }
654 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000655 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000656}
657
Mike Stumpbf916502009-07-24 19:02:52 +0000658static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000659HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
660 if (Attr.getNumArgs() != 0) {
661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
662 return;
663 }
664
665 if (!isa<FunctionDecl>(D)) {
666 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
667 return;
668 }
669
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000670 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000671}
672
Steve Naroff9eae5762008-09-18 16:44:58 +0000673static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000674 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000675 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000676 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000677 return;
678 }
Mike Stumpbf916502009-07-24 19:02:52 +0000679
Steve Naroff9eae5762008-09-18 16:44:58 +0000680 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000682 return;
683 }
Mike Stumpbf916502009-07-24 19:02:52 +0000684
Steve Naroff9eae5762008-09-18 16:44:58 +0000685 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000686 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000687 type = BlocksAttr::ByRef;
688 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000689 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000690 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000691 return;
692 }
Mike Stumpbf916502009-07-24 19:02:52 +0000693
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000694 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000695}
696
Anders Carlsson77091822008-10-05 18:05:59 +0000697static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
698 // check the attribute arguments.
699 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000700 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
701 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000702 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000703 }
704
Anders Carlsson77091822008-10-05 18:05:59 +0000705 int sentinel = 0;
706 if (Attr.getNumArgs() > 0) {
707 Expr *E = static_cast<Expr *>(Attr.getArg(0));
708 llvm::APSInt Idx(32);
709 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000710 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000711 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000712 return;
713 }
714 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000715
Anders Carlsson77091822008-10-05 18:05:59 +0000716 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000717 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
718 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000719 return;
720 }
721 }
722
723 int nullPos = 0;
724 if (Attr.getNumArgs() > 1) {
725 Expr *E = static_cast<Expr *>(Attr.getArg(1));
726 llvm::APSInt Idx(32);
727 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000728 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000729 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000730 return;
731 }
732 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000733
Anders Carlsson77091822008-10-05 18:05:59 +0000734 if (nullPos > 1 || nullPos < 0) {
735 // FIXME: This error message could be improved, it would be nice
736 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000737 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
738 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000739 return;
740 }
741 }
742
743 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000744 const FunctionType *FT = FD->getType()->getAsFunctionType();
745 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000746
Chris Lattner897cd902009-03-17 23:03:47 +0000747 if (isa<FunctionNoProtoType>(FT)) {
748 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
749 return;
750 }
Mike Stumpbf916502009-07-24 19:02:52 +0000751
Chris Lattner897cd902009-03-17 23:03:47 +0000752 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000753 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000754 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000755 }
Anders Carlsson77091822008-10-05 18:05:59 +0000756 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
757 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000758 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000759 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000760 }
761 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000762 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
763 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000764 ;
765 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000766 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000767 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000768 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Ted Kremenek6217b802009-07-29 21:53:49 +0000769 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000770 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000771 int m = Ty->isFunctionPointerType() ? 0 : 1;
772 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000773 return;
774 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000775 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000776 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000777 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000778 return;
779 }
Anders Carlsson77091822008-10-05 18:05:59 +0000780 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000782 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000783 return;
784 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000785 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000786}
787
Chris Lattner026dc962009-02-14 07:37:35 +0000788static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
789 // check the attribute arguments.
790 if (Attr.getNumArgs() != 0) {
791 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
792 return;
793 }
794
795 // TODO: could also be applied to methods?
796 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
797 if (!Fn) {
798 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000799 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000800 return;
801 }
Mike Stumpbf916502009-07-24 19:02:52 +0000802
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000803 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000804}
805
806static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000807 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000808 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000810 return;
811 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000812
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000813 /* weak only applies to non-static declarations */
814 bool isStatic = false;
815 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
816 isStatic = VD->getStorageClass() == VarDecl::Static;
817 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
818 isStatic = FD->getStorageClass() == FunctionDecl::Static;
819 }
820 if (isStatic) {
821 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
822 dyn_cast<NamedDecl>(D)->getNameAsString();
823 return;
824 }
825
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000826 // TODO: could also be applied to methods?
827 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000829 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000830 return;
831 }
Mike Stumpbf916502009-07-24 19:02:52 +0000832
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000833 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000834}
835
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000836static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
837 // check the attribute arguments.
838 if (Attr.getNumArgs() != 0) {
839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
840 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000841 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000842
843 // weak_import only applies to variable & function declarations.
844 bool isDef = false;
845 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
846 isDef = (!VD->hasExternalStorage() || VD->getInit());
847 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000848 isDef = FD->getBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000849 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
850 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000851 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000852 } else {
853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000854 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000855 return;
856 }
857
858 // Merge should handle any subsequent violations.
859 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000860 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000861 diag::warn_attribute_weak_import_invalid_on_definition)
862 << "weak_import" << 2 /*variable and function*/;
863 return;
864 }
865
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000866 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000867}
868
Chris Lattner026dc962009-02-14 07:37:35 +0000869static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000870 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000871 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000872 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000873 return;
874 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000875
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000876 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000877 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000878 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000879 return;
880 }
881
Chris Lattner026dc962009-02-14 07:37:35 +0000882 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000883 if (!FD) {
884 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000885 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000886 return;
887 }
888
889 // Currently, the dllimport attribute is ignored for inlined functions.
890 // Warning is emitted.
891 if (FD->isInline()) {
892 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
893 return;
894 }
895
896 // The attribute is also overridden by a subsequent declaration as dllexport.
897 // Warning is emitted.
898 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
899 nextAttr = nextAttr->getNext()) {
900 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
901 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
902 return;
903 }
904 }
905
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000906 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000907 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
908 return;
909 }
910
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000911 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000912}
913
Chris Lattner026dc962009-02-14 07:37:35 +0000914static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000915 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000916 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000917 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000918 return;
919 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000920
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000921 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000922 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000923 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000924 return;
925 }
926
Chris Lattner026dc962009-02-14 07:37:35 +0000927 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000928 if (!FD) {
929 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000930 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000931 return;
932 }
933
Mike Stumpbf916502009-07-24 19:02:52 +0000934 // Currently, the dllexport attribute is ignored for inlined functions, unless
935 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000936 if (FD->isInline()) {
937 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
938 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
939 return;
940 }
941
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000942 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000943}
944
Nate Begeman6f3d8382009-06-26 06:32:41 +0000945static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
946 Sema &S) {
947 // Attribute has 3 arguments.
948 if (Attr.getNumArgs() != 3) {
949 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
950 return;
951 }
952
953 unsigned WGSize[3];
954 for (unsigned i = 0; i < 3; ++i) {
955 Expr *E = static_cast<Expr *>(Attr.getArg(i));
956 llvm::APSInt ArgNum(32);
957 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
958 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
959 << "reqd_work_group_size" << E->getSourceRange();
960 return;
961 }
962 WGSize[i] = (unsigned) ArgNum.getZExtValue();
963 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000964 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000965 WGSize[2]));
966}
967
Chris Lattner026dc962009-02-14 07:37:35 +0000968static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000969 // Attribute has no arguments.
970 if (Attr.getNumArgs() != 1) {
971 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
972 return;
973 }
974
975 // Make sure that there is a string literal as the sections's single
976 // argument.
Mike Stumpbf916502009-07-24 19:02:52 +0000977 StringLiteral *SE =
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000978 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
979 if (!SE) {
980 // FIXME
981 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
982 return;
983 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000984 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000985 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000986}
987
Chris Lattner803d0802008-06-29 00:43:07 +0000988static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000989 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000990 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000991 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000992 return;
993 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000994
995 // Attribute can be applied only to functions.
996 if (!isa<FunctionDecl>(d)) {
997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000998 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000999 return;
1000 }
1001
1002 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001003 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001004 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1005 << "stdcall" << "fastcall";
1006 return;
1007 }
1008
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001009 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001010}
1011
Chris Lattner803d0802008-06-29 00:43:07 +00001012static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001013 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001014 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001015 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001016 return;
1017 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001018
1019 if (!isa<FunctionDecl>(d)) {
1020 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001021 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001022 return;
1023 }
1024
1025 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001026 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +00001027 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1028 << "fastcall" << "stdcall";
1029 return;
1030 }
1031
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001032 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001033}
1034
Chris Lattner803d0802008-06-29 00:43:07 +00001035static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001036 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001037 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001039 return;
1040 }
Mike Stumpbf916502009-07-24 19:02:52 +00001041
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001042 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001043}
1044
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001045static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1046 // check the attribute arguments.
1047 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001048 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001049 return;
1050 }
Mike Stumpbf916502009-07-24 19:02:52 +00001051
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001052 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001053}
1054
1055static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1056 // check the attribute arguments.
1057 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001058 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001059 return;
1060 }
Mike Stumpbf916502009-07-24 19:02:52 +00001061
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001062 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001063}
1064
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001065static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001066 // Match gcc which ignores cleanup attrs when compiling C++.
1067 if (S.getLangOptions().CPlusPlus)
1068 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001069
1070 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001071 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1072 return;
1073 }
Mike Stumpbf916502009-07-24 19:02:52 +00001074
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001075 if (Attr.getNumArgs() != 0) {
1076 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1077 return;
1078 }
Mike Stumpbf916502009-07-24 19:02:52 +00001079
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001080 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001081
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001082 if (!VD || !VD->hasLocalStorage()) {
1083 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1084 return;
1085 }
Mike Stumpbf916502009-07-24 19:02:52 +00001086
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001087 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001088 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1089 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001090 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001091 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001092 Attr.getParameterName();
1093 return;
1094 }
Mike Stumpbf916502009-07-24 19:02:52 +00001095
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001096 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1097 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001098 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001099 Attr.getParameterName();
1100 return;
1101 }
1102
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001103 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001104 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001105 Attr.getParameterName();
1106 return;
1107 }
Mike Stumpbf916502009-07-24 19:02:52 +00001108
Anders Carlsson89941c12009-02-07 23:16:50 +00001109 // We're currently more strict than GCC about what function types we accept.
1110 // If this ever proves to be a problem it should be easy to fix.
1111 QualType Ty = S.Context.getPointerType(VD->getType());
1112 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001113 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001114 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001115 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1116 Attr.getParameterName() << ParamTy << Ty;
1117 return;
1118 }
Mike Stumpbf916502009-07-24 19:02:52 +00001119
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001120 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001121}
1122
Mike Stumpbf916502009-07-24 19:02:52 +00001123/// Handle __attribute__((format_arg((idx)))) attribute based on
1124/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1125static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001126 if (Attr.getNumArgs() != 1) {
1127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1128 return;
1129 }
1130 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1131 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1132 << Attr.getName() << 0 /*function*/;
1133 return;
1134 }
Mike Stumpbf916502009-07-24 19:02:52 +00001135 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1136 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001137 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1138 unsigned FirstIdx = 1;
1139 // checks for the 2nd argument
1140 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1141 llvm::APSInt Idx(32);
1142 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1144 << "format" << 2 << IdxExpr->getSourceRange();
1145 return;
1146 }
Mike Stumpbf916502009-07-24 19:02:52 +00001147
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001148 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1150 << "format" << 2 << IdxExpr->getSourceRange();
1151 return;
1152 }
Mike Stumpbf916502009-07-24 19:02:52 +00001153
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001154 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001155
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001156 // make sure the format string is really a string
1157 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001158
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001159 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1160 if (not_nsstring_type &&
1161 !isCFStringType(Ty, S.Context) &&
1162 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001163 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001164 // FIXME: Should highlight the actual expression that has the wrong type.
1165 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001166 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001167 << IdxExpr->getSourceRange();
1168 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001169 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001170 Ty = getFunctionOrMethodResultType(d);
1171 if (!isNSStringType(Ty, S.Context) &&
1172 !isCFStringType(Ty, S.Context) &&
1173 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001174 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001175 // FIXME: Should highlight the actual expression that has the wrong type.
1176 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001177 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001178 << IdxExpr->getSourceRange();
1179 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001180 }
1181
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001182 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001183}
1184
Mike Stumpbf916502009-07-24 19:02:52 +00001185/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1186/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001187static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188
Chris Lattner545dd342008-06-28 23:36:30 +00001189 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001190 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001191 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001192 return;
1193 }
1194
Chris Lattner545dd342008-06-28 23:36:30 +00001195 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001196 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001197 return;
1198 }
1199
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001200 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001202 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001203 return;
1204 }
1205
Mike Stumpbf916502009-07-24 19:02:52 +00001206 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1207 // needed in order to be compatible with GCC the index must start in 1 and the
1208 // limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001209 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001210 unsigned FirstIdx = 1;
1211
Chris Lattner545dd342008-06-28 23:36:30 +00001212 const char *Format = Attr.getParameterName()->getName();
1213 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001214
1215 // Normalize the argument, __foo__ becomes foo.
1216 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1217 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1218 Format += 2;
1219 FormatLen -= 4;
1220 }
1221
1222 bool Supported = false;
1223 bool is_NSString = false;
1224 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001225 bool is_CFString = false;
Mike Stumpbf916502009-07-24 19:02:52 +00001226
Chris Lattner6b6b5372008-06-26 18:38:35 +00001227 switch (FormatLen) {
1228 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001229 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1230 case 6: Supported = !memcmp(Format, "printf", 6); break;
Ryan Flynn4403a5e2009-08-06 03:00:50 +00001231 case 7: Supported = !memcmp(Format, "printf0", 7) ||
1232 !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001233 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001234 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1235 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1236 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001237 break;
1238 }
Mike Stumpbf916502009-07-24 19:02:52 +00001239
Chris Lattner6b6b5372008-06-26 18:38:35 +00001240 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001241 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1242 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001243 return;
1244 }
1245
1246 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001247 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001248 llvm::APSInt Idx(32);
1249 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001251 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001252 return;
1253 }
1254
1255 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001256 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001257 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001258 return;
1259 }
1260
1261 // FIXME: Do we need to bounds check?
1262 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001263
Chris Lattner6b6b5372008-06-26 18:38:35 +00001264 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001265 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001266
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001267 if (is_CFString) {
1268 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001269 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1270 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001271 return;
1272 }
1273 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001274 // FIXME: do we need to check if the type is NSString*? What are the
1275 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001276 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001277 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001278 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1279 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001280 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001281 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001282 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001283 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001284 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001285 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1286 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001287 return;
1288 }
1289
1290 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001291 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001292 llvm::APSInt FirstArg(32);
1293 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001294 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001295 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001296 return;
1297 }
1298
1299 // check if the function is variadic if the 3rd argument non-zero
1300 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001301 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001302 ++NumArgs; // +1 for ...
1303 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001304 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001305 return;
1306 }
1307 }
1308
Chris Lattner3c73c412008-11-19 08:23:25 +00001309 // strftime requires FirstArg to be 0 because it doesn't read from any
1310 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001311 if (is_strftime) {
1312 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001313 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1314 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001315 return;
1316 }
1317 // if 0 it disables parameter checking (to use with e.g. va_list)
1318 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001320 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001321 return;
1322 }
1323
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001324 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001325 Idx.getZExtValue(), FirstArg.getZExtValue()));
1326}
1327
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001328static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1329 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001330 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001331 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001333 return;
1334 }
1335
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001336 // Try to find the underlying union declaration.
1337 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001338 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001339 if (TD && TD->getUnderlyingType()->isUnionType())
1340 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1341 else
1342 RD = dyn_cast<RecordDecl>(d);
1343
1344 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001345 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001346 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347 return;
1348 }
1349
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001350 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001351 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001352 diag::warn_transparent_union_attribute_not_definition);
1353 return;
1354 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001355
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001356 RecordDecl::field_iterator Field = RD->field_begin(),
1357 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001358 if (Field == FieldEnd) {
1359 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1360 return;
1361 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001362
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001363 FieldDecl *FirstField = *Field;
1364 QualType FirstType = FirstField->getType();
1365 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001366 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001367 diag::warn_transparent_union_attribute_floating);
1368 return;
1369 }
1370
1371 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1372 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1373 for (; Field != FieldEnd; ++Field) {
1374 QualType FieldType = Field->getType();
1375 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1376 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1377 // Warn if we drop the attribute.
1378 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001379 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001380 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001381 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001382 diag::warn_transparent_union_attribute_field_size_align)
1383 << isSize << Field->getDeclName() << FieldBits;
1384 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001385 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001386 diag::note_transparent_union_first_field_size_align)
1387 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001388 return;
1389 }
1390 }
1391
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001392 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001393}
1394
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001395static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001396 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001397 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001399 return;
1400 }
Chris Lattner545dd342008-06-28 23:36:30 +00001401 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001402 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001403
Chris Lattner6b6b5372008-06-26 18:38:35 +00001404 // Make sure that there is a string literal as the annotation's single
1405 // argument.
1406 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001407 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001408 return;
1409 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001410 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001411 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001412}
1413
Chris Lattner803d0802008-06-29 00:43:07 +00001414static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001415 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001416 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001417 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001418 return;
1419 }
1420
1421 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001422 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001423 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001424 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425 Align = 128;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001426 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001427 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001428 }
Mike Stumpbf916502009-07-24 19:02:52 +00001429
Chris Lattner49e2d342008-06-28 23:50:44 +00001430 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1431 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001432 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001433 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1434 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001435 return;
1436 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001437 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpbf916502009-07-24 19:02:52 +00001438 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001439 << alignmentExpr->getSourceRange();
1440 return;
1441 }
1442
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001443 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001445
Mike Stumpbf916502009-07-24 19:02:52 +00001446/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1447/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001448///
Mike Stumpbf916502009-07-24 19:02:52 +00001449/// Despite what would be logical, the mode attribute is a decl attribute, not a
1450/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1451/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001452static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001453 // This attribute isn't documented, but glibc uses it. It changes
1454 // the width of an int or unsigned int to the specified size.
1455
1456 // Check that there aren't any arguments
1457 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001459 return;
1460 }
1461
1462 IdentifierInfo *Name = Attr.getParameterName();
1463 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001464 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001465 return;
1466 }
1467 const char *Str = Name->getName();
1468 unsigned Len = Name->getLength();
1469
1470 // Normalize the attribute name, __foo__ becomes foo.
1471 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1472 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1473 Str += 2;
1474 Len -= 4;
1475 }
1476
1477 unsigned DestWidth = 0;
1478 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001479 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001480 switch (Len) {
1481 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001482 switch (Str[0]) {
1483 case 'Q': DestWidth = 8; break;
1484 case 'H': DestWidth = 16; break;
1485 case 'S': DestWidth = 32; break;
1486 case 'D': DestWidth = 64; break;
1487 case 'X': DestWidth = 96; break;
1488 case 'T': DestWidth = 128; break;
1489 }
1490 if (Str[1] == 'F') {
1491 IntegerMode = false;
1492 } else if (Str[1] == 'C') {
1493 IntegerMode = false;
1494 ComplexMode = true;
1495 } else if (Str[1] != 'I') {
1496 DestWidth = 0;
1497 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001498 break;
1499 case 4:
1500 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1501 // pointer on PIC16 and other embedded platforms.
1502 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001503 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001504 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001505 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001506 break;
1507 case 7:
1508 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001509 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001510 break;
1511 }
1512
1513 QualType OldTy;
1514 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1515 OldTy = TD->getUnderlyingType();
1516 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1517 OldTy = VD->getType();
1518 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001519 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1520 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001521 return;
1522 }
Eli Friedman73397492009-03-03 06:41:03 +00001523
1524 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1525 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1526 else if (IntegerMode) {
1527 if (!OldTy->isIntegralType())
1528 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1529 } else if (ComplexMode) {
1530 if (!OldTy->isComplexType())
1531 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1532 } else {
1533 if (!OldTy->isFloatingType())
1534 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1535 }
1536
Mike Stump390b4cc2009-05-16 07:39:55 +00001537 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1538 // and friends, at least with glibc.
1539 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1540 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001541 // FIXME: Make sure floating-point mappings are accurate
1542 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001543 QualType NewTy;
1544 switch (DestWidth) {
1545 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001546 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001547 return;
1548 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001549 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001550 return;
1551 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001552 if (!IntegerMode) {
1553 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1554 return;
1555 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001556 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001557 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001558 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001559 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001560 break;
1561 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001562 if (!IntegerMode) {
1563 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1564 return;
1565 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001566 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001567 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001568 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001569 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001570 break;
1571 case 32:
1572 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001573 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001574 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001575 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001576 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001577 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001578 break;
1579 case 64:
1580 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001581 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001582 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001583 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001584 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001585 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001586 break;
Eli Friedman73397492009-03-03 06:41:03 +00001587 case 96:
1588 NewTy = S.Context.LongDoubleTy;
1589 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001590 case 128:
1591 if (!IntegerMode) {
1592 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1593 return;
1594 }
1595 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001596 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001597 }
1598
Eli Friedman73397492009-03-03 06:41:03 +00001599 if (ComplexMode) {
1600 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001601 }
1602
1603 // Install the new type.
1604 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1605 TD->setUnderlyingType(NewTy);
1606 else
1607 cast<ValueDecl>(D)->setType(NewTy);
1608}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001609
Anders Carlssond87df372009-02-13 06:46:13 +00001610static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1611 // check the attribute arguments.
1612 if (Attr.getNumArgs() > 0) {
1613 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1614 return;
1615 }
Anders Carlssone896d982009-02-13 08:11:52 +00001616
Anders Carlsson5bab7882009-02-19 19:16:48 +00001617 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001618 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001619 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001620 return;
1621 }
Mike Stumpbf916502009-07-24 19:02:52 +00001622
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001623 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001624}
1625
Anders Carlsson5bab7882009-02-19 19:16:48 +00001626static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1627 // check the attribute arguments.
1628 if (Attr.getNumArgs() != 0) {
1629 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1630 return;
1631 }
Mike Stumpbf916502009-07-24 19:02:52 +00001632
Chris Lattnerc5197432009-04-14 17:02:11 +00001633 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001634 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001635 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001636 return;
1637 }
Mike Stumpbf916502009-07-24 19:02:52 +00001638
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001639 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001640}
1641
Chris Lattnercf2a7212009-04-20 19:12:28 +00001642static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001643 // check the attribute arguments.
1644 if (Attr.getNumArgs() != 0) {
1645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1646 return;
1647 }
Mike Stumpbf916502009-07-24 19:02:52 +00001648
Chris Lattnerc5197432009-04-14 17:02:11 +00001649 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1650 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001651 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001652 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001653 return;
1654 }
Mike Stumpbf916502009-07-24 19:02:52 +00001655
Chris Lattnerc5197432009-04-14 17:02:11 +00001656 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001657 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001658 return;
1659 }
Mike Stumpbf916502009-07-24 19:02:52 +00001660
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001661 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001662}
1663
Fariborz Jahanianee760332009-03-27 18:38:55 +00001664static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1665 // check the attribute arguments.
1666 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001668 return;
1669 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001670
Fariborz Jahanianee760332009-03-27 18:38:55 +00001671 if (!isFunctionOrMethod(d)) {
1672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001673 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001674 return;
1675 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001676
1677 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1678 llvm::APSInt NumParams(32);
1679 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1680 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1681 << "regparm" << NumParamsExpr->getSourceRange();
1682 return;
1683 }
1684
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001685 if (S.Context.Target.getRegParmMax() == 0) {
1686 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001687 << NumParamsExpr->getSourceRange();
1688 return;
1689 }
1690
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001691 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001692 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1693 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001694 return;
1695 }
1696
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001697 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001698}
1699
Chris Lattner0744e5f2008-06-29 00:23:49 +00001700//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001701// Checker-specific attribute handlers.
1702//===----------------------------------------------------------------------===//
1703
1704static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1705 Sema &S) {
1706
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001707 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001708
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001709 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1710 RetTy = MD->getResultType();
1711 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1712 RetTy = FD->getResultType();
1713 else {
1714 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1715 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001716 return;
1717 }
Mike Stumpbf916502009-07-24 19:02:52 +00001718
Ted Kremenek6217b802009-07-29 21:53:49 +00001719 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
Steve Naroff14108da2009-07-10 23:34:53 +00001720 || RetTy->getAsObjCObjectPointerType())) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001721 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1722 << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001723 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001724 }
Mike Stumpbf916502009-07-24 19:02:52 +00001725
Ted Kremenekb71368d2009-05-09 02:44:38 +00001726 switch (Attr.getKind()) {
1727 default:
1728 assert(0 && "invalid ownership attribute");
1729 return;
1730 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001731 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001732 return;
1733 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001734 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001735 return;
1736 };
1737}
1738
1739//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001740// Top Level Sema Entry Points
1741//===----------------------------------------------------------------------===//
1742
Sebastian Redla89d82c2008-12-21 19:24:58 +00001743/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001744/// the attribute applies to decls. If the attribute is a type attribute, just
1745/// silently ignore it.
Mike Stumpbf916502009-07-24 19:02:52 +00001746static void ProcessDeclAttribute(Scope *scope, Decl *D,
1747 const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001748 if (Attr.isDeclspecAttribute())
1749 // FIXME: Try to deal with __declspec attributes!
1750 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001751 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001752 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001753 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001754 case AttributeList::AT_objc_gc:
Mike Stumpbf916502009-07-24 19:02:52 +00001755 // Ignore these, these are type attributes, handled by
1756 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001757 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001758 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1759 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001760 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001761 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001762 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001763 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001764 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1765 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1766 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1767 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1768 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1769 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001770 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001771 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001772 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001773 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1774 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001775 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001776 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001777 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Ryan Flynn76168e22009-08-09 20:07:29 +00001778 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001779 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1780 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1781 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001782
1783 // Checker-specific.
1784 case AttributeList::AT_ns_returns_retained:
1785 case AttributeList::AT_cf_returns_retained:
1786 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1787
Nate Begeman6f3d8382009-06-26 06:32:41 +00001788 case AttributeList::AT_reqd_wg_size:
1789 HandleReqdWorkGroupSize(D, Attr, S); break;
1790
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001791 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001792 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001793 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001794 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001795 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001796 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001797 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001798 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001799 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1800 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001801 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001802 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001803 case AttributeList::AT_transparent_union:
1804 HandleTransparentUnionAttr(D, Attr, S);
1805 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001806 case AttributeList::AT_objc_exception:
1807 HandleObjCExceptionAttr(D, Attr, S);
1808 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001809 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001810 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001811 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001812 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001813 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1814 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001815 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001816 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001817 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001818 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001819 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001820 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001821 // Just ignore
1822 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001823 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001824 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001825 break;
1826 }
1827}
1828
1829/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1830/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001831void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001832 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001833 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001834 AttrList = AttrList->getNext();
1835 }
1836}
1837
Ryan Flynne25ff832009-07-30 03:15:39 +00001838/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1839/// #pragma weak needs a non-definition decl and source may not have one
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001840NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II)
Ryan Flynne25ff832009-07-30 03:15:39 +00001841{
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001842 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00001843 NamedDecl *NewD = 0;
1844 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1845 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1846 FD->getLocation(), DeclarationName(II),
1847 FD->getType());
1848 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1849 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1850 VD->getLocation(), II,
1851 VD->getType(), VD->getStorageClass());
1852 }
1853 return NewD;
1854}
1855
1856/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1857/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001858void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001859 if (!W.getUsed()) { // only do this once
1860 W.setUsed(true);
1861 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1862 IdentifierInfo *NDId = ND->getIdentifier();
1863 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1864 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
1865 NewD->addAttr(::new (Context) WeakAttr());
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001866 WeakTopLevelDecl.push_back(NewD);
1867 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1868 // to insert Decl at TU scope, sorry.
1869 DeclContext *SavedContext = CurContext;
1870 CurContext = Context.getTranslationUnitDecl();
1871 PushOnScopeChains(NewD, S);
1872 CurContext = SavedContext;
Ryan Flynne25ff832009-07-30 03:15:39 +00001873 } else { // just add weak to existing
1874 ND->addAttr(::new (Context) WeakAttr());
1875 }
1876 }
1877}
1878
Chris Lattner0744e5f2008-06-29 00:23:49 +00001879/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1880/// it, apply them to D. This is a bit tricky because PD can have attributes
1881/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001882void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00001883 // Handle #pragma weak
1884 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
1885 if (ND->hasLinkage()) {
1886 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
1887 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00001888 // Identifier referenced by #pragma weak before it was declared
1889 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00001890 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
1891 }
1892 }
1893 }
1894
Chris Lattner0744e5f2008-06-29 00:23:49 +00001895 // Apply decl attributes from the DeclSpec if present.
1896 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001897 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001898
Chris Lattner0744e5f2008-06-29 00:23:49 +00001899 // Walk the declarator structure, applying decl attributes that were in a type
1900 // position to the decl itself. This handles cases like:
1901 // int *__attr__(x)** D;
1902 // when X is a decl attribute.
1903 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1904 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001905 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00001906
Chris Lattner0744e5f2008-06-29 00:23:49 +00001907 // Finally, apply any attributes on the decl itself.
1908 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001909 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001910}