blob: 919f28ffec7868396602fe2171947b41b3fb75bf [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
Mike Stump19c30c02009-04-29 19:03:13 +0000411 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
412 ValueDecl *VD = dyn_cast<ValueDecl>(d);
413 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
414 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
415 << attrName << 0 /*function*/;
416 return false;
417 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000418 }
419
Ted Kremenekb7252322009-04-10 00:01:14 +0000420 return true;
421}
422
423static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
424 if (HandleCommonNoReturnAttr(d, Attr, S, "noreturn"))
425 d->addAttr(::new (S.Context) NoReturnAttr());
426}
427
428static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
429 Sema &S) {
430 if (HandleCommonNoReturnAttr(d, Attr, S, "analyzer_noreturn"))
431 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000432}
433
Ted Kremenek73798892008-07-25 04:39:19 +0000434static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
435 // check the attribute arguments.
436 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000438 return;
439 }
440
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000441 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000443 << "unused" << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000444 return;
445 }
446
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000447 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000448}
449
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000450static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
451 // check the attribute arguments.
452 if (Attr.getNumArgs() != 0) {
453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
454 return;
455 }
456
457 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000458 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000459 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
460 return;
461 }
462 } else if (!isFunctionOrMethod(d)) {
463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000464 << "used" << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000465 return;
466 }
467
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000468 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000469}
470
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000471static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
472 // check the attribute arguments.
473 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000474 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
475 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000476 return;
477 }
478
479 int priority = 65535; // FIXME: Do not hardcode such constants.
480 if (Attr.getNumArgs() > 0) {
481 Expr *E = static_cast<Expr *>(Attr.getArg(0));
482 llvm::APSInt Idx(32);
483 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000484 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000485 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000486 return;
487 }
488 priority = Idx.getZExtValue();
489 }
490
Chris Lattnerc5197432009-04-14 17:02:11 +0000491 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000492 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000493 << "constructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000494 return;
495 }
496
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000497 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000498}
499
500static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
501 // check the attribute arguments.
502 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000503 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
504 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000505 return;
506 }
507
508 int priority = 65535; // FIXME: Do not hardcode such constants.
509 if (Attr.getNumArgs() > 0) {
510 Expr *E = static_cast<Expr *>(Attr.getArg(0));
511 llvm::APSInt Idx(32);
512 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000514 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000515 return;
516 }
517 priority = Idx.getZExtValue();
518 }
519
Anders Carlsson6782fc62008-08-22 22:10:48 +0000520 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000521 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000522 << "destructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000523 return;
524 }
525
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000526 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000527}
528
Chris Lattner803d0802008-06-29 00:43:07 +0000529static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000530 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000531 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000532 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000533 return;
534 }
535
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000536 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000537}
538
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000539static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
540 // check the attribute arguments.
541 if (Attr.getNumArgs() != 0) {
542 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
543 return;
544 }
545
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000546 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000547}
548
Chris Lattner803d0802008-06-29 00:43:07 +0000549static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000550 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000551 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000553 return;
554 }
555
Chris Lattner545dd342008-06-28 23:36:30 +0000556 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000557 Arg = Arg->IgnoreParenCasts();
558 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
559
560 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000561 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000562 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000563 return;
564 }
565
566 const char *TypeStr = Str->getStrData();
567 unsigned TypeLen = Str->getByteLength();
568 VisibilityAttr::VisibilityTypes type;
569
570 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
571 type = VisibilityAttr::DefaultVisibility;
572 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
573 type = VisibilityAttr::HiddenVisibility;
574 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
575 type = VisibilityAttr::HiddenVisibility; // FIXME
576 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
577 type = VisibilityAttr::ProtectedVisibility;
578 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000579 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000580 return;
581 }
582
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000583 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000584}
585
Chris Lattner0db29ec2009-02-14 08:09:34 +0000586static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
587 Sema &S) {
588 if (Attr.getNumArgs() != 0) {
589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
590 return;
591 }
592
593 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
594 if (OCI == 0) {
595 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
596 return;
597 }
598
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000599 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000600}
601
602static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000603 if (Attr.getNumArgs() != 0) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
605 return;
606 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000607 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000608 QualType T = TD->getUnderlyingType();
609 if (!T->isPointerType() ||
610 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
611 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
612 return;
613 }
614 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000615 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000616}
617
Douglas Gregorf9201e02009-02-11 23:02:49 +0000618static void
619HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
620 if (Attr.getNumArgs() != 0) {
621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
622 return;
623 }
624
625 if (!isa<FunctionDecl>(D)) {
626 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
627 return;
628 }
629
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000630 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000631}
632
Steve Naroff9eae5762008-09-18 16:44:58 +0000633static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
634 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000635 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000636 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000637 return;
638 }
639
640 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000641 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000642 return;
643 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000644
645 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000646 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000647 type = BlocksAttr::ByRef;
648 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000649 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000650 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000651 return;
652 }
653
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000654 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000655}
656
Anders Carlsson77091822008-10-05 18:05:59 +0000657static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
658 // check the attribute arguments.
659 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000660 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
661 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000662 return;
663 }
664
665 int sentinel = 0;
666 if (Attr.getNumArgs() > 0) {
667 Expr *E = static_cast<Expr *>(Attr.getArg(0));
668 llvm::APSInt Idx(32);
669 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000670 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000671 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000672 return;
673 }
674 sentinel = Idx.getZExtValue();
675
676 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000677 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
678 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000679 return;
680 }
681 }
682
683 int nullPos = 0;
684 if (Attr.getNumArgs() > 1) {
685 Expr *E = static_cast<Expr *>(Attr.getArg(1));
686 llvm::APSInt Idx(32);
687 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000688 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000689 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000690 return;
691 }
692 nullPos = Idx.getZExtValue();
693
694 if (nullPos > 1 || nullPos < 0) {
695 // FIXME: This error message could be improved, it would be nice
696 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000697 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
698 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000699 return;
700 }
701 }
702
703 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000704 const FunctionType *FT = FD->getType()->getAsFunctionType();
705 assert(FT && "FunctionDecl has non-function type?");
706
707 if (isa<FunctionNoProtoType>(FT)) {
708 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
709 return;
710 }
711
712 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Anders Carlsson77091822008-10-05 18:05:59 +0000713 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
714 return;
715 }
716 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
717 if (!MD->isVariadic()) {
718 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
719 return;
720 }
721 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000722 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000723 << "sentinel" << 3 /*function or method*/;
Anders Carlsson77091822008-10-05 18:05:59 +0000724 return;
725 }
726
727 // FIXME: Actually create the attribute.
728}
729
Chris Lattner026dc962009-02-14 07:37:35 +0000730static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
731 // check the attribute arguments.
732 if (Attr.getNumArgs() != 0) {
733 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
734 return;
735 }
736
737 // TODO: could also be applied to methods?
738 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
739 if (!Fn) {
740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
741 << "warn_unused_result" << 0 /*function*/;
742 return;
743 }
744
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000745 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000746}
747
748static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000749 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000750 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000752 return;
753 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000754
755 // TODO: could also be applied to methods?
756 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
758 << "weak" << 2 /*variable and function*/;
759 return;
760 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000761
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000762 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000763}
764
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000765static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
766 // check the attribute arguments.
767 if (Attr.getNumArgs() != 0) {
768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
769 return;
770 }
771
772 // weak_import only applies to variable & function declarations.
773 bool isDef = false;
774 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
775 isDef = (!VD->hasExternalStorage() || VD->getInit());
776 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor72971342009-04-18 00:02:19 +0000777 isDef = FD->getBody(S.Context);
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000778 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
779 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000780 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000781 } else {
782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
783 << "weak_import" << 2 /*variable and function*/;
784 return;
785 }
786
787 // Merge should handle any subsequent violations.
788 if (isDef) {
789 S.Diag(Attr.getLoc(),
790 diag::warn_attribute_weak_import_invalid_on_definition)
791 << "weak_import" << 2 /*variable and function*/;
792 return;
793 }
794
795 D->addAttr(::new (S.Context) WeakImportAttr());
796}
797
Chris Lattner026dc962009-02-14 07:37:35 +0000798static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000799 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000800 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000801 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000802 return;
803 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000804
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000805 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000806 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000807 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000808 return;
809 }
810
Chris Lattner026dc962009-02-14 07:37:35 +0000811 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000812 if (!FD) {
813 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000814 << "dllimport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000815 return;
816 }
817
818 // Currently, the dllimport attribute is ignored for inlined functions.
819 // Warning is emitted.
820 if (FD->isInline()) {
821 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
822 return;
823 }
824
825 // The attribute is also overridden by a subsequent declaration as dllexport.
826 // Warning is emitted.
827 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
828 nextAttr = nextAttr->getNext()) {
829 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
830 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
831 return;
832 }
833 }
834
Chris Lattner026dc962009-02-14 07:37:35 +0000835 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000836 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
837 return;
838 }
839
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000840 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000841}
842
Chris Lattner026dc962009-02-14 07:37:35 +0000843static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000844 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000845 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000847 return;
848 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000849
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000850 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000851 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000852 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000853 return;
854 }
855
Chris Lattner026dc962009-02-14 07:37:35 +0000856 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000857 if (!FD) {
858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000859 << "dllexport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000860 return;
861 }
862
863 // Currently, the dllexport attribute is ignored for inlined functions,
864 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
865 if (FD->isInline()) {
866 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
867 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
868 return;
869 }
870
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000871 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000872}
873
Chris Lattner026dc962009-02-14 07:37:35 +0000874static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000875 // Attribute has no arguments.
876 if (Attr.getNumArgs() != 1) {
877 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
878 return;
879 }
880
881 // Make sure that there is a string literal as the sections's single
882 // argument.
883 StringLiteral *SE =
884 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
885 if (!SE) {
886 // FIXME
887 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
888 return;
889 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000890 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
891 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000892}
893
Chris Lattner803d0802008-06-29 00:43:07 +0000894static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000895 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000896 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000897 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000898 return;
899 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000900
901 // Attribute can be applied only to functions.
902 if (!isa<FunctionDecl>(d)) {
903 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000904 << "stdcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000905 return;
906 }
907
908 // stdcall and fastcall attributes are mutually incompatible.
909 if (d->getAttr<FastCallAttr>()) {
910 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
911 << "stdcall" << "fastcall";
912 return;
913 }
914
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000915 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000916}
917
Chris Lattner803d0802008-06-29 00:43:07 +0000918static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000919 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000920 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000922 return;
923 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000924
925 if (!isa<FunctionDecl>(d)) {
926 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000927 << "fastcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000928 return;
929 }
930
931 // stdcall and fastcall attributes are mutually incompatible.
932 if (d->getAttr<StdCallAttr>()) {
933 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
934 << "fastcall" << "stdcall";
935 return;
936 }
937
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000938 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000939}
940
Chris Lattner803d0802008-06-29 00:43:07 +0000941static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000942 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000943 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000944 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000945 return;
946 }
947
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000948 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000949}
950
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000951static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
952 // check the attribute arguments.
953 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000954 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000955 return;
956 }
957
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000958 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000959}
960
961static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
962 // check the attribute arguments.
963 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000964 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000965 return;
966 }
967
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000968 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000969}
970
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000971static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000972 // Match gcc which ignores cleanup attrs when compiling C++.
973 if (S.getLangOptions().CPlusPlus)
974 return;
975
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000976 if (!Attr.getParameterName()) {
977 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
978 return;
979 }
980
981 if (Attr.getNumArgs() != 0) {
982 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
983 return;
984 }
985
986 VarDecl *VD = dyn_cast<VarDecl>(d);
987
988 if (!VD || !VD->hasLocalStorage()) {
989 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
990 return;
991 }
992
993 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000994 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
995 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000996 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000997 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000998 Attr.getParameterName();
999 return;
1000 }
1001
1002 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1003 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001004 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001005 Attr.getParameterName();
1006 return;
1007 }
1008
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001009 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001011 Attr.getParameterName();
1012 return;
1013 }
1014
Anders Carlsson89941c12009-02-07 23:16:50 +00001015 // We're currently more strict than GCC about what function types we accept.
1016 // If this ever proves to be a problem it should be easy to fix.
1017 QualType Ty = S.Context.getPointerType(VD->getType());
1018 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001019 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001020 S.Diag(Attr.getLoc(),
1021 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1022 Attr.getParameterName() << ParamTy << Ty;
1023 return;
1024 }
1025
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001026 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001027}
1028
Chris Lattner6b6b5372008-06-26 18:38:35 +00001029/// Handle __attribute__((format(type,idx,firstarg))) attributes
1030/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001031static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001032
Chris Lattner545dd342008-06-28 23:36:30 +00001033 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001035 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001036 return;
1037 }
1038
Chris Lattner545dd342008-06-28 23:36:30 +00001039 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001041 return;
1042 }
1043
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001044 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001045 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001046 << "format" << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001047 return;
1048 }
1049
1050 // FIXME: in C++ the implicit 'this' function parameter also counts.
1051 // this is needed in order to be compatible with GCC
1052 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001053 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001054 unsigned FirstIdx = 1;
1055
Chris Lattner545dd342008-06-28 23:36:30 +00001056 const char *Format = Attr.getParameterName()->getName();
1057 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001058
1059 // Normalize the argument, __foo__ becomes foo.
1060 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1061 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1062 Format += 2;
1063 FormatLen -= 4;
1064 }
1065
1066 bool Supported = false;
1067 bool is_NSString = false;
1068 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001069 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001070
1071 switch (FormatLen) {
1072 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001073 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1074 case 6: Supported = !memcmp(Format, "printf", 6); break;
1075 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001076 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001077 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1078 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1079 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001080 break;
1081 }
1082
1083 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001084 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1085 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001086 return;
1087 }
1088
1089 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001090 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001091 llvm::APSInt Idx(32);
1092 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001094 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001095 return;
1096 }
1097
1098 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001099 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001100 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001101 return;
1102 }
1103
1104 // FIXME: Do we need to bounds check?
1105 unsigned ArgIdx = Idx.getZExtValue() - 1;
1106
1107 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001108 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001109
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001110 if (is_CFString) {
1111 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001112 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1113 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001114 return;
1115 }
1116 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001117 // FIXME: do we need to check if the type is NSString*? What are
1118 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001119 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001120 // FIXME: Should highlight the actual expression that has the
1121 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001122 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1123 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001124 return;
1125 }
1126 } else if (!Ty->isPointerType() ||
1127 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1128 // FIXME: Should highlight the actual expression that has the
1129 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001130 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1131 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001132 return;
1133 }
1134
1135 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001136 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001137 llvm::APSInt FirstArg(32);
1138 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001140 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001141 return;
1142 }
1143
1144 // check if the function is variadic if the 3rd argument non-zero
1145 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001146 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001147 ++NumArgs; // +1 for ...
1148 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001149 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001150 return;
1151 }
1152 }
1153
Chris Lattner3c73c412008-11-19 08:23:25 +00001154 // strftime requires FirstArg to be 0 because it doesn't read from any
1155 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001156 if (is_strftime) {
1157 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001158 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1159 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001160 return;
1161 }
1162 // if 0 it disables parameter checking (to use with e.g. va_list)
1163 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001164 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001165 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166 return;
1167 }
1168
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001169 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001170 Idx.getZExtValue(), FirstArg.getZExtValue()));
1171}
1172
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001173static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1174 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001175 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001176 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001178 return;
1179 }
1180
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001181 // Try to find the underlying union declaration.
1182 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001183 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001184 if (TD && TD->getUnderlyingType()->isUnionType())
1185 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1186 else
1187 RD = dyn_cast<RecordDecl>(d);
1188
1189 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001190 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001191 << "transparent_union" << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001192 return;
1193 }
1194
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001195 if (!RD->isDefinition()) {
1196 S.Diag(Attr.getLoc(),
1197 diag::warn_transparent_union_attribute_not_definition);
1198 return;
1199 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001200
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001201 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1202 FieldEnd = RD->field_end(S.Context);
1203 if (Field == FieldEnd) {
1204 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1205 return;
1206 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001207
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001208 FieldDecl *FirstField = *Field;
1209 QualType FirstType = FirstField->getType();
1210 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1211 S.Diag(FirstField->getLocation(),
1212 diag::warn_transparent_union_attribute_floating);
1213 return;
1214 }
1215
1216 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1217 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1218 for (; Field != FieldEnd; ++Field) {
1219 QualType FieldType = Field->getType();
1220 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1221 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1222 // Warn if we drop the attribute.
1223 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1224 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1225 : S.Context.getTypeAlign(FieldType);
1226 S.Diag(Field->getLocation(),
1227 diag::warn_transparent_union_attribute_field_size_align)
1228 << isSize << Field->getDeclName() << FieldBits;
1229 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1230 S.Diag(FirstField->getLocation(),
1231 diag::note_transparent_union_first_field_size_align)
1232 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001233 return;
1234 }
1235 }
1236
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001237 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001238}
1239
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001240static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001242 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001243 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001244 return;
1245 }
Chris Lattner545dd342008-06-28 23:36:30 +00001246 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001247 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1248
1249 // Make sure that there is a string literal as the annotation's single
1250 // argument.
1251 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001252 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001253 return;
1254 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001255 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1256 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001257}
1258
Chris Lattner803d0802008-06-29 00:43:07 +00001259static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001261 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001262 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263 return;
1264 }
1265
1266 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001267 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001268 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001269 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001270 Align = 128;
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001271 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001272 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001273 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001274
1275 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1276 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001277 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1279 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001280 return;
1281 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001282 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1283 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1284 << alignmentExpr->getSourceRange();
1285 return;
1286 }
1287
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001288 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001289}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001290
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001291/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001292/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001293///
1294/// Despite what would be logical, the mode attribute is a decl attribute,
1295/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1296/// 'G' be HImode, not an intermediate pointer.
1297///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001298static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001299 // This attribute isn't documented, but glibc uses it. It changes
1300 // the width of an int or unsigned int to the specified size.
1301
1302 // Check that there aren't any arguments
1303 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001304 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001305 return;
1306 }
1307
1308 IdentifierInfo *Name = Attr.getParameterName();
1309 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001310 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001311 return;
1312 }
1313 const char *Str = Name->getName();
1314 unsigned Len = Name->getLength();
1315
1316 // Normalize the attribute name, __foo__ becomes foo.
1317 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1318 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1319 Str += 2;
1320 Len -= 4;
1321 }
1322
1323 unsigned DestWidth = 0;
1324 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001325 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001326 switch (Len) {
1327 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001328 switch (Str[0]) {
1329 case 'Q': DestWidth = 8; break;
1330 case 'H': DestWidth = 16; break;
1331 case 'S': DestWidth = 32; break;
1332 case 'D': DestWidth = 64; break;
1333 case 'X': DestWidth = 96; break;
1334 case 'T': DestWidth = 128; break;
1335 }
1336 if (Str[1] == 'F') {
1337 IntegerMode = false;
1338 } else if (Str[1] == 'C') {
1339 IntegerMode = false;
1340 ComplexMode = true;
1341 } else if (Str[1] != 'I') {
1342 DestWidth = 0;
1343 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001344 break;
1345 case 4:
1346 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1347 // pointer on PIC16 and other embedded platforms.
1348 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001349 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001350 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001351 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001352 break;
1353 case 7:
1354 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001355 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001356 break;
1357 }
1358
1359 QualType OldTy;
1360 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1361 OldTy = TD->getUnderlyingType();
1362 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1363 OldTy = VD->getType();
1364 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001365 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1366 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001367 return;
1368 }
Eli Friedman73397492009-03-03 06:41:03 +00001369
1370 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1371 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1372 else if (IntegerMode) {
1373 if (!OldTy->isIntegralType())
1374 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1375 } else if (ComplexMode) {
1376 if (!OldTy->isComplexType())
1377 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1378 } else {
1379 if (!OldTy->isFloatingType())
1380 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1381 }
1382
Eli Friedmanf98aba32009-02-13 02:31:07 +00001383 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1384 // int8_t and friends, at least with glibc.
1385 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1386 // the wrong width on unusual platforms.
1387 // FIXME: Make sure floating-point mappings are accurate
1388 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001389 QualType NewTy;
1390 switch (DestWidth) {
1391 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001392 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001393 return;
1394 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001395 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001396 return;
1397 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001398 if (!IntegerMode) {
1399 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1400 return;
1401 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001402 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001403 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001404 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001405 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001406 break;
1407 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001408 if (!IntegerMode) {
1409 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1410 return;
1411 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001412 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001413 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001414 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001415 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001416 break;
1417 case 32:
1418 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001419 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001420 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001421 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001422 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001423 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001424 break;
1425 case 64:
1426 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001427 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001428 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001429 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001430 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001431 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001432 break;
Eli Friedman73397492009-03-03 06:41:03 +00001433 case 96:
1434 NewTy = S.Context.LongDoubleTy;
1435 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001436 case 128:
1437 if (!IntegerMode) {
1438 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1439 return;
1440 }
1441 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001442 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001443 }
1444
Eli Friedman73397492009-03-03 06:41:03 +00001445 if (ComplexMode) {
1446 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001447 }
1448
1449 // Install the new type.
1450 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1451 TD->setUnderlyingType(NewTy);
1452 else
1453 cast<ValueDecl>(D)->setType(NewTy);
1454}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001455
Anders Carlssond87df372009-02-13 06:46:13 +00001456static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1457 // check the attribute arguments.
1458 if (Attr.getNumArgs() > 0) {
1459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1460 return;
1461 }
Anders Carlssone896d982009-02-13 08:11:52 +00001462
Anders Carlsson5bab7882009-02-19 19:16:48 +00001463 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001464 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001465 << "nodebug" << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001466 return;
1467 }
1468
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001469 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001470}
1471
Anders Carlsson5bab7882009-02-19 19:16:48 +00001472static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1473 // check the attribute arguments.
1474 if (Attr.getNumArgs() != 0) {
1475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1476 return;
1477 }
1478
Chris Lattnerc5197432009-04-14 17:02:11 +00001479 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001480 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1481 << "noinline" << 0 /*function*/;
1482 return;
1483 }
1484
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001485 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001486}
1487
Chris Lattnercf2a7212009-04-20 19:12:28 +00001488static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001489 // check the attribute arguments.
1490 if (Attr.getNumArgs() != 0) {
1491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1492 return;
1493 }
1494
Chris Lattnerc5197432009-04-14 17:02:11 +00001495 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1496 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001497 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnercf2a7212009-04-20 19:12:28 +00001498 << "gnu_inline" << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001499 return;
1500 }
1501
Chris Lattnerc5197432009-04-14 17:02:11 +00001502 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001503 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001504 return;
1505 }
1506
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001507 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001508}
1509
Fariborz Jahanianee760332009-03-27 18:38:55 +00001510static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1511 // check the attribute arguments.
1512 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001514 return;
1515 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001516
Fariborz Jahanianee760332009-03-27 18:38:55 +00001517 if (!isFunctionOrMethod(d)) {
1518 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1519 << "regparm" << 0 /*function*/;
1520 return;
1521 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001522
1523 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1524 llvm::APSInt NumParams(32);
1525 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1526 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1527 << "regparm" << NumParamsExpr->getSourceRange();
1528 return;
1529 }
1530
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001531 if (S.Context.Target.getRegParmMax() == 0) {
1532 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001533 << NumParamsExpr->getSourceRange();
1534 return;
1535 }
1536
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001537 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001538 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1539 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001540 return;
1541 }
1542
1543 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001544}
1545
Chris Lattner0744e5f2008-06-29 00:23:49 +00001546//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001547// Checker-specific attribute handlers.
1548//===----------------------------------------------------------------------===//
1549
1550static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1551 Sema &S) {
1552
1553 if (!isa<ObjCMethodDecl>(d) && !isa<FunctionDecl>(d)) {
1554 const char *name;
1555
1556 switch (Attr.getKind()) {
1557 default:
1558 assert(0 && "invalid ownership attribute");
1559 return;
1560 case AttributeList::AT_cf_returns_retained:
1561 name = "cf_returns_retained"; break;
1562 case AttributeList::AT_ns_returns_retained:
1563 name = "ns_returns_retained"; break;
1564 };
1565
1566 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1567 name << 3 /* function or method */;
1568 return;
1569 }
1570
1571 switch (Attr.getKind()) {
1572 default:
1573 assert(0 && "invalid ownership attribute");
1574 return;
1575 case AttributeList::AT_cf_returns_retained:
1576 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1577 return;
1578 case AttributeList::AT_ns_returns_retained:
1579 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1580 return;
1581 };
1582}
1583
1584//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001585// Top Level Sema Entry Points
1586//===----------------------------------------------------------------------===//
1587
Sebastian Redla89d82c2008-12-21 19:24:58 +00001588/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001589/// the attribute applies to decls. If the attribute is a type attribute, just
1590/// silently ignore it.
1591static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1592 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001593 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001594 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001595 case AttributeList::AT_objc_gc:
1596 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001597 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001598 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1599 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001600 case AttributeList::AT_always_inline:
1601 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001602 case AttributeList::AT_analyzer_noreturn:
1603 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001604 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1605 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1606 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1607 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1608 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1609 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001610 case AttributeList::AT_ext_vector_type:
1611 HandleExtVectorTypeAttr(D, Attr, S);
1612 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001613 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1614 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001615 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001616 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001617 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1618 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1619 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001620
1621 // Checker-specific.
1622 case AttributeList::AT_ns_returns_retained:
1623 case AttributeList::AT_cf_returns_retained:
1624 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1625
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001626 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001627 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001628 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001629 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001630 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001631 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001632 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001633 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001634 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1635 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001636 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001637 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001638 case AttributeList::AT_transparent_union:
1639 HandleTransparentUnionAttr(D, Attr, S);
1640 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001641 case AttributeList::AT_objc_exception:
1642 HandleObjCExceptionAttr(D, Attr, S);
1643 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001644 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001645 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001646 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001647 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001648 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1649 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001650 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001651 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001652 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001653 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001654 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001655 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001656 // Just ignore
1657 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001658 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001659 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001660 break;
1661 }
1662}
1663
1664/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1665/// attribute list to the specified decl, ignoring any type attributes.
1666void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1667 while (AttrList) {
1668 ProcessDeclAttribute(D, *AttrList, *this);
1669 AttrList = AttrList->getNext();
1670 }
1671}
1672
Chris Lattner0744e5f2008-06-29 00:23:49 +00001673/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1674/// it, apply them to D. This is a bit tricky because PD can have attributes
1675/// specified in many different places, and we need to find and apply them all.
1676void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1677 // Apply decl attributes from the DeclSpec if present.
1678 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1679 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001680
Chris Lattner0744e5f2008-06-29 00:23:49 +00001681 // Walk the declarator structure, applying decl attributes that were in a type
1682 // position to the decl itself. This handles cases like:
1683 // int *__attr__(x)** D;
1684 // when X is a decl attribute.
1685 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1686 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1687 ProcessDeclAttributeList(D, Attrs);
1688
1689 // Finally, apply any attributes on the decl itself.
1690 if (const AttributeList *Attrs = PD.getAttributes())
1691 ProcessDeclAttributeList(D, Attrs);
1692}