blob: 5b611e1d18ad6ef62d71db22d149c06795be4f4f [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
394 if (!isFunctionOrMethod(d)) {
395 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
488 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
489 if (!Fn) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000490 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000491 << "constructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000492 return;
493 }
494
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000495 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000496}
497
498static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
499 // check the attribute arguments.
500 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000501 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
502 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000503 return;
504 }
505
506 int priority = 65535; // FIXME: Do not hardcode such constants.
507 if (Attr.getNumArgs() > 0) {
508 Expr *E = static_cast<Expr *>(Attr.getArg(0));
509 llvm::APSInt Idx(32);
510 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000512 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000513 return;
514 }
515 priority = Idx.getZExtValue();
516 }
517
Anders Carlsson6782fc62008-08-22 22:10:48 +0000518 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000519 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000520 << "destructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000521 return;
522 }
523
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000524 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000525}
526
Chris Lattner803d0802008-06-29 00:43:07 +0000527static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000528 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000529 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000530 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000531 return;
532 }
533
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000534 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000535}
536
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000537static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
538 // check the attribute arguments.
539 if (Attr.getNumArgs() != 0) {
540 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
541 return;
542 }
543
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000544 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000545}
546
Chris Lattner803d0802008-06-29 00:43:07 +0000547static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000548 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000549 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000550 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000551 return;
552 }
553
Chris Lattner545dd342008-06-28 23:36:30 +0000554 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000555 Arg = Arg->IgnoreParenCasts();
556 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
557
558 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000559 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000560 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000561 return;
562 }
563
564 const char *TypeStr = Str->getStrData();
565 unsigned TypeLen = Str->getByteLength();
566 VisibilityAttr::VisibilityTypes type;
567
568 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
569 type = VisibilityAttr::DefaultVisibility;
570 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
571 type = VisibilityAttr::HiddenVisibility;
572 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
573 type = VisibilityAttr::HiddenVisibility; // FIXME
574 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
575 type = VisibilityAttr::ProtectedVisibility;
576 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000577 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000578 return;
579 }
580
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000581 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000582}
583
Chris Lattner0db29ec2009-02-14 08:09:34 +0000584static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
585 Sema &S) {
586 if (Attr.getNumArgs() != 0) {
587 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
588 return;
589 }
590
591 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
592 if (OCI == 0) {
593 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
594 return;
595 }
596
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000597 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000598}
599
600static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000601 if (Attr.getNumArgs() != 0) {
602 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
603 return;
604 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000605 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000606 QualType T = TD->getUnderlyingType();
607 if (!T->isPointerType() ||
608 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
609 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
610 return;
611 }
612 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000613 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000614}
615
Douglas Gregorf9201e02009-02-11 23:02:49 +0000616static void
617HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
618 if (Attr.getNumArgs() != 0) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
620 return;
621 }
622
623 if (!isa<FunctionDecl>(D)) {
624 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
625 return;
626 }
627
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000628 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000629}
630
Steve Naroff9eae5762008-09-18 16:44:58 +0000631static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
632 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000633 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000634 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000635 return;
636 }
637
638 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000640 return;
641 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000642
643 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000644 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000645 type = BlocksAttr::ByRef;
646 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000647 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000648 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000649 return;
650 }
651
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000652 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000653}
654
Anders Carlsson77091822008-10-05 18:05:59 +0000655static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
656 // check the attribute arguments.
657 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000658 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
659 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000660 return;
661 }
662
663 int sentinel = 0;
664 if (Attr.getNumArgs() > 0) {
665 Expr *E = static_cast<Expr *>(Attr.getArg(0));
666 llvm::APSInt Idx(32);
667 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000668 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000669 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000670 return;
671 }
672 sentinel = Idx.getZExtValue();
673
674 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000675 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
676 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000677 return;
678 }
679 }
680
681 int nullPos = 0;
682 if (Attr.getNumArgs() > 1) {
683 Expr *E = static_cast<Expr *>(Attr.getArg(1));
684 llvm::APSInt Idx(32);
685 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000686 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000687 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000688 return;
689 }
690 nullPos = Idx.getZExtValue();
691
692 if (nullPos > 1 || nullPos < 0) {
693 // FIXME: This error message could be improved, it would be nice
694 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000695 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
696 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000697 return;
698 }
699 }
700
701 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000702 const FunctionType *FT = FD->getType()->getAsFunctionType();
703 assert(FT && "FunctionDecl has non-function type?");
704
705 if (isa<FunctionNoProtoType>(FT)) {
706 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
707 return;
708 }
709
710 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Anders Carlsson77091822008-10-05 18:05:59 +0000711 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
712 return;
713 }
714 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
715 if (!MD->isVariadic()) {
716 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
717 return;
718 }
719 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000720 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000721 << "sentinel" << 3 /*function or method*/;
Anders Carlsson77091822008-10-05 18:05:59 +0000722 return;
723 }
724
725 // FIXME: Actually create the attribute.
726}
727
Chris Lattner026dc962009-02-14 07:37:35 +0000728static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
729 // check the attribute arguments.
730 if (Attr.getNumArgs() != 0) {
731 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
732 return;
733 }
734
735 // TODO: could also be applied to methods?
736 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
737 if (!Fn) {
738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
739 << "warn_unused_result" << 0 /*function*/;
740 return;
741 }
742
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000743 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000744}
745
746static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000747 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000748 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000749 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000750 return;
751 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000752
753 // TODO: could also be applied to methods?
754 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
755 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
756 << "weak" << 2 /*variable and function*/;
757 return;
758 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000759
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000760 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000761}
762
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000763static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
764 // check the attribute arguments.
765 if (Attr.getNumArgs() != 0) {
766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
767 return;
768 }
769
770 // weak_import only applies to variable & function declarations.
771 bool isDef = false;
772 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
773 isDef = (!VD->hasExternalStorage() || VD->getInit());
774 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
775 isDef = FD->getBody();
Mike Stump862a2c52009-03-18 15:05:17 +0000776 } else if (isa<ObjCPropertyDecl>(D)) {
777 // We ignore weak import on properties
Mike Stump1c90f4d2009-03-18 17:39:31 +0000778 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000779 } else {
780 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
781 << "weak_import" << 2 /*variable and function*/;
782 return;
783 }
784
785 // Merge should handle any subsequent violations.
786 if (isDef) {
787 S.Diag(Attr.getLoc(),
788 diag::warn_attribute_weak_import_invalid_on_definition)
789 << "weak_import" << 2 /*variable and function*/;
790 return;
791 }
792
793 D->addAttr(::new (S.Context) WeakImportAttr());
794}
795
Chris Lattner026dc962009-02-14 07:37:35 +0000796static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000797 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000798 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000799 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000800 return;
801 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000802
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000803 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000804 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000805 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000806 return;
807 }
808
Chris Lattner026dc962009-02-14 07:37:35 +0000809 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000810 if (!FD) {
811 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000812 << "dllimport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000813 return;
814 }
815
816 // Currently, the dllimport attribute is ignored for inlined functions.
817 // Warning is emitted.
818 if (FD->isInline()) {
819 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
820 return;
821 }
822
823 // The attribute is also overridden by a subsequent declaration as dllexport.
824 // Warning is emitted.
825 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
826 nextAttr = nextAttr->getNext()) {
827 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
828 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
829 return;
830 }
831 }
832
Chris Lattner026dc962009-02-14 07:37:35 +0000833 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000834 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
835 return;
836 }
837
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000838 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000839}
840
Chris Lattner026dc962009-02-14 07:37:35 +0000841static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000842 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000843 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000845 return;
846 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000847
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000848 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000849 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000850 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000851 return;
852 }
853
Chris Lattner026dc962009-02-14 07:37:35 +0000854 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000855 if (!FD) {
856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000857 << "dllexport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000858 return;
859 }
860
861 // Currently, the dllexport attribute is ignored for inlined functions,
862 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
863 if (FD->isInline()) {
864 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
865 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
866 return;
867 }
868
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000869 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000870}
871
Chris Lattner026dc962009-02-14 07:37:35 +0000872static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000873 // Attribute has no arguments.
874 if (Attr.getNumArgs() != 1) {
875 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
876 return;
877 }
878
879 // Make sure that there is a string literal as the sections's single
880 // argument.
881 StringLiteral *SE =
882 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
883 if (!SE) {
884 // FIXME
885 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
886 return;
887 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000888 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
889 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000890}
891
Chris Lattner803d0802008-06-29 00:43:07 +0000892static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000893 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000894 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000896 return;
897 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000898
899 // Attribute can be applied only to functions.
900 if (!isa<FunctionDecl>(d)) {
901 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000902 << "stdcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000903 return;
904 }
905
906 // stdcall and fastcall attributes are mutually incompatible.
907 if (d->getAttr<FastCallAttr>()) {
908 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
909 << "stdcall" << "fastcall";
910 return;
911 }
912
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000913 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000914}
915
Chris Lattner803d0802008-06-29 00:43:07 +0000916static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000917 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000918 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000919 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000920 return;
921 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000922
923 if (!isa<FunctionDecl>(d)) {
924 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000925 << "fastcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000926 return;
927 }
928
929 // stdcall and fastcall attributes are mutually incompatible.
930 if (d->getAttr<StdCallAttr>()) {
931 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
932 << "fastcall" << "stdcall";
933 return;
934 }
935
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000936 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000937}
938
Chris Lattner803d0802008-06-29 00:43:07 +0000939static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000940 // check the attribute 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 }
945
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000946 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000947}
948
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000949static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
950 // check the attribute arguments.
951 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000952 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000953 return;
954 }
955
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000956 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000957}
958
959static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
960 // check the attribute arguments.
961 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000962 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000963 return;
964 }
965
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000966 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000967}
968
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000969static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000970 // Match gcc which ignores cleanup attrs when compiling C++.
971 if (S.getLangOptions().CPlusPlus)
972 return;
973
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000974 if (!Attr.getParameterName()) {
975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
976 return;
977 }
978
979 if (Attr.getNumArgs() != 0) {
980 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
981 return;
982 }
983
984 VarDecl *VD = dyn_cast<VarDecl>(d);
985
986 if (!VD || !VD->hasLocalStorage()) {
987 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
988 return;
989 }
990
991 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000992 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
993 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000994 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000995 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000996 Attr.getParameterName();
997 return;
998 }
999
1000 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1001 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001002 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001003 Attr.getParameterName();
1004 return;
1005 }
1006
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001007 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001008 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001009 Attr.getParameterName();
1010 return;
1011 }
1012
Anders Carlsson89941c12009-02-07 23:16:50 +00001013 // We're currently more strict than GCC about what function types we accept.
1014 // If this ever proves to be a problem it should be easy to fix.
1015 QualType Ty = S.Context.getPointerType(VD->getType());
1016 QualType ParamTy = FD->getParamDecl(0)->getType();
Anders Carlssonb90052a2009-02-25 17:19:08 +00001017 if (S.CheckAssignmentConstraints(Ty, ParamTy) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001018 S.Diag(Attr.getLoc(),
1019 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1020 Attr.getParameterName() << ParamTy << Ty;
1021 return;
1022 }
1023
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001024 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001025}
1026
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027/// Handle __attribute__((format(type,idx,firstarg))) attributes
1028/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001029static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001030
Chris Lattner545dd342008-06-28 23:36:30 +00001031 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001032 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001033 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001034 return;
1035 }
1036
Chris Lattner545dd342008-06-28 23:36:30 +00001037 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001039 return;
1040 }
1041
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001042 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001043 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001044 << "format" << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001045 return;
1046 }
1047
1048 // FIXME: in C++ the implicit 'this' function parameter also counts.
1049 // this is needed in order to be compatible with GCC
1050 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001051 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001052 unsigned FirstIdx = 1;
1053
Chris Lattner545dd342008-06-28 23:36:30 +00001054 const char *Format = Attr.getParameterName()->getName();
1055 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001056
1057 // Normalize the argument, __foo__ becomes foo.
1058 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1059 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1060 Format += 2;
1061 FormatLen -= 4;
1062 }
1063
1064 bool Supported = false;
1065 bool is_NSString = false;
1066 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001067 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001068
1069 switch (FormatLen) {
1070 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001071 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1072 case 6: Supported = !memcmp(Format, "printf", 6); break;
1073 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001074 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001075 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1076 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1077 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001078 break;
1079 }
1080
1081 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001082 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1083 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001084 return;
1085 }
1086
1087 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001088 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001089 llvm::APSInt Idx(32);
1090 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001091 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001092 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001093 return;
1094 }
1095
1096 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001097 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001098 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001099 return;
1100 }
1101
1102 // FIXME: Do we need to bounds check?
1103 unsigned ArgIdx = Idx.getZExtValue() - 1;
1104
1105 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001106 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001107
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001108 if (is_CFString) {
1109 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001110 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1111 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001112 return;
1113 }
1114 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001115 // FIXME: do we need to check if the type is NSString*? What are
1116 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001117 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001118 // FIXME: Should highlight the actual expression that has the
1119 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001120 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1121 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001122 return;
1123 }
1124 } else if (!Ty->isPointerType() ||
1125 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1126 // FIXME: Should highlight the actual expression that has the
1127 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001128 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1129 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001130 return;
1131 }
1132
1133 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001134 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001135 llvm::APSInt FirstArg(32);
1136 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001137 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001138 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001139 return;
1140 }
1141
1142 // check if the function is variadic if the 3rd argument non-zero
1143 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001144 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001145 ++NumArgs; // +1 for ...
1146 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001147 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001148 return;
1149 }
1150 }
1151
Chris Lattner3c73c412008-11-19 08:23:25 +00001152 // strftime requires FirstArg to be 0 because it doesn't read from any
1153 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001154 if (is_strftime) {
1155 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001156 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1157 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001158 return;
1159 }
1160 // if 0 it disables parameter checking (to use with e.g. va_list)
1161 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001162 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001163 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001164 return;
1165 }
1166
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001167 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001168 Idx.getZExtValue(), FirstArg.getZExtValue()));
1169}
1170
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001171static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1172 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001173 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001174 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001175 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001176 return;
1177 }
1178
Eli Friedmanbc887452008-09-02 05:19:23 +00001179 // FIXME: This shouldn't be restricted to typedefs
1180 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1181 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001182 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001183 << "transparent_union" << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001184 return;
1185 }
1186
Eli Friedmanbc887452008-09-02 05:19:23 +00001187 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188
Eli Friedmanbc887452008-09-02 05:19:23 +00001189 // FIXME: Should we do a check for RD->isDefinition()?
1190
1191 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
1192 // we might silently generate incorrect code; see following code
Douglas Gregor6ab35242009-04-09 21:40:53 +00001193 for (RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1194 FieldEnd = RD->field_end(S.Context);
Douglas Gregor44b43212008-12-11 16:49:14 +00001195 Field != FieldEnd; ++Field) {
1196 if (!Field->getType()->isPointerType()) {
Eli Friedmanbc887452008-09-02 05:19:23 +00001197 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
1198 return;
1199 }
1200 }
1201
1202 // FIXME: This is a complete hack; we should be properly propagating
1203 // transparent_union through Sema. That said, this is close enough to
1204 // correctly compile all the common cases of transparent_union without
1205 // errors or warnings
1206 QualType NewTy = S.Context.VoidPtrTy;
1207 NewTy.addConst();
1208 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001209}
1210
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001211static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001212 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001213 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001215 return;
1216 }
Chris Lattner545dd342008-06-28 23:36:30 +00001217 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001218 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1219
1220 // Make sure that there is a string literal as the annotation's single
1221 // argument.
1222 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001223 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001224 return;
1225 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001226 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1227 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228}
1229
Chris Lattner803d0802008-06-29 00:43:07 +00001230static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001231 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001232 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001233 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001234 return;
1235 }
1236
1237 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001238 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001239 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001240 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 Align = 128;
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001242 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001243 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001244 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001245
1246 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1247 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001248 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1250 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001251 return;
1252 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001253 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1254 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1255 << alignmentExpr->getSourceRange();
1256 return;
1257 }
1258
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001259 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001261
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001262/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001263/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001264///
1265/// Despite what would be logical, the mode attribute is a decl attribute,
1266/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1267/// 'G' be HImode, not an intermediate pointer.
1268///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001269static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001270 // This attribute isn't documented, but glibc uses it. It changes
1271 // the width of an int or unsigned int to the specified size.
1272
1273 // Check that there aren't any arguments
1274 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001275 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001276 return;
1277 }
1278
1279 IdentifierInfo *Name = Attr.getParameterName();
1280 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001281 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001282 return;
1283 }
1284 const char *Str = Name->getName();
1285 unsigned Len = Name->getLength();
1286
1287 // Normalize the attribute name, __foo__ becomes foo.
1288 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1289 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1290 Str += 2;
1291 Len -= 4;
1292 }
1293
1294 unsigned DestWidth = 0;
1295 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001296 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001297 switch (Len) {
1298 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001299 switch (Str[0]) {
1300 case 'Q': DestWidth = 8; break;
1301 case 'H': DestWidth = 16; break;
1302 case 'S': DestWidth = 32; break;
1303 case 'D': DestWidth = 64; break;
1304 case 'X': DestWidth = 96; break;
1305 case 'T': DestWidth = 128; break;
1306 }
1307 if (Str[1] == 'F') {
1308 IntegerMode = false;
1309 } else if (Str[1] == 'C') {
1310 IntegerMode = false;
1311 ComplexMode = true;
1312 } else if (Str[1] != 'I') {
1313 DestWidth = 0;
1314 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001315 break;
1316 case 4:
1317 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1318 // pointer on PIC16 and other embedded platforms.
1319 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001320 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001321 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001322 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001323 break;
1324 case 7:
1325 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001326 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001327 break;
1328 }
1329
1330 QualType OldTy;
1331 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1332 OldTy = TD->getUnderlyingType();
1333 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1334 OldTy = VD->getType();
1335 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001336 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1337 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001338 return;
1339 }
Eli Friedman73397492009-03-03 06:41:03 +00001340
1341 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1342 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1343 else if (IntegerMode) {
1344 if (!OldTy->isIntegralType())
1345 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1346 } else if (ComplexMode) {
1347 if (!OldTy->isComplexType())
1348 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1349 } else {
1350 if (!OldTy->isFloatingType())
1351 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1352 }
1353
Eli Friedmanf98aba32009-02-13 02:31:07 +00001354 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1355 // int8_t and friends, at least with glibc.
1356 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1357 // the wrong width on unusual platforms.
1358 // FIXME: Make sure floating-point mappings are accurate
1359 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001360 QualType NewTy;
1361 switch (DestWidth) {
1362 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001363 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001364 return;
1365 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001366 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001367 return;
1368 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001369 if (!IntegerMode) {
1370 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1371 return;
1372 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001373 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001374 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001375 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001376 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001377 break;
1378 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001379 if (!IntegerMode) {
1380 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1381 return;
1382 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001383 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001384 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001385 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001386 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001387 break;
1388 case 32:
1389 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001390 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001391 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001392 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001393 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001394 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001395 break;
1396 case 64:
1397 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001398 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001399 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001400 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001401 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001402 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001403 break;
Eli Friedman73397492009-03-03 06:41:03 +00001404 case 96:
1405 NewTy = S.Context.LongDoubleTy;
1406 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001407 case 128:
1408 if (!IntegerMode) {
1409 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1410 return;
1411 }
1412 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001413 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001414 }
1415
Eli Friedman73397492009-03-03 06:41:03 +00001416 if (ComplexMode) {
1417 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001418 }
1419
1420 // Install the new type.
1421 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1422 TD->setUnderlyingType(NewTy);
1423 else
1424 cast<ValueDecl>(D)->setType(NewTy);
1425}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001426
Anders Carlssond87df372009-02-13 06:46:13 +00001427static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1428 // check the attribute arguments.
1429 if (Attr.getNumArgs() > 0) {
1430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1431 return;
1432 }
Anders Carlssone896d982009-02-13 08:11:52 +00001433
Anders Carlsson5bab7882009-02-19 19:16:48 +00001434 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001436 << "nodebug" << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001437 return;
1438 }
1439
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001440 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001441}
1442
Anders Carlsson5bab7882009-02-19 19:16:48 +00001443static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1444 // check the attribute arguments.
1445 if (Attr.getNumArgs() != 0) {
1446 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1447 return;
1448 }
1449
1450 if (!isFunctionOrMethod(d)) {
1451 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1452 << "noinline" << 0 /*function*/;
1453 return;
1454 }
1455
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001456 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001457}
1458
Chris Lattner26e25542009-04-14 16:30:50 +00001459static void HandleGNUCInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1460 // check the attribute arguments.
1461 if (Attr.getNumArgs() != 0) {
1462 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1463 return;
1464 }
1465
1466 if (!isFunctionOrMethod(d)) {
1467 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1468 << "gnuc_inline" << 0 /*function*/;
1469 return;
1470 }
1471
1472 d->addAttr(::new (S.Context) GNUCInlineAttr());
1473}
1474
Fariborz Jahanianee760332009-03-27 18:38:55 +00001475static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1476 // check the attribute arguments.
1477 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001479 return;
1480 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001481
Fariborz Jahanianee760332009-03-27 18:38:55 +00001482 if (!isFunctionOrMethod(d)) {
1483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1484 << "regparm" << 0 /*function*/;
1485 return;
1486 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001487
1488 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1489 llvm::APSInt NumParams(32);
1490 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1491 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1492 << "regparm" << NumParamsExpr->getSourceRange();
1493 return;
1494 }
1495
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001496 if (S.Context.Target.getRegParmMax() == 0) {
1497 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001498 << NumParamsExpr->getSourceRange();
1499 return;
1500 }
1501
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001502 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001503 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1504 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001505 return;
1506 }
1507
1508 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001509}
1510
Chris Lattner0744e5f2008-06-29 00:23:49 +00001511//===----------------------------------------------------------------------===//
1512// Top Level Sema Entry Points
1513//===----------------------------------------------------------------------===//
1514
Sebastian Redla89d82c2008-12-21 19:24:58 +00001515/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001516/// the attribute applies to decls. If the attribute is a type attribute, just
1517/// silently ignore it.
1518static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1519 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001520 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001521 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001522 case AttributeList::AT_objc_gc:
1523 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001524 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001525 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1526 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001527 case AttributeList::AT_always_inline:
1528 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001529 case AttributeList::AT_analyzer_noreturn:
1530 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001531 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1532 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1533 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1534 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1535 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1536 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001537 case AttributeList::AT_ext_vector_type:
1538 HandleExtVectorTypeAttr(D, Attr, S);
1539 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001540 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1541 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner26e25542009-04-14 16:30:50 +00001542 case AttributeList::AT_gnuc_inline: HandleGNUCInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001543 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001544 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1545 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1546 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1547 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001548 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001549 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001550 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001551 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001552 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001553 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001554 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001555 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1556 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001557 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001558 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001559 case AttributeList::AT_transparent_union:
1560 HandleTransparentUnionAttr(D, Attr, S);
1561 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001562 case AttributeList::AT_objc_exception:
1563 HandleObjCExceptionAttr(D, Attr, S);
1564 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001565 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001566 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001567 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001568 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001569 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1570 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001571 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001572 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001573 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001574 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001575 case AttributeList::IgnoredAttribute:
1576 // Just ignore
1577 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001578 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001579 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001580 break;
1581 }
1582}
1583
1584/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1585/// attribute list to the specified decl, ignoring any type attributes.
1586void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1587 while (AttrList) {
1588 ProcessDeclAttribute(D, *AttrList, *this);
1589 AttrList = AttrList->getNext();
1590 }
1591}
1592
1593
Chris Lattner0744e5f2008-06-29 00:23:49 +00001594/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1595/// it, apply them to D. This is a bit tricky because PD can have attributes
1596/// specified in many different places, and we need to find and apply them all.
1597void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1598 // Apply decl attributes from the DeclSpec if present.
1599 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1600 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001601
Chris Lattner0744e5f2008-06-29 00:23:49 +00001602 // Walk the declarator structure, applying decl attributes that were in a type
1603 // position to the decl itself. This handles cases like:
1604 // int *__attr__(x)** D;
1605 // when X is a decl attribute.
1606 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1607 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1608 ProcessDeclAttributeList(D, Attrs);
1609
1610 // Finally, apply any attributes on the decl itself.
1611 if (const AttributeList *Attrs = PD.getAttributes())
1612 ProcessDeclAttributeList(D, Attrs);
1613}
1614