blob: 290c58ea8a5121ba191dc47f5826fc95669e24d0 [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
Daniel Dunbard3f2c102008-10-19 02:04:16 +000027static const FunctionType *getFunctionType(Decl *d) {
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();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000040
41 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000042}
43
Daniel Dunbar35682492008-09-26 04:12:28 +000044// FIXME: We should provide an abstraction around a method or function
45// to provide the following bits of information.
46
Daniel Dunbard3f2c102008-10-19 02:04:16 +000047/// isFunctionOrMethod - Return true if the given decl has function
48/// type (function or function-typed variable) or an Objective-C
49/// method.
Daniel Dunbar35682492008-09-26 04:12:28 +000050static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000051 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000052}
53
Daniel Dunbard3f2c102008-10-19 02:04:16 +000054/// hasFunctionProto - Return true if the given decl has a argument
55/// information. This decl should have already passed
56/// isFunctionOrMethod.
57static bool hasFunctionProto(Decl *d) {
58 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +000059 return isa<FunctionProtoType>(FnTy);
Daniel Dunbard3f2c102008-10-19 02:04:16 +000060 } else {
61 assert(isa<ObjCMethodDecl>(d));
62 return true;
63 }
64}
65
66/// getFunctionOrMethodNumArgs - Return number of function or method
67/// arguments. It is an error to call this on a K&R function (use
68/// hasFunctionProto first).
Daniel Dunbar35682492008-09-26 04:12:28 +000069static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000070 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000071 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chris Lattner89951a82009-02-20 18:43:26 +000072 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000073}
74
75static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +000076 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000077 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chris Lattner89951a82009-02-20 18:43:26 +000078
79 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +000080}
81
82static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000083 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +000084 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000085 return proto->isVariadic();
86 } else {
87 return cast<ObjCMethodDecl>(d)->isVariadic();
88 }
89}
90
Chris Lattner6b6b5372008-06-26 18:38:35 +000091static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000092 const PointerType *PT = T->getAsPointerType();
93 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000094 return false;
95
Chris Lattnerb77792e2008-07-26 22:17:49 +000096 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000097 if (!ClsT)
98 return false;
99
100 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
101
102 // FIXME: Should we walk the chain of classes?
103 return ClsName == &Ctx.Idents.get("NSString") ||
104 ClsName == &Ctx.Idents.get("NSMutableString");
105}
106
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000107static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
108 const PointerType *PT = T->getAsPointerType();
109 if (!PT)
110 return false;
111
112 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
113 if (!RT)
114 return false;
115
116 const RecordDecl *RD = RT->getDecl();
117 if (RD->getTagKind() != TagDecl::TK_struct)
118 return false;
119
120 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
121}
122
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000123//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000124// Attribute Implementations
125//===----------------------------------------------------------------------===//
126
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000127// FIXME: All this manual attribute parsing code is gross. At the
128// least add some helper functions to check most argument patterns (#
129// and types of args).
130
Chris Lattner803d0802008-06-29 00:43:07 +0000131static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
132 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000133 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
134 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000135 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000136 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000137 }
138
Chris Lattner6b6b5372008-06-26 18:38:35 +0000139 QualType curType = tDecl->getUnderlyingType();
140 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000141 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000142 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000143 return;
144 }
Chris Lattner545dd342008-06-28 23:36:30 +0000145 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000146 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000147 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
149 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000150 return;
151 }
152 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
153 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000154 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000155 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000156 return;
157 }
158 // unlike gcc's vector_size attribute, the size is specified as the
159 // number of elements, not the number of bytes.
160 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
161
162 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000163 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
164 << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165 return;
166 }
167 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000168 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000169 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000170 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000171}
172
Chris Lattner065c5a82008-06-28 23:48:25 +0000173
174/// HandleVectorSizeAttribute - this attribute is only applicable to
175/// integral and float scalars, although arrays, pointers, and function
176/// return values are allowed in conjunction with this construct. Aggregates
177/// with this attribute are invalid, even if they are of the same size as a
178/// corresponding scalar.
179/// The raw attribute should contain precisely 1 argument, the vector size
180/// for the variable, measured in bytes. If curType and rawAttr are well
181/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000182static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000183 QualType CurType;
184 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
185 CurType = VD->getType();
186 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
187 CurType = TD->getUnderlyingType();
188 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000189 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
190 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000191 return;
192 }
193
194 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000195 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000196 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000197 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000198 }
Chris Lattner545dd342008-06-28 23:36:30 +0000199 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000200 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000201 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000202 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
203 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000204 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 }
206 // navigate to the base type - we need to provide for vector pointers,
207 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000208 if (CurType->isPointerType() || CurType->isArrayType() ||
209 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 assert(0 && "HandleVector(): Complex type construction unimplemented");
211 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
212 do {
213 if (PointerType *PT = dyn_cast<PointerType>(canonType))
214 canonType = PT->getPointeeType().getTypePtr();
215 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
216 canonType = AT->getElementType().getTypePtr();
217 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
218 canonType = FT->getResultType().getTypePtr();
219 } while (canonType->isPointerType() || canonType->isArrayType() ||
220 canonType->isFunctionType());
221 */
222 }
223 // the base type must be integer or float.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000224 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000226 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000227 }
Chris Lattner803d0802008-06-29 00:43:07 +0000228 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000229 // vecSize is specified in bytes - convert to bits.
230 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
231
232 // the vector size needs to be an integral multiple of the type size.
233 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
235 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000236 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000237 }
238 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
240 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000241 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000242 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000243
244 // Success! Instantiate the vector type, the number of elements is > 0, and
245 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000246 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000247
248 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
249 VD->setType(CurType);
250 else
251 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000252}
253
Chris Lattner803d0802008-06-29 00:43:07 +0000254static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000255 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000256 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000258 return;
259 }
260
261 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000262 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000263 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
264 // If the alignment is less than or equal to 8 bits, the packed attribute
265 // has no effect.
266 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000267 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000268 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000269 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000270 else
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000271 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000272 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000274}
275
Ted Kremenek96329d42008-07-15 22:26:48 +0000276static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
277 // check the attribute arguments.
278 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000280 return;
281 }
282
283 // The IBOutlet attribute only applies to instance variables of Objective-C
284 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000285 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000286 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000287 else
Ted Kremenek32742602009-02-17 22:20:20 +0000288 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000289}
290
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000291static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000292 // GCC ignores the nonnull attribute on K&R style function
293 // prototypes, so we ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000294 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000295 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000296 << "nonnull" << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000297 return;
298 }
299
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000300 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000301
302 // The nonnull attribute only applies to pointers.
303 llvm::SmallVector<unsigned, 10> NonNullArgs;
304
305 for (AttributeList::arg_iterator I=Attr.arg_begin(),
306 E=Attr.arg_end(); I!=E; ++I) {
307
308
309 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000310 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000311 llvm::APSInt ArgNum(32);
312 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
314 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000315 return;
316 }
317
318 unsigned x = (unsigned) ArgNum.getZExtValue();
319
320 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000322 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000323 return;
324 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000325
326 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000327
328 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000329 QualType T = getFunctionOrMethodArgType(d, x);
330 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000331 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000332 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
333 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000334 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000335 }
336
337 NonNullArgs.push_back(x);
338 }
339
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000340 // If no arguments were specified to __attribute__((nonnull)) then all
341 // pointer arguments have a nonnull attribute.
342 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000343 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
344 QualType T = getFunctionOrMethodArgType(d, I);
345 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000346 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000347 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000348
349 if (NonNullArgs.empty()) {
350 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
351 return;
352 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000353 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000354
355 unsigned* start = &NonNullArgs[0];
356 unsigned size = NonNullArgs.size();
357 std::sort(start, start + size);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000358 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000359}
360
Chris Lattner803d0802008-06-29 00:43:07 +0000361static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000362 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000363 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000364 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000365 return;
366 }
367
Chris Lattner545dd342008-06-28 23:36:30 +0000368 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000369 Arg = Arg->IgnoreParenCasts();
370 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
371
372 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000373 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000374 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000375 return;
376 }
377
378 const char *Alias = Str->getStrData();
379 unsigned AliasLen = Str->getByteLength();
380
381 // FIXME: check if target symbol exists in current file
382
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000383 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000384}
385
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000386static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
387 Sema &S) {
388 // check the attribute arguments.
389 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000390 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000391 return;
392 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000393
Chris Lattnerc5197432009-04-14 17:02:11 +0000394 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000395 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
396 << "always_inline" << 0 /*function*/;
397 return;
398 }
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000399
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000400 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000401}
402
Ted Kremenekb7252322009-04-10 00:01:14 +0000403static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
404 Sema &S, const char *attrName) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000405 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000406 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000407 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000408 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000409 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000410
411 if (!isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000412 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenekb7252322009-04-10 00:01:14 +0000413 << attrName << 0 /*function*/;
414 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000415 }
416
Ted Kremenekb7252322009-04-10 00:01:14 +0000417 return true;
418}
419
420static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
421 if (HandleCommonNoReturnAttr(d, Attr, S, "noreturn"))
422 d->addAttr(::new (S.Context) NoReturnAttr());
423}
424
425static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
426 Sema &S) {
427 if (HandleCommonNoReturnAttr(d, Attr, S, "analyzer_noreturn"))
428 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000429}
430
Ted Kremenek73798892008-07-25 04:39:19 +0000431static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
432 // check the attribute arguments.
433 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000435 return;
436 }
437
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000438 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000440 << "unused" << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000441 return;
442 }
443
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000444 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000445}
446
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000447static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
448 // check the attribute arguments.
449 if (Attr.getNumArgs() != 0) {
450 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
451 return;
452 }
453
454 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000455 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000456 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
457 return;
458 }
459 } else if (!isFunctionOrMethod(d)) {
460 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000461 << "used" << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000462 return;
463 }
464
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000465 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000466}
467
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000468static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
469 // check the attribute arguments.
470 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000471 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
472 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000473 return;
474 }
475
476 int priority = 65535; // FIXME: Do not hardcode such constants.
477 if (Attr.getNumArgs() > 0) {
478 Expr *E = static_cast<Expr *>(Attr.getArg(0));
479 llvm::APSInt Idx(32);
480 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000481 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000482 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000483 return;
484 }
485 priority = Idx.getZExtValue();
486 }
487
Chris Lattnerc5197432009-04-14 17:02:11 +0000488 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000489 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000490 << "constructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000491 return;
492 }
493
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000494 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000495}
496
497static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
498 // check the attribute arguments.
499 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000500 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
501 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000502 return;
503 }
504
505 int priority = 65535; // FIXME: Do not hardcode such constants.
506 if (Attr.getNumArgs() > 0) {
507 Expr *E = static_cast<Expr *>(Attr.getArg(0));
508 llvm::APSInt Idx(32);
509 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000510 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000511 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000512 return;
513 }
514 priority = Idx.getZExtValue();
515 }
516
Anders Carlsson6782fc62008-08-22 22:10:48 +0000517 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000518 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000519 << "destructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000520 return;
521 }
522
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000523 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000524}
525
Chris Lattner803d0802008-06-29 00:43:07 +0000526static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000527 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000528 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000529 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000530 return;
531 }
532
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000533 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000534}
535
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000536static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
537 // check the attribute arguments.
538 if (Attr.getNumArgs() != 0) {
539 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
540 return;
541 }
542
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000543 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000544}
545
Chris Lattner803d0802008-06-29 00:43:07 +0000546static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000547 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000548 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000549 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000550 return;
551 }
552
Chris Lattner545dd342008-06-28 23:36:30 +0000553 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000554 Arg = Arg->IgnoreParenCasts();
555 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
556
557 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000558 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000559 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000560 return;
561 }
562
563 const char *TypeStr = Str->getStrData();
564 unsigned TypeLen = Str->getByteLength();
565 VisibilityAttr::VisibilityTypes type;
566
567 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
568 type = VisibilityAttr::DefaultVisibility;
569 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
570 type = VisibilityAttr::HiddenVisibility;
571 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
572 type = VisibilityAttr::HiddenVisibility; // FIXME
573 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
574 type = VisibilityAttr::ProtectedVisibility;
575 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000576 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000577 return;
578 }
579
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000580 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000581}
582
Chris Lattner0db29ec2009-02-14 08:09:34 +0000583static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
584 Sema &S) {
585 if (Attr.getNumArgs() != 0) {
586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
587 return;
588 }
589
590 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
591 if (OCI == 0) {
592 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
593 return;
594 }
595
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000596 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000597}
598
599static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000600 if (Attr.getNumArgs() != 0) {
601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
602 return;
603 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000604 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000605 QualType T = TD->getUnderlyingType();
606 if (!T->isPointerType() ||
607 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
608 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
609 return;
610 }
611 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000612 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000613}
614
Douglas Gregorf9201e02009-02-11 23:02:49 +0000615static void
616HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
617 if (Attr.getNumArgs() != 0) {
618 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
619 return;
620 }
621
622 if (!isa<FunctionDecl>(D)) {
623 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
624 return;
625 }
626
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000627 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000628}
629
Steve Naroff9eae5762008-09-18 16:44:58 +0000630static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
631 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000632 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000633 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000634 return;
635 }
636
637 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000639 return;
640 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000641
642 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000643 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000644 type = BlocksAttr::ByRef;
645 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000646 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000647 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000648 return;
649 }
650
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000651 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000652}
653
Anders Carlsson77091822008-10-05 18:05:59 +0000654static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
655 // check the attribute arguments.
656 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
658 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000659 return;
660 }
661
662 int sentinel = 0;
663 if (Attr.getNumArgs() > 0) {
664 Expr *E = static_cast<Expr *>(Attr.getArg(0));
665 llvm::APSInt Idx(32);
666 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000667 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000668 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000669 return;
670 }
671 sentinel = Idx.getZExtValue();
672
673 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000674 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
675 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000676 return;
677 }
678 }
679
680 int nullPos = 0;
681 if (Attr.getNumArgs() > 1) {
682 Expr *E = static_cast<Expr *>(Attr.getArg(1));
683 llvm::APSInt Idx(32);
684 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000685 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000686 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000687 return;
688 }
689 nullPos = Idx.getZExtValue();
690
691 if (nullPos > 1 || nullPos < 0) {
692 // FIXME: This error message could be improved, it would be nice
693 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000694 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
695 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000696 return;
697 }
698 }
699
700 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000701 const FunctionType *FT = FD->getType()->getAsFunctionType();
702 assert(FT && "FunctionDecl has non-function type?");
703
704 if (isa<FunctionNoProtoType>(FT)) {
705 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
706 return;
707 }
708
709 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Anders Carlsson77091822008-10-05 18:05:59 +0000710 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
711 return;
712 }
713 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
714 if (!MD->isVariadic()) {
715 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
716 return;
717 }
718 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000720 << "sentinel" << 3 /*function or method*/;
Anders Carlsson77091822008-10-05 18:05:59 +0000721 return;
722 }
723
724 // FIXME: Actually create the attribute.
725}
726
Chris Lattner026dc962009-02-14 07:37:35 +0000727static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
728 // check the attribute arguments.
729 if (Attr.getNumArgs() != 0) {
730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
731 return;
732 }
733
734 // TODO: could also be applied to methods?
735 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
736 if (!Fn) {
737 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
738 << "warn_unused_result" << 0 /*function*/;
739 return;
740 }
741
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000742 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000743}
744
745static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000746 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000747 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000748 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000749 return;
750 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000751
752 // TODO: could also be applied to methods?
753 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
754 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
755 << "weak" << 2 /*variable and function*/;
756 return;
757 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000758
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000759 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000760}
761
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000762static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
763 // check the attribute arguments.
764 if (Attr.getNumArgs() != 0) {
765 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
766 return;
767 }
768
769 // weak_import only applies to variable & function declarations.
770 bool isDef = false;
771 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
772 isDef = (!VD->hasExternalStorage() || VD->getInit());
773 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor72971342009-04-18 00:02:19 +0000774 isDef = FD->getBody(S.Context);
Mike Stump862a2c52009-03-18 15:05:17 +0000775 } else if (isa<ObjCPropertyDecl>(D)) {
776 // We ignore weak import on properties
Mike Stump1c90f4d2009-03-18 17:39:31 +0000777 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000778 } else {
779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
780 << "weak_import" << 2 /*variable and function*/;
781 return;
782 }
783
784 // Merge should handle any subsequent violations.
785 if (isDef) {
786 S.Diag(Attr.getLoc(),
787 diag::warn_attribute_weak_import_invalid_on_definition)
788 << "weak_import" << 2 /*variable and function*/;
789 return;
790 }
791
792 D->addAttr(::new (S.Context) WeakImportAttr());
793}
794
Chris Lattner026dc962009-02-14 07:37:35 +0000795static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000796 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000797 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000799 return;
800 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000801
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000802 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000803 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000804 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000805 return;
806 }
807
Chris Lattner026dc962009-02-14 07:37:35 +0000808 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000809 if (!FD) {
810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000811 << "dllimport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000812 return;
813 }
814
815 // Currently, the dllimport attribute is ignored for inlined functions.
816 // Warning is emitted.
817 if (FD->isInline()) {
818 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
819 return;
820 }
821
822 // The attribute is also overridden by a subsequent declaration as dllexport.
823 // Warning is emitted.
824 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
825 nextAttr = nextAttr->getNext()) {
826 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
827 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
828 return;
829 }
830 }
831
Chris Lattner026dc962009-02-14 07:37:35 +0000832 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000833 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
834 return;
835 }
836
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000837 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000838}
839
Chris Lattner026dc962009-02-14 07:37:35 +0000840static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000841 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000842 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000843 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000844 return;
845 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000846
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000847 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000848 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000849 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000850 return;
851 }
852
Chris Lattner026dc962009-02-14 07:37:35 +0000853 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000854 if (!FD) {
855 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000856 << "dllexport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000857 return;
858 }
859
860 // Currently, the dllexport attribute is ignored for inlined functions,
861 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
862 if (FD->isInline()) {
863 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
864 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
865 return;
866 }
867
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000868 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000869}
870
Chris Lattner026dc962009-02-14 07:37:35 +0000871static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000872 // Attribute has no arguments.
873 if (Attr.getNumArgs() != 1) {
874 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
875 return;
876 }
877
878 // Make sure that there is a string literal as the sections's single
879 // argument.
880 StringLiteral *SE =
881 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
882 if (!SE) {
883 // FIXME
884 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
885 return;
886 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000887 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
888 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000889}
890
Chris Lattner803d0802008-06-29 00:43:07 +0000891static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000892 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000893 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000894 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000895 return;
896 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000897
898 // Attribute can be applied only to functions.
899 if (!isa<FunctionDecl>(d)) {
900 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000901 << "stdcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000902 return;
903 }
904
905 // stdcall and fastcall attributes are mutually incompatible.
906 if (d->getAttr<FastCallAttr>()) {
907 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
908 << "stdcall" << "fastcall";
909 return;
910 }
911
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000912 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000913}
914
Chris Lattner803d0802008-06-29 00:43:07 +0000915static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000916 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000917 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000919 return;
920 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000921
922 if (!isa<FunctionDecl>(d)) {
923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000924 << "fastcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000925 return;
926 }
927
928 // stdcall and fastcall attributes are mutually incompatible.
929 if (d->getAttr<StdCallAttr>()) {
930 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
931 << "fastcall" << "stdcall";
932 return;
933 }
934
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000935 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000936}
937
Chris Lattner803d0802008-06-29 00:43:07 +0000938static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000939 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000940 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000942 return;
943 }
944
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000945 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000946}
947
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000948static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
949 // check the attribute arguments.
950 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000951 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000952 return;
953 }
954
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000955 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000956}
957
958static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
959 // check the attribute arguments.
960 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000961 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000962 return;
963 }
964
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000965 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000966}
967
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000968static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000969 // Match gcc which ignores cleanup attrs when compiling C++.
970 if (S.getLangOptions().CPlusPlus)
971 return;
972
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000973 if (!Attr.getParameterName()) {
974 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
975 return;
976 }
977
978 if (Attr.getNumArgs() != 0) {
979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
980 return;
981 }
982
983 VarDecl *VD = dyn_cast<VarDecl>(d);
984
985 if (!VD || !VD->hasLocalStorage()) {
986 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
987 return;
988 }
989
990 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000991 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
992 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000993 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000994 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000995 Attr.getParameterName();
996 return;
997 }
998
999 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1000 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001001 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001002 Attr.getParameterName();
1003 return;
1004 }
1005
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001006 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001007 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001008 Attr.getParameterName();
1009 return;
1010 }
1011
Anders Carlsson89941c12009-02-07 23:16:50 +00001012 // We're currently more strict than GCC about what function types we accept.
1013 // If this ever proves to be a problem it should be easy to fix.
1014 QualType Ty = S.Context.getPointerType(VD->getType());
1015 QualType ParamTy = FD->getParamDecl(0)->getType();
Anders Carlssonb90052a2009-02-25 17:19:08 +00001016 if (S.CheckAssignmentConstraints(Ty, ParamTy) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001017 S.Diag(Attr.getLoc(),
1018 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1019 Attr.getParameterName() << ParamTy << Ty;
1020 return;
1021 }
1022
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001023 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001024}
1025
Chris Lattner6b6b5372008-06-26 18:38:35 +00001026/// Handle __attribute__((format(type,idx,firstarg))) attributes
1027/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001028static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001029
Chris Lattner545dd342008-06-28 23:36:30 +00001030 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001031 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001032 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001033 return;
1034 }
1035
Chris Lattner545dd342008-06-28 23:36:30 +00001036 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001037 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001038 return;
1039 }
1040
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001041 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001042 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001043 << "format" << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001044 return;
1045 }
1046
1047 // FIXME: in C++ the implicit 'this' function parameter also counts.
1048 // this is needed in order to be compatible with GCC
1049 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001050 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001051 unsigned FirstIdx = 1;
1052
Chris Lattner545dd342008-06-28 23:36:30 +00001053 const char *Format = Attr.getParameterName()->getName();
1054 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001055
1056 // Normalize the argument, __foo__ becomes foo.
1057 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1058 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1059 Format += 2;
1060 FormatLen -= 4;
1061 }
1062
1063 bool Supported = false;
1064 bool is_NSString = false;
1065 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001066 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001067
1068 switch (FormatLen) {
1069 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001070 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1071 case 6: Supported = !memcmp(Format, "printf", 6); break;
1072 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001073 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001074 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1075 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1076 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001077 break;
1078 }
1079
1080 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001081 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1082 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001083 return;
1084 }
1085
1086 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001087 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001088 llvm::APSInt Idx(32);
1089 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001090 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001091 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001092 return;
1093 }
1094
1095 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001096 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001097 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001098 return;
1099 }
1100
1101 // FIXME: Do we need to bounds check?
1102 unsigned ArgIdx = Idx.getZExtValue() - 1;
1103
1104 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001105 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001106
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001107 if (is_CFString) {
1108 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001109 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1110 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001111 return;
1112 }
1113 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001114 // FIXME: do we need to check if the type is NSString*? What are
1115 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001116 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001117 // FIXME: Should highlight the actual expression that has the
1118 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001119 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1120 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001121 return;
1122 }
1123 } else if (!Ty->isPointerType() ||
1124 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1125 // FIXME: Should highlight the actual expression that has the
1126 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001127 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1128 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001129 return;
1130 }
1131
1132 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001133 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001134 llvm::APSInt FirstArg(32);
1135 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001136 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001137 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001138 return;
1139 }
1140
1141 // check if the function is variadic if the 3rd argument non-zero
1142 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001143 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001144 ++NumArgs; // +1 for ...
1145 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001146 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001147 return;
1148 }
1149 }
1150
Chris Lattner3c73c412008-11-19 08:23:25 +00001151 // strftime requires FirstArg to be 0 because it doesn't read from any
1152 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001153 if (is_strftime) {
1154 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001155 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1156 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001157 return;
1158 }
1159 // if 0 it disables parameter checking (to use with e.g. va_list)
1160 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001162 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001163 return;
1164 }
1165
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001166 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001167 Idx.getZExtValue(), FirstArg.getZExtValue()));
1168}
1169
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001170static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1171 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001172 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001173 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001174 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001175 return;
1176 }
1177
Eli Friedmanbc887452008-09-02 05:19:23 +00001178 // FIXME: This shouldn't be restricted to typedefs
1179 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1180 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001181 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001182 << "transparent_union" << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001183 return;
1184 }
1185
Eli Friedmanbc887452008-09-02 05:19:23 +00001186 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001187
Eli Friedmanbc887452008-09-02 05:19:23 +00001188 // FIXME: Should we do a check for RD->isDefinition()?
1189
1190 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
1191 // we might silently generate incorrect code; see following code
Douglas Gregor6ab35242009-04-09 21:40:53 +00001192 for (RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1193 FieldEnd = RD->field_end(S.Context);
Douglas Gregor44b43212008-12-11 16:49:14 +00001194 Field != FieldEnd; ++Field) {
1195 if (!Field->getType()->isPointerType()) {
Eli Friedmanbc887452008-09-02 05:19:23 +00001196 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
1197 return;
1198 }
1199 }
1200
1201 // FIXME: This is a complete hack; we should be properly propagating
1202 // transparent_union through Sema. That said, this is close enough to
1203 // correctly compile all the common cases of transparent_union without
1204 // errors or warnings
1205 QualType NewTy = S.Context.VoidPtrTy;
1206 NewTy.addConst();
1207 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001208}
1209
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001210static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001211 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001212 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001214 return;
1215 }
Chris Lattner545dd342008-06-28 23:36:30 +00001216 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001217 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1218
1219 // Make sure that there is a string literal as the annotation's single
1220 // argument.
1221 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001222 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001223 return;
1224 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001225 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1226 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001227}
1228
Chris Lattner803d0802008-06-29 00:43:07 +00001229static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001230 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001231 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001233 return;
1234 }
1235
1236 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001237 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001238 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001239 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001240 Align = 128;
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001241 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001242 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001243 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001244
1245 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1246 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001247 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001248 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1249 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001250 return;
1251 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001252 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1253 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1254 << alignmentExpr->getSourceRange();
1255 return;
1256 }
1257
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001258 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001259}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001260
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001261/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001262/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001263///
1264/// Despite what would be logical, the mode attribute is a decl attribute,
1265/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1266/// 'G' be HImode, not an intermediate pointer.
1267///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001268static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001269 // This attribute isn't documented, but glibc uses it. It changes
1270 // the width of an int or unsigned int to the specified size.
1271
1272 // Check that there aren't any arguments
1273 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001275 return;
1276 }
1277
1278 IdentifierInfo *Name = Attr.getParameterName();
1279 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001280 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001281 return;
1282 }
1283 const char *Str = Name->getName();
1284 unsigned Len = Name->getLength();
1285
1286 // Normalize the attribute name, __foo__ becomes foo.
1287 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1288 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1289 Str += 2;
1290 Len -= 4;
1291 }
1292
1293 unsigned DestWidth = 0;
1294 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001295 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001296 switch (Len) {
1297 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001298 switch (Str[0]) {
1299 case 'Q': DestWidth = 8; break;
1300 case 'H': DestWidth = 16; break;
1301 case 'S': DestWidth = 32; break;
1302 case 'D': DestWidth = 64; break;
1303 case 'X': DestWidth = 96; break;
1304 case 'T': DestWidth = 128; break;
1305 }
1306 if (Str[1] == 'F') {
1307 IntegerMode = false;
1308 } else if (Str[1] == 'C') {
1309 IntegerMode = false;
1310 ComplexMode = true;
1311 } else if (Str[1] != 'I') {
1312 DestWidth = 0;
1313 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001314 break;
1315 case 4:
1316 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1317 // pointer on PIC16 and other embedded platforms.
1318 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001319 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001320 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001321 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001322 break;
1323 case 7:
1324 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001325 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001326 break;
1327 }
1328
1329 QualType OldTy;
1330 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1331 OldTy = TD->getUnderlyingType();
1332 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1333 OldTy = VD->getType();
1334 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001335 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1336 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001337 return;
1338 }
Eli Friedman73397492009-03-03 06:41:03 +00001339
1340 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1341 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1342 else if (IntegerMode) {
1343 if (!OldTy->isIntegralType())
1344 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1345 } else if (ComplexMode) {
1346 if (!OldTy->isComplexType())
1347 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1348 } else {
1349 if (!OldTy->isFloatingType())
1350 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1351 }
1352
Eli Friedmanf98aba32009-02-13 02:31:07 +00001353 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1354 // int8_t and friends, at least with glibc.
1355 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1356 // the wrong width on unusual platforms.
1357 // FIXME: Make sure floating-point mappings are accurate
1358 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001359 QualType NewTy;
1360 switch (DestWidth) {
1361 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001362 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001363 return;
1364 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001365 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001366 return;
1367 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001368 if (!IntegerMode) {
1369 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1370 return;
1371 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001372 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001373 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001374 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001375 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001376 break;
1377 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001378 if (!IntegerMode) {
1379 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1380 return;
1381 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001382 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001383 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001384 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001385 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001386 break;
1387 case 32:
1388 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001389 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001390 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001391 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001392 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001393 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001394 break;
1395 case 64:
1396 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001397 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001398 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001399 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001400 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001401 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001402 break;
Eli Friedman73397492009-03-03 06:41:03 +00001403 case 96:
1404 NewTy = S.Context.LongDoubleTy;
1405 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001406 case 128:
1407 if (!IntegerMode) {
1408 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1409 return;
1410 }
1411 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001412 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001413 }
1414
Eli Friedman73397492009-03-03 06:41:03 +00001415 if (ComplexMode) {
1416 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001417 }
1418
1419 // Install the new type.
1420 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1421 TD->setUnderlyingType(NewTy);
1422 else
1423 cast<ValueDecl>(D)->setType(NewTy);
1424}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001425
Anders Carlssond87df372009-02-13 06:46:13 +00001426static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1427 // check the attribute arguments.
1428 if (Attr.getNumArgs() > 0) {
1429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1430 return;
1431 }
Anders Carlssone896d982009-02-13 08:11:52 +00001432
Anders Carlsson5bab7882009-02-19 19:16:48 +00001433 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001434 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001435 << "nodebug" << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001436 return;
1437 }
1438
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001439 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001440}
1441
Anders Carlsson5bab7882009-02-19 19:16:48 +00001442static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1443 // check the attribute arguments.
1444 if (Attr.getNumArgs() != 0) {
1445 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1446 return;
1447 }
1448
Chris Lattnerc5197432009-04-14 17:02:11 +00001449 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001450 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1451 << "noinline" << 0 /*function*/;
1452 return;
1453 }
1454
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001455 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001456}
1457
Chris Lattnercf2a7212009-04-20 19:12:28 +00001458static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001459 // check the attribute arguments.
1460 if (Attr.getNumArgs() != 0) {
1461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1462 return;
1463 }
1464
Chris Lattnerc5197432009-04-14 17:02:11 +00001465 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1466 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001467 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnercf2a7212009-04-20 19:12:28 +00001468 << "gnu_inline" << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001469 return;
1470 }
1471
Chris Lattnerc5197432009-04-14 17:02:11 +00001472 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001473 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001474 return;
1475 }
1476
1477 if (Fn->getStorageClass() == FunctionDecl::Extern) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001478 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_extern_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001479 return;
1480 }
1481
Chris Lattnercf2a7212009-04-20 19:12:28 +00001482 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001483}
1484
Fariborz Jahanianee760332009-03-27 18:38:55 +00001485static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1486 // check the attribute arguments.
1487 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001489 return;
1490 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001491
Fariborz Jahanianee760332009-03-27 18:38:55 +00001492 if (!isFunctionOrMethod(d)) {
1493 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1494 << "regparm" << 0 /*function*/;
1495 return;
1496 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001497
1498 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1499 llvm::APSInt NumParams(32);
1500 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1501 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1502 << "regparm" << NumParamsExpr->getSourceRange();
1503 return;
1504 }
1505
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001506 if (S.Context.Target.getRegParmMax() == 0) {
1507 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001508 << NumParamsExpr->getSourceRange();
1509 return;
1510 }
1511
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001512 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1514 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001515 return;
1516 }
1517
1518 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001519}
1520
Chris Lattner0744e5f2008-06-29 00:23:49 +00001521//===----------------------------------------------------------------------===//
1522// Top Level Sema Entry Points
1523//===----------------------------------------------------------------------===//
1524
Sebastian Redla89d82c2008-12-21 19:24:58 +00001525/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001526/// the attribute applies to decls. If the attribute is a type attribute, just
1527/// silently ignore it.
1528static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1529 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001530 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001531 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001532 case AttributeList::AT_objc_gc:
1533 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001534 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001535 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1536 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001537 case AttributeList::AT_always_inline:
1538 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001539 case AttributeList::AT_analyzer_noreturn:
1540 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001541 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1542 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1543 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1544 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1545 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1546 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001547 case AttributeList::AT_ext_vector_type:
1548 HandleExtVectorTypeAttr(D, Attr, S);
1549 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001550 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1551 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001552 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001553 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001554 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1555 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1556 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1557 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001558 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001559 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001560 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001561 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001562 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001563 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001564 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001565 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1566 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001567 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001568 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001569 case AttributeList::AT_transparent_union:
1570 HandleTransparentUnionAttr(D, Attr, S);
1571 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001572 case AttributeList::AT_objc_exception:
1573 HandleObjCExceptionAttr(D, Attr, S);
1574 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001575 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001576 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001577 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001578 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001579 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1580 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001581 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001582 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001583 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001584 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001585 case AttributeList::IgnoredAttribute:
1586 // Just ignore
1587 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001588 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001589 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001590 break;
1591 }
1592}
1593
1594/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1595/// attribute list to the specified decl, ignoring any type attributes.
1596void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1597 while (AttrList) {
1598 ProcessDeclAttribute(D, *AttrList, *this);
1599 AttrList = AttrList->getNext();
1600 }
1601}
1602
1603
Chris Lattner0744e5f2008-06-29 00:23:49 +00001604/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1605/// it, apply them to D. This is a bit tricky because PD can have attributes
1606/// specified in many different places, and we need to find and apply them all.
1607void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1608 // Apply decl attributes from the DeclSpec if present.
1609 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1610 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001611
Chris Lattner0744e5f2008-06-29 00:23:49 +00001612 // Walk the declarator structure, applying decl attributes that were in a type
1613 // position to the decl itself. This handles cases like:
1614 // int *__attr__(x)** D;
1615 // when X is a decl attribute.
1616 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1617 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1618 ProcessDeclAttributeList(D, Attrs);
1619
1620 // Finally, apply any attributes on the decl itself.
1621 if (const AttributeList *Attrs = PD.getAttributes())
1622 ProcessDeclAttributeList(D, Attrs);
1623}
1624