blob: d57630e6af7f2f5fcba1e4a90be2ccaf3f543272 [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;
37
38 if (Ty->isFunctionPointerType())
39 Ty = Ty->getAsPointerType()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000040 else if (blocksToo && Ty->isBlockPointerType())
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000041 Ty = Ty->getAsBlockPointerType()->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();
Fariborz Jahanian620d89c2009-05-15 23:15:03 +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) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000120 const PointerType *PT = T->getAsPointerType();
121 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000122 return false;
123
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;
127
128 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
129
130 // 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) {
136 const PointerType *PT = T->getAsPointerType();
137 if (!PT)
138 return false;
139
140 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
141 if (!RT)
142 return false;
143
144 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
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000159static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
160 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 }
166
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);
190
191 // 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
197/// HandleVectorSizeAttribute - this attribute is only applicable to
198/// integral and float scalars, although arrays, pointers, and function
199/// return values are allowed in conjunction with this construct. Aggregates
200/// with this attribute are invalid, even if they are of the same size as a
201/// corresponding scalar.
202/// The raw attribute should contain precisely 1 argument, the vector size
203/// for the variable, measured in bytes. If curType and rawAttr are well
204/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000205static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000206 QualType CurType;
207 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
208 CurType = VD->getType();
209 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
210 CurType = TD->getUnderlyingType();
211 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000212 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
213 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000214 return;
215 }
216
217 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000218 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000219 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000220 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000221 }
Chris Lattner545dd342008-06-28 23:36:30 +0000222 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000223 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000224 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
226 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000227 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000228 }
229 // navigate to the base type - we need to provide for vector pointers,
230 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000231 if (CurType->isPointerType() || CurType->isArrayType() ||
232 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000233 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
234 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000235 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
236 do {
237 if (PointerType *PT = dyn_cast<PointerType>(canonType))
238 canonType = PT->getPointeeType().getTypePtr();
239 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
240 canonType = AT->getElementType().getTypePtr();
241 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
242 canonType = FT->getResultType().getTypePtr();
243 } while (canonType->isPointerType() || canonType->isArrayType() ||
244 canonType->isFunctionType());
245 */
246 }
Chris Lattner82afa2d2009-05-13 05:13:44 +0000247 // the base type must be integer or float, and can't already be a vector.
248 if (CurType->isVectorType() ||
249 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000250 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000251 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000252 }
Chris Lattner803d0802008-06-29 00:43:07 +0000253 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000254 // vecSize is specified in bytes - convert to bits.
255 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
256
257 // the vector size needs to be an integral multiple of the type size.
258 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000259 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
260 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000261 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000262 }
263 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000264 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
265 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000266 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000267 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000268
269 // Success! Instantiate the vector type, the number of elements is > 0, and
270 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000271 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000272
273 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
274 VD->setType(CurType);
275 else
276 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000277}
278
Chris Lattner803d0802008-06-29 00:43:07 +0000279static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000280 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000281 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000282 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000283 return;
284 }
285
286 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Douglas Gregor68584ed2009-06-18 16:11:24 +0000287 TD->addAttr(S.Context, ::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000288 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
289 // If the alignment is less than or equal to 8 bits, the packed attribute
290 // has no effect.
291 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000292 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000293 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000294 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000295 else
Douglas Gregor68584ed2009-06-18 16:11:24 +0000296 FD->addAttr(S.Context, ::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000297 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000298 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000299}
300
Ted Kremenek96329d42008-07-15 22:26:48 +0000301static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
302 // check the attribute arguments.
303 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000304 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000305 return;
306 }
307
308 // The IBOutlet attribute only applies to instance variables of Objective-C
309 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000310 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Douglas Gregor68584ed2009-06-18 16:11:24 +0000311 d->addAttr(S.Context, ::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000312 else
Ted Kremenek32742602009-02-17 22:20:20 +0000313 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000314}
315
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000316static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000317 // GCC ignores the nonnull attribute on K&R style function
318 // prototypes, so we ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000319 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000320 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000321 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000322 return;
323 }
324
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000325 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000326
327 // The nonnull attribute only applies to pointers.
328 llvm::SmallVector<unsigned, 10> NonNullArgs;
329
330 for (AttributeList::arg_iterator I=Attr.arg_begin(),
331 E=Attr.arg_end(); I!=E; ++I) {
332
333
334 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000335 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336 llvm::APSInt ArgNum(32);
337 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000338 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
339 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 return;
341 }
342
343 unsigned x = (unsigned) ArgNum.getZExtValue();
344
345 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000346 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000347 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000348 return;
349 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000350
351 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000352
353 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000354 QualType T = getFunctionOrMethodArgType(d, x);
355 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000356 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000357 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
358 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000359 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000360 }
361
362 NonNullArgs.push_back(x);
363 }
364
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000365 // If no arguments were specified to __attribute__((nonnull)) then all
366 // pointer arguments have a nonnull attribute.
367 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000368 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
369 QualType T = getFunctionOrMethodArgType(d, I);
370 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000371 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000372 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000373
374 if (NonNullArgs.empty()) {
375 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
376 return;
377 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000378 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000379
380 unsigned* start = &NonNullArgs[0];
381 unsigned size = NonNullArgs.size();
382 std::sort(start, start + size);
Douglas Gregor68584ed2009-06-18 16:11:24 +0000383 d->addAttr(S.Context, ::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000384}
385
Chris Lattner803d0802008-06-29 00:43:07 +0000386static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000387 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000388 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000389 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000390 return;
391 }
392
Chris Lattner545dd342008-06-28 23:36:30 +0000393 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000394 Arg = Arg->IgnoreParenCasts();
395 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
396
397 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000398 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000399 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000400 return;
401 }
402
403 const char *Alias = Str->getStrData();
404 unsigned AliasLen = Str->getByteLength();
405
406 // FIXME: check if target symbol exists in current file
407
Douglas Gregor68584ed2009-06-18 16:11:24 +0000408 d->addAttr(S.Context, ::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000409}
410
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000411static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
412 Sema &S) {
413 // check the attribute arguments.
414 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000416 return;
417 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000418
Chris Lattnerc5197432009-04-14 17:02:11 +0000419 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000421 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000422 return;
423 }
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000424
Douglas Gregor68584ed2009-06-18 16:11:24 +0000425 d->addAttr(S.Context, ::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000426}
427
Ted Kremenekb7252322009-04-10 00:01:14 +0000428static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000429 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000430 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000431 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000433 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000434 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000435
Mike Stump19c30c02009-04-29 19:03:13 +0000436 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
437 ValueDecl *VD = dyn_cast<ValueDecl>(d);
438 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000440 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000441 return false;
442 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000443 }
444
Ted Kremenekb7252322009-04-10 00:01:14 +0000445 return true;
446}
447
448static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000449 if (HandleCommonNoReturnAttr(d, Attr, S))
Douglas Gregor68584ed2009-06-18 16:11:24 +0000450 d->addAttr(S.Context, ::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000451}
452
453static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
454 Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000455 if (HandleCommonNoReturnAttr(d, Attr, S))
Douglas Gregor68584ed2009-06-18 16:11:24 +0000456 d->addAttr(S.Context, ::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000457}
458
Ted Kremenek73798892008-07-25 04:39:19 +0000459static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
460 // check the attribute arguments.
461 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000462 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000463 return;
464 }
465
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000466 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000467 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000468 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000469 return;
470 }
471
Douglas Gregor68584ed2009-06-18 16:11:24 +0000472 d->addAttr(S.Context, ::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000473}
474
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000475static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
476 // check the attribute arguments.
477 if (Attr.getNumArgs() != 0) {
478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
479 return;
480 }
481
482 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000483 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000484 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
485 return;
486 }
487 } else if (!isFunctionOrMethod(d)) {
488 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000489 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000490 return;
491 }
492
Douglas Gregor68584ed2009-06-18 16:11:24 +0000493 d->addAttr(S.Context, ::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000494}
495
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000496static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
497 // check the attribute arguments.
498 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000499 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
500 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000501 return;
502 }
503
504 int priority = 65535; // FIXME: Do not hardcode such constants.
505 if (Attr.getNumArgs() > 0) {
506 Expr *E = static_cast<Expr *>(Attr.getArg(0));
507 llvm::APSInt Idx(32);
508 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000509 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000510 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000511 return;
512 }
513 priority = Idx.getZExtValue();
514 }
515
Chris Lattnerc5197432009-04-14 17:02:11 +0000516 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000517 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000518 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000519 return;
520 }
521
Douglas Gregor68584ed2009-06-18 16:11:24 +0000522 d->addAttr(S.Context, ::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000523}
524
525static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
526 // check the attribute arguments.
527 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000528 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
529 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000530 return;
531 }
532
533 int priority = 65535; // FIXME: Do not hardcode such constants.
534 if (Attr.getNumArgs() > 0) {
535 Expr *E = static_cast<Expr *>(Attr.getArg(0));
536 llvm::APSInt Idx(32);
537 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000539 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000540 return;
541 }
542 priority = Idx.getZExtValue();
543 }
544
Anders Carlsson6782fc62008-08-22 22:10:48 +0000545 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000546 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000547 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000548 return;
549 }
550
Douglas Gregor68584ed2009-06-18 16:11:24 +0000551 d->addAttr(S.Context, ::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000552}
553
Chris Lattner803d0802008-06-29 00:43:07 +0000554static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000555 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000556 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000557 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000558 return;
559 }
560
Douglas Gregor68584ed2009-06-18 16:11:24 +0000561 d->addAttr(S.Context, ::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000562}
563
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000564static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
565 // check the attribute arguments.
566 if (Attr.getNumArgs() != 0) {
567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
568 return;
569 }
570
Douglas Gregor68584ed2009-06-18 16:11:24 +0000571 d->addAttr(S.Context, ::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000572}
573
Chris Lattner803d0802008-06-29 00:43:07 +0000574static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000575 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000576 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000577 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000578 return;
579 }
580
Chris Lattner545dd342008-06-28 23:36:30 +0000581 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000582 Arg = Arg->IgnoreParenCasts();
583 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
584
585 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000588 return;
589 }
590
591 const char *TypeStr = Str->getStrData();
592 unsigned TypeLen = Str->getByteLength();
593 VisibilityAttr::VisibilityTypes type;
594
595 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
596 type = VisibilityAttr::DefaultVisibility;
597 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
598 type = VisibilityAttr::HiddenVisibility;
599 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
600 type = VisibilityAttr::HiddenVisibility; // FIXME
601 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
602 type = VisibilityAttr::ProtectedVisibility;
603 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000604 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000605 return;
606 }
607
Douglas Gregor68584ed2009-06-18 16:11:24 +0000608 d->addAttr(S.Context, ::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000609}
610
Chris Lattner0db29ec2009-02-14 08:09:34 +0000611static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
612 Sema &S) {
613 if (Attr.getNumArgs() != 0) {
614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
615 return;
616 }
617
618 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
619 if (OCI == 0) {
620 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
621 return;
622 }
623
Douglas Gregor68584ed2009-06-18 16:11:24 +0000624 D->addAttr(S.Context, ::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000625}
626
627static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000628 if (Attr.getNumArgs() != 0) {
629 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
630 return;
631 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000632 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000633 QualType T = TD->getUnderlyingType();
634 if (!T->isPointerType() ||
635 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
636 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
637 return;
638 }
639 }
Douglas Gregor68584ed2009-06-18 16:11:24 +0000640 D->addAttr(S.Context, ::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000641}
642
Douglas Gregorf9201e02009-02-11 23:02:49 +0000643static void
644HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
645 if (Attr.getNumArgs() != 0) {
646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
647 return;
648 }
649
650 if (!isa<FunctionDecl>(D)) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
652 return;
653 }
654
Douglas Gregor68584ed2009-06-18 16:11:24 +0000655 D->addAttr(S.Context, ::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000656}
657
Steve Naroff9eae5762008-09-18 16:44:58 +0000658static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
659 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000660 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000661 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000662 return;
663 }
664
665 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000667 return;
668 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000669
670 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000671 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000672 type = BlocksAttr::ByRef;
673 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000674 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000675 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000676 return;
677 }
678
Douglas Gregor68584ed2009-06-18 16:11:24 +0000679 d->addAttr(S.Context, ::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000680}
681
Anders Carlsson77091822008-10-05 18:05:59 +0000682static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
683 // check the attribute arguments.
684 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
686 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000687 return;
688 }
689
690 int sentinel = 0;
691 if (Attr.getNumArgs() > 0) {
692 Expr *E = static_cast<Expr *>(Attr.getArg(0));
693 llvm::APSInt Idx(32);
694 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000696 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000697 return;
698 }
699 sentinel = Idx.getZExtValue();
700
701 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000702 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
703 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000704 return;
705 }
706 }
707
708 int nullPos = 0;
709 if (Attr.getNumArgs() > 1) {
710 Expr *E = static_cast<Expr *>(Attr.getArg(1));
711 llvm::APSInt Idx(32);
712 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000713 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000714 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000715 return;
716 }
717 nullPos = Idx.getZExtValue();
718
719 if (nullPos > 1 || nullPos < 0) {
720 // FIXME: This error message could be improved, it would be nice
721 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000722 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
723 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000724 return;
725 }
726 }
727
728 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000729 const FunctionType *FT = FD->getType()->getAsFunctionType();
730 assert(FT && "FunctionDecl has non-function type?");
731
732 if (isa<FunctionNoProtoType>(FT)) {
733 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
734 return;
735 }
736
737 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000738 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000739 return;
740 }
741 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
742 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000743 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000744 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000745 }
746 } else if (isa<BlockDecl>(d)) {
747 // Note! BlockDecl is typeless. Variadic diagnostics
748 // will be issued by the caller.
749 ;
750 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000751 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000752 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
753 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
754 : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000755 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000756 int m = Ty->isFunctionPointerType() ? 0 : 1;
757 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000758 return;
759 }
760 }
761 else {
762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000763 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000764 return;
765 }
Anders Carlsson77091822008-10-05 18:05:59 +0000766 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000768 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000769 return;
770 }
Douglas Gregor68584ed2009-06-18 16:11:24 +0000771 d->addAttr(S.Context, ::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000772}
773
Chris Lattner026dc962009-02-14 07:37:35 +0000774static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
775 // check the attribute arguments.
776 if (Attr.getNumArgs() != 0) {
777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
778 return;
779 }
780
781 // TODO: could also be applied to methods?
782 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
783 if (!Fn) {
784 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000785 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000786 return;
787 }
788
Douglas Gregor68584ed2009-06-18 16:11:24 +0000789 Fn->addAttr(S.Context, ::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000790}
791
792static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000793 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000794 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000795 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000796 return;
797 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000798
799 // TODO: could also be applied to methods?
800 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000802 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000803 return;
804 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000805
Douglas Gregor68584ed2009-06-18 16:11:24 +0000806 D->addAttr(S.Context, ::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000807}
808
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000809static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
810 // check the attribute arguments.
811 if (Attr.getNumArgs() != 0) {
812 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
813 return;
814 }
815
816 // weak_import only applies to variable & function declarations.
817 bool isDef = false;
818 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
819 isDef = (!VD->hasExternalStorage() || VD->getInit());
820 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor72971342009-04-18 00:02:19 +0000821 isDef = FD->getBody(S.Context);
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000822 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
823 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000824 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000825 } else {
826 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000827 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000828 return;
829 }
830
831 // Merge should handle any subsequent violations.
832 if (isDef) {
833 S.Diag(Attr.getLoc(),
834 diag::warn_attribute_weak_import_invalid_on_definition)
835 << "weak_import" << 2 /*variable and function*/;
836 return;
837 }
838
Douglas Gregor68584ed2009-06-18 16:11:24 +0000839 D->addAttr(S.Context, ::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000840}
841
Chris Lattner026dc962009-02-14 07:37:35 +0000842static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000843 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000844 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000845 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000846 return;
847 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000848
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000849 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000850 if (isa<VarDecl>(D)) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000851 D->addAttr(S.Context, ::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000852 return;
853 }
854
Chris Lattner026dc962009-02-14 07:37:35 +0000855 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000856 if (!FD) {
857 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000858 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000859 return;
860 }
861
862 // Currently, the dllimport attribute is ignored for inlined functions.
863 // Warning is emitted.
864 if (FD->isInline()) {
865 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
866 return;
867 }
868
869 // The attribute is also overridden by a subsequent declaration as dllexport.
870 // Warning is emitted.
871 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
872 nextAttr = nextAttr->getNext()) {
873 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
874 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
875 return;
876 }
877 }
878
Douglas Gregor68584ed2009-06-18 16:11:24 +0000879 if (D->getAttr<DLLExportAttr>(S.Context)) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
881 return;
882 }
883
Douglas Gregor68584ed2009-06-18 16:11:24 +0000884 D->addAttr(S.Context, ::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000885}
886
Chris Lattner026dc962009-02-14 07:37:35 +0000887static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000888 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000889 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000890 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000891 return;
892 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000893
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000894 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000895 if (isa<VarDecl>(D)) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000896 D->addAttr(S.Context, ::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000897 return;
898 }
899
Chris Lattner026dc962009-02-14 07:37:35 +0000900 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000901 if (!FD) {
902 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000903 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000904 return;
905 }
906
907 // Currently, the dllexport attribute is ignored for inlined functions,
908 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
909 if (FD->isInline()) {
910 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
911 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
912 return;
913 }
914
Douglas Gregor68584ed2009-06-18 16:11:24 +0000915 D->addAttr(S.Context, ::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000916}
917
Chris Lattner026dc962009-02-14 07:37:35 +0000918static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000919 // Attribute has no arguments.
920 if (Attr.getNumArgs() != 1) {
921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
922 return;
923 }
924
925 // Make sure that there is a string literal as the sections's single
926 // argument.
927 StringLiteral *SE =
928 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
929 if (!SE) {
930 // FIXME
931 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
932 return;
933 }
Douglas Gregor68584ed2009-06-18 16:11:24 +0000934 D->addAttr(S.Context,
935 ::new (S.Context) SectionAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000936 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000937}
938
Chris Lattner803d0802008-06-29 00:43:07 +0000939static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000940 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000941 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000943 return;
944 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000945
946 // Attribute can be applied only to functions.
947 if (!isa<FunctionDecl>(d)) {
948 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000949 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000950 return;
951 }
952
953 // stdcall and fastcall attributes are mutually incompatible.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000954 if (d->getAttr<FastCallAttr>(S.Context)) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000955 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
956 << "stdcall" << "fastcall";
957 return;
958 }
959
Douglas Gregor68584ed2009-06-18 16:11:24 +0000960 d->addAttr(S.Context, ::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000961}
962
Chris Lattner803d0802008-06-29 00:43:07 +0000963static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000964 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000965 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000966 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000967 return;
968 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000969
970 if (!isa<FunctionDecl>(d)) {
971 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000972 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000973 return;
974 }
975
976 // stdcall and fastcall attributes are mutually incompatible.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000977 if (d->getAttr<StdCallAttr>(S.Context)) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000978 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
979 << "fastcall" << "stdcall";
980 return;
981 }
982
Douglas Gregor68584ed2009-06-18 16:11:24 +0000983 d->addAttr(S.Context, ::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000984}
985
Chris Lattner803d0802008-06-29 00:43:07 +0000986static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000987 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000988 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000989 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000990 return;
991 }
992
Douglas Gregor68584ed2009-06-18 16:11:24 +0000993 d->addAttr(S.Context, ::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000994}
995
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000996static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
997 // check the attribute arguments.
998 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000999 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001000 return;
1001 }
1002
Douglas Gregor68584ed2009-06-18 16:11:24 +00001003 d->addAttr(S.Context, ::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001004}
1005
1006static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1007 // check the attribute arguments.
1008 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001009 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001010 return;
1011 }
1012
Douglas Gregor68584ed2009-06-18 16:11:24 +00001013 d->addAttr(S.Context, ::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001014}
1015
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001016static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001017 // Match gcc which ignores cleanup attrs when compiling C++.
1018 if (S.getLangOptions().CPlusPlus)
1019 return;
1020
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001021 if (!Attr.getParameterName()) {
1022 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1023 return;
1024 }
1025
1026 if (Attr.getNumArgs() != 0) {
1027 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1028 return;
1029 }
1030
1031 VarDecl *VD = dyn_cast<VarDecl>(d);
1032
1033 if (!VD || !VD->hasLocalStorage()) {
1034 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1035 return;
1036 }
1037
1038 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001039 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1040 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001041 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001042 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001043 Attr.getParameterName();
1044 return;
1045 }
1046
1047 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1048 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001049 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001050 Attr.getParameterName();
1051 return;
1052 }
1053
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001054 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001055 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001056 Attr.getParameterName();
1057 return;
1058 }
1059
Anders Carlsson89941c12009-02-07 23:16:50 +00001060 // We're currently more strict than GCC about what function types we accept.
1061 // If this ever proves to be a problem it should be easy to fix.
1062 QualType Ty = S.Context.getPointerType(VD->getType());
1063 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001064 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001065 S.Diag(Attr.getLoc(),
1066 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1067 Attr.getParameterName() << ParamTy << Ty;
1068 return;
1069 }
1070
Douglas Gregor68584ed2009-06-18 16:11:24 +00001071 d->addAttr(S.Context, ::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001072}
1073
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001074/// Handle __attribute__((format_arg((idx)))) attribute
1075/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1076static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1077 if (Attr.getNumArgs() != 1) {
1078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1079 return;
1080 }
1081 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1082 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1083 << Attr.getName() << 0 /*function*/;
1084 return;
1085 }
1086 // FIXME: in C++ the implicit 'this' function parameter also counts.
1087 // this is needed in order to be compatible with GCC
1088 // the index must start with 1.
1089 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1090 unsigned FirstIdx = 1;
1091 // checks for the 2nd argument
1092 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1093 llvm::APSInt Idx(32);
1094 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1095 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1096 << "format" << 2 << IdxExpr->getSourceRange();
1097 return;
1098 }
1099
1100 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1101 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1102 << "format" << 2 << IdxExpr->getSourceRange();
1103 return;
1104 }
1105
1106 unsigned ArgIdx = Idx.getZExtValue() - 1;
1107
1108 // make sure the format string is really a string
1109 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
1110
1111 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1112 if (not_nsstring_type &&
1113 !isCFStringType(Ty, S.Context) &&
1114 (!Ty->isPointerType() ||
1115 !Ty->getAsPointerType()->getPointeeType()->isCharType())) {
1116 // FIXME: Should highlight the actual expression that has the wrong type.
1117 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1118 << (not_nsstring_type ? "a string type" : "an NSString")
1119 << IdxExpr->getSourceRange();
1120 return;
1121 }
1122 Ty = getFunctionOrMethodResultType(d);
1123 if (!isNSStringType(Ty, S.Context) &&
1124 !isCFStringType(Ty, S.Context) &&
1125 (!Ty->isPointerType() ||
1126 !Ty->getAsPointerType()->getPointeeType()->isCharType())) {
1127 // FIXME: Should highlight the actual expression that has the wrong type.
1128 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
1129 << (not_nsstring_type ? "string type" : "NSString")
1130 << IdxExpr->getSourceRange();
1131 return;
1132 }
1133
Douglas Gregor68584ed2009-06-18 16:11:24 +00001134 d->addAttr(S.Context, ::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001135}
1136
Chris Lattner6b6b5372008-06-26 18:38:35 +00001137/// Handle __attribute__((format(type,idx,firstarg))) attributes
1138/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001139static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001140
Chris Lattner545dd342008-06-28 23:36:30 +00001141 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001142 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001143 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001144 return;
1145 }
1146
Chris Lattner545dd342008-06-28 23:36:30 +00001147 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001149 return;
1150 }
1151
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001152 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001153 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001154 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001155 return;
1156 }
1157
1158 // FIXME: in C++ the implicit 'this' function parameter also counts.
1159 // this is needed in order to be compatible with GCC
1160 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001161 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001162 unsigned FirstIdx = 1;
1163
Chris Lattner545dd342008-06-28 23:36:30 +00001164 const char *Format = Attr.getParameterName()->getName();
1165 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166
1167 // Normalize the argument, __foo__ becomes foo.
1168 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1169 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1170 Format += 2;
1171 FormatLen -= 4;
1172 }
1173
1174 bool Supported = false;
1175 bool is_NSString = false;
1176 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001177 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001178
1179 switch (FormatLen) {
1180 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001181 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1182 case 6: Supported = !memcmp(Format, "printf", 6); break;
1183 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001184 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001185 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1186 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1187 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188 break;
1189 }
1190
1191 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001192 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1193 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001194 return;
1195 }
1196
1197 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001198 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001199 llvm::APSInt Idx(32);
1200 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001202 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001203 return;
1204 }
1205
1206 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001208 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001209 return;
1210 }
1211
1212 // FIXME: Do we need to bounds check?
1213 unsigned ArgIdx = Idx.getZExtValue() - 1;
1214
1215 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001216 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001217
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001218 if (is_CFString) {
1219 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001220 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1221 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001222 return;
1223 }
1224 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001225 // FIXME: do we need to check if the type is NSString*? What are the
1226 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001227 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001228 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001229 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1230 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001231 return;
1232 }
1233 } else if (!Ty->isPointerType() ||
1234 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001235 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001236 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1237 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001238 return;
1239 }
1240
1241 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001242 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001243 llvm::APSInt FirstArg(32);
1244 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001246 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001247 return;
1248 }
1249
1250 // check if the function is variadic if the 3rd argument non-zero
1251 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001252 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001253 ++NumArgs; // +1 for ...
1254 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001255 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001256 return;
1257 }
1258 }
1259
Chris Lattner3c73c412008-11-19 08:23:25 +00001260 // strftime requires FirstArg to be 0 because it doesn't read from any
1261 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001262 if (is_strftime) {
1263 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001264 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1265 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001266 return;
1267 }
1268 // if 0 it disables parameter checking (to use with e.g. va_list)
1269 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001271 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001272 return;
1273 }
1274
Douglas Gregor68584ed2009-06-18 16:11:24 +00001275 d->addAttr(S.Context,
1276 ::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001277 Idx.getZExtValue(), FirstArg.getZExtValue()));
1278}
1279
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001280static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1281 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001282 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001283 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001285 return;
1286 }
1287
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001288 // Try to find the underlying union declaration.
1289 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001290 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001291 if (TD && TD->getUnderlyingType()->isUnionType())
1292 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1293 else
1294 RD = dyn_cast<RecordDecl>(d);
1295
1296 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001297 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001298 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001299 return;
1300 }
1301
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001302 if (!RD->isDefinition()) {
1303 S.Diag(Attr.getLoc(),
1304 diag::warn_transparent_union_attribute_not_definition);
1305 return;
1306 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001307
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001308 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1309 FieldEnd = RD->field_end(S.Context);
1310 if (Field == FieldEnd) {
1311 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1312 return;
1313 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001314
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001315 FieldDecl *FirstField = *Field;
1316 QualType FirstType = FirstField->getType();
1317 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1318 S.Diag(FirstField->getLocation(),
1319 diag::warn_transparent_union_attribute_floating);
1320 return;
1321 }
1322
1323 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1324 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1325 for (; Field != FieldEnd; ++Field) {
1326 QualType FieldType = Field->getType();
1327 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1328 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1329 // Warn if we drop the attribute.
1330 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1331 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1332 : S.Context.getTypeAlign(FieldType);
1333 S.Diag(Field->getLocation(),
1334 diag::warn_transparent_union_attribute_field_size_align)
1335 << isSize << Field->getDeclName() << FieldBits;
1336 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1337 S.Diag(FirstField->getLocation(),
1338 diag::note_transparent_union_first_field_size_align)
1339 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001340 return;
1341 }
1342 }
1343
Douglas Gregor68584ed2009-06-18 16:11:24 +00001344 RD->addAttr(S.Context, ::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001345}
1346
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001347static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001348 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001349 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001350 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001351 return;
1352 }
Chris Lattner545dd342008-06-28 23:36:30 +00001353 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001354 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1355
1356 // Make sure that there is a string literal as the annotation's single
1357 // argument.
1358 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001359 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001360 return;
1361 }
Douglas Gregor68584ed2009-06-18 16:11:24 +00001362 d->addAttr(S.Context,
1363 ::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001364 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001365}
1366
Chris Lattner803d0802008-06-29 00:43:07 +00001367static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001368 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001369 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001371 return;
1372 }
1373
1374 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001375 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001376 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001377 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001378 Align = 128;
Douglas Gregor68584ed2009-06-18 16:11:24 +00001379 d->addAttr(S.Context, ::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001380 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001381 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001382
1383 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1384 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001385 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1387 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001388 return;
1389 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001390 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1391 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1392 << alignmentExpr->getSourceRange();
1393 return;
1394 }
1395
Douglas Gregor68584ed2009-06-18 16:11:24 +00001396 d->addAttr(S.Context, ::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001397}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001398
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001399/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001400/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001401///
1402/// Despite what would be logical, the mode attribute is a decl attribute,
1403/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1404/// 'G' be HImode, not an intermediate pointer.
1405///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001406static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001407 // This attribute isn't documented, but glibc uses it. It changes
1408 // the width of an int or unsigned int to the specified size.
1409
1410 // Check that there aren't any arguments
1411 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001412 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001413 return;
1414 }
1415
1416 IdentifierInfo *Name = Attr.getParameterName();
1417 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001418 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001419 return;
1420 }
1421 const char *Str = Name->getName();
1422 unsigned Len = Name->getLength();
1423
1424 // Normalize the attribute name, __foo__ becomes foo.
1425 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1426 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1427 Str += 2;
1428 Len -= 4;
1429 }
1430
1431 unsigned DestWidth = 0;
1432 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001433 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001434 switch (Len) {
1435 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001436 switch (Str[0]) {
1437 case 'Q': DestWidth = 8; break;
1438 case 'H': DestWidth = 16; break;
1439 case 'S': DestWidth = 32; break;
1440 case 'D': DestWidth = 64; break;
1441 case 'X': DestWidth = 96; break;
1442 case 'T': DestWidth = 128; break;
1443 }
1444 if (Str[1] == 'F') {
1445 IntegerMode = false;
1446 } else if (Str[1] == 'C') {
1447 IntegerMode = false;
1448 ComplexMode = true;
1449 } else if (Str[1] != 'I') {
1450 DestWidth = 0;
1451 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001452 break;
1453 case 4:
1454 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1455 // pointer on PIC16 and other embedded platforms.
1456 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001457 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001458 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001459 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001460 break;
1461 case 7:
1462 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001463 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001464 break;
1465 }
1466
1467 QualType OldTy;
1468 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1469 OldTy = TD->getUnderlyingType();
1470 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1471 OldTy = VD->getType();
1472 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001473 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1474 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001475 return;
1476 }
Eli Friedman73397492009-03-03 06:41:03 +00001477
1478 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1479 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1480 else if (IntegerMode) {
1481 if (!OldTy->isIntegralType())
1482 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1483 } else if (ComplexMode) {
1484 if (!OldTy->isComplexType())
1485 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1486 } else {
1487 if (!OldTy->isFloatingType())
1488 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1489 }
1490
Mike Stump390b4cc2009-05-16 07:39:55 +00001491 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1492 // and friends, at least with glibc.
1493 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1494 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001495 // FIXME: Make sure floating-point mappings are accurate
1496 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001497 QualType NewTy;
1498 switch (DestWidth) {
1499 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001500 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001501 return;
1502 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001503 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001504 return;
1505 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001506 if (!IntegerMode) {
1507 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1508 return;
1509 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001510 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001511 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001512 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001513 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001514 break;
1515 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001516 if (!IntegerMode) {
1517 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1518 return;
1519 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001520 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001521 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001522 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001523 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001524 break;
1525 case 32:
1526 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001527 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001528 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001529 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001530 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001531 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001532 break;
1533 case 64:
1534 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001535 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001536 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001537 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001538 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001539 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001540 break;
Eli Friedman73397492009-03-03 06:41:03 +00001541 case 96:
1542 NewTy = S.Context.LongDoubleTy;
1543 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001544 case 128:
1545 if (!IntegerMode) {
1546 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1547 return;
1548 }
1549 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001550 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001551 }
1552
Eli Friedman73397492009-03-03 06:41:03 +00001553 if (ComplexMode) {
1554 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001555 }
1556
1557 // Install the new type.
1558 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1559 TD->setUnderlyingType(NewTy);
1560 else
1561 cast<ValueDecl>(D)->setType(NewTy);
1562}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001563
Anders Carlssond87df372009-02-13 06:46:13 +00001564static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1565 // check the attribute arguments.
1566 if (Attr.getNumArgs() > 0) {
1567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1568 return;
1569 }
Anders Carlssone896d982009-02-13 08:11:52 +00001570
Anders Carlsson5bab7882009-02-19 19:16:48 +00001571 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001573 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001574 return;
1575 }
1576
Douglas Gregor68584ed2009-06-18 16:11:24 +00001577 d->addAttr(S.Context, ::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001578}
1579
Anders Carlsson5bab7882009-02-19 19:16:48 +00001580static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1581 // check the attribute arguments.
1582 if (Attr.getNumArgs() != 0) {
1583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1584 return;
1585 }
1586
Chris Lattnerc5197432009-04-14 17:02:11 +00001587 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001588 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001589 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001590 return;
1591 }
1592
Douglas Gregor68584ed2009-06-18 16:11:24 +00001593 d->addAttr(S.Context, ::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001594}
1595
Chris Lattnercf2a7212009-04-20 19:12:28 +00001596static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001597 // check the attribute arguments.
1598 if (Attr.getNumArgs() != 0) {
1599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1600 return;
1601 }
1602
Chris Lattnerc5197432009-04-14 17:02:11 +00001603 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1604 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001605 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001606 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001607 return;
1608 }
1609
Chris Lattnerc5197432009-04-14 17:02:11 +00001610 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001611 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001612 return;
1613 }
1614
Douglas Gregor68584ed2009-06-18 16:11:24 +00001615 d->addAttr(S.Context, ::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001616}
1617
Fariborz Jahanianee760332009-03-27 18:38:55 +00001618static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1619 // check the attribute arguments.
1620 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001622 return;
1623 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001624
Fariborz Jahanianee760332009-03-27 18:38:55 +00001625 if (!isFunctionOrMethod(d)) {
1626 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001627 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001628 return;
1629 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001630
1631 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1632 llvm::APSInt NumParams(32);
1633 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1634 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1635 << "regparm" << NumParamsExpr->getSourceRange();
1636 return;
1637 }
1638
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001639 if (S.Context.Target.getRegParmMax() == 0) {
1640 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001641 << NumParamsExpr->getSourceRange();
1642 return;
1643 }
1644
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001645 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001646 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1647 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001648 return;
1649 }
1650
Douglas Gregor68584ed2009-06-18 16:11:24 +00001651 d->addAttr(S.Context,
1652 ::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001653}
1654
Chris Lattner0744e5f2008-06-29 00:23:49 +00001655//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001656// Checker-specific attribute handlers.
1657//===----------------------------------------------------------------------===//
1658
1659static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1660 Sema &S) {
1661
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001662 QualType RetTy;
1663
1664 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1665 RetTy = MD->getResultType();
1666 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1667 RetTy = FD->getResultType();
1668 else {
1669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1670 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001671 return;
1672 }
1673
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001674 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1675 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1676 << Attr.getName();
1677 return;
1678 }
1679
Ted Kremenekb71368d2009-05-09 02:44:38 +00001680 switch (Attr.getKind()) {
1681 default:
1682 assert(0 && "invalid ownership attribute");
1683 return;
1684 case AttributeList::AT_cf_returns_retained:
Douglas Gregor68584ed2009-06-18 16:11:24 +00001685 d->addAttr(S.Context, ::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001686 return;
1687 case AttributeList::AT_ns_returns_retained:
Douglas Gregor68584ed2009-06-18 16:11:24 +00001688 d->addAttr(S.Context, ::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001689 return;
1690 };
1691}
1692
1693//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001694// Top Level Sema Entry Points
1695//===----------------------------------------------------------------------===//
1696
Sebastian Redla89d82c2008-12-21 19:24:58 +00001697/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001698/// the attribute applies to decls. If the attribute is a type attribute, just
1699/// silently ignore it.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001700static void ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr, Sema &S) {
Eli Friedman290eeb02009-06-08 23:27:34 +00001701 if (Attr.isDeclspecAttribute())
1702 // FIXME: Try to deal with __declspec attributes!
1703 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001704 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001705 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001706 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001707 case AttributeList::AT_objc_gc:
1708 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001709 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001710 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1711 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001712 case AttributeList::AT_always_inline:
1713 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001714 case AttributeList::AT_analyzer_noreturn:
1715 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001716 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1717 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1718 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1719 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1720 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1721 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001722 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001723 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00001724 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001725 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1726 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001727 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001728 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001729 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001730 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1731 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1732 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001733
1734 // Checker-specific.
1735 case AttributeList::AT_ns_returns_retained:
1736 case AttributeList::AT_cf_returns_retained:
1737 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1738
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001739 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001740 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001741 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001742 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001743 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001744 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001745 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001746 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001747 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1748 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001749 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001750 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001751 case AttributeList::AT_transparent_union:
1752 HandleTransparentUnionAttr(D, Attr, S);
1753 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001754 case AttributeList::AT_objc_exception:
1755 HandleObjCExceptionAttr(D, Attr, S);
1756 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001757 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001758 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001759 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001760 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001761 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1762 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001763 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001764 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001765 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001766 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001767 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001768 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001769 // Just ignore
1770 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001771 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001772 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001773 break;
1774 }
1775}
1776
1777/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1778/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001779void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattner803d0802008-06-29 00:43:07 +00001780 while (AttrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001781 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattner803d0802008-06-29 00:43:07 +00001782 AttrList = AttrList->getNext();
1783 }
1784}
1785
Chris Lattner0744e5f2008-06-29 00:23:49 +00001786/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1787/// it, apply them to D. This is a bit tricky because PD can have attributes
1788/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001789void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00001790 // Apply decl attributes from the DeclSpec if present.
1791 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001792 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001793
Chris Lattner0744e5f2008-06-29 00:23:49 +00001794 // Walk the declarator structure, applying decl attributes that were in a type
1795 // position to the decl itself. This handles cases like:
1796 // int *__attr__(x)** D;
1797 // when X is a decl attribute.
1798 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1799 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001800 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001801
1802 // Finally, apply any attributes on the decl itself.
1803 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001804 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00001805}