blob: 57cd3d2bcb235f1fe1061073d6ca628569a3861b [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
Chris Lattner803d0802008-06-29 00:43:07 +0000403static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000404 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000405 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000407 return;
408 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000409
410 if (!isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000411 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000412 << "noreturn" << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000413 return;
414 }
415
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000416 d->addAttr(::new (S.Context) NoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000417}
418
Ted Kremenek73798892008-07-25 04:39:19 +0000419static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
420 // check the attribute arguments.
421 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000422 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000423 return;
424 }
425
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000426 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000427 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000428 << "unused" << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000429 return;
430 }
431
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000432 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000433}
434
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000435static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
436 // check the attribute arguments.
437 if (Attr.getNumArgs() != 0) {
438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
439 return;
440 }
441
442 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000443 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000444 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
445 return;
446 }
447 } else if (!isFunctionOrMethod(d)) {
448 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000449 << "used" << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000450 return;
451 }
452
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000453 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000454}
455
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000456static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
457 // check the attribute arguments.
458 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
460 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000461 return;
462 }
463
464 int priority = 65535; // FIXME: Do not hardcode such constants.
465 if (Attr.getNumArgs() > 0) {
466 Expr *E = static_cast<Expr *>(Attr.getArg(0));
467 llvm::APSInt Idx(32);
468 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000469 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000470 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000471 return;
472 }
473 priority = Idx.getZExtValue();
474 }
475
476 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
477 if (!Fn) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000478 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000479 << "constructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000480 return;
481 }
482
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000483 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000484}
485
486static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
487 // check the attribute arguments.
488 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000489 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
490 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000491 return;
492 }
493
494 int priority = 65535; // FIXME: Do not hardcode such constants.
495 if (Attr.getNumArgs() > 0) {
496 Expr *E = static_cast<Expr *>(Attr.getArg(0));
497 llvm::APSInt Idx(32);
498 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000499 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000500 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000501 return;
502 }
503 priority = Idx.getZExtValue();
504 }
505
Anders Carlsson6782fc62008-08-22 22:10:48 +0000506 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000507 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000508 << "destructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000509 return;
510 }
511
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000512 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000513}
514
Chris Lattner803d0802008-06-29 00:43:07 +0000515static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000516 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000517 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000518 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000519 return;
520 }
521
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000522 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000523}
524
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000525static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
526 // check the attribute arguments.
527 if (Attr.getNumArgs() != 0) {
528 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
529 return;
530 }
531
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000532 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000533}
534
Chris Lattner803d0802008-06-29 00:43:07 +0000535static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000536 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000537 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000538 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000539 return;
540 }
541
Chris Lattner545dd342008-06-28 23:36:30 +0000542 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000543 Arg = Arg->IgnoreParenCasts();
544 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
545
546 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000547 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000548 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000549 return;
550 }
551
552 const char *TypeStr = Str->getStrData();
553 unsigned TypeLen = Str->getByteLength();
554 VisibilityAttr::VisibilityTypes type;
555
556 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
557 type = VisibilityAttr::DefaultVisibility;
558 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
559 type = VisibilityAttr::HiddenVisibility;
560 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
561 type = VisibilityAttr::HiddenVisibility; // FIXME
562 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
563 type = VisibilityAttr::ProtectedVisibility;
564 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000565 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000566 return;
567 }
568
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000569 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000570}
571
Chris Lattner0db29ec2009-02-14 08:09:34 +0000572static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
573 Sema &S) {
574 if (Attr.getNumArgs() != 0) {
575 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
576 return;
577 }
578
579 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
580 if (OCI == 0) {
581 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
582 return;
583 }
584
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000585 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000586}
587
588static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000589 if (Attr.getNumArgs() != 0) {
590 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
591 return;
592 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000593 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000594 QualType T = TD->getUnderlyingType();
595 if (!T->isPointerType() ||
596 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
597 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
598 return;
599 }
600 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000601 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000602}
603
Douglas Gregorf9201e02009-02-11 23:02:49 +0000604static void
605HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
606 if (Attr.getNumArgs() != 0) {
607 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
608 return;
609 }
610
611 if (!isa<FunctionDecl>(D)) {
612 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
613 return;
614 }
615
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000616 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000617}
618
Steve Naroff9eae5762008-09-18 16:44:58 +0000619static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
620 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000621 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000622 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000623 return;
624 }
625
626 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000628 return;
629 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000630
631 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000632 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000633 type = BlocksAttr::ByRef;
634 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000635 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000636 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000637 return;
638 }
639
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000640 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000641}
642
Anders Carlsson77091822008-10-05 18:05:59 +0000643static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
644 // check the attribute arguments.
645 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
647 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000648 return;
649 }
650
651 int sentinel = 0;
652 if (Attr.getNumArgs() > 0) {
653 Expr *E = static_cast<Expr *>(Attr.getArg(0));
654 llvm::APSInt Idx(32);
655 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000657 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000658 return;
659 }
660 sentinel = Idx.getZExtValue();
661
662 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000663 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
664 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000665 return;
666 }
667 }
668
669 int nullPos = 0;
670 if (Attr.getNumArgs() > 1) {
671 Expr *E = static_cast<Expr *>(Attr.getArg(1));
672 llvm::APSInt Idx(32);
673 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000674 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000675 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000676 return;
677 }
678 nullPos = Idx.getZExtValue();
679
680 if (nullPos > 1 || nullPos < 0) {
681 // FIXME: This error message could be improved, it would be nice
682 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000683 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
684 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000685 return;
686 }
687 }
688
689 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
690 QualType FT = FD->getType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000691 if (!FT->getAsFunctionProtoType()->isVariadic()) {
Anders Carlsson77091822008-10-05 18:05:59 +0000692 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
693 return;
694 }
695 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
696 if (!MD->isVariadic()) {
697 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
698 return;
699 }
700 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000701 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000702 << "sentinel" << 3 /*function or method*/;
Anders Carlsson77091822008-10-05 18:05:59 +0000703 return;
704 }
705
706 // FIXME: Actually create the attribute.
707}
708
Chris Lattner026dc962009-02-14 07:37:35 +0000709static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
710 // check the attribute arguments.
711 if (Attr.getNumArgs() != 0) {
712 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
713 return;
714 }
715
716 // TODO: could also be applied to methods?
717 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
718 if (!Fn) {
719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
720 << "warn_unused_result" << 0 /*function*/;
721 return;
722 }
723
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000724 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000725}
726
727static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000728 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000729 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000731 return;
732 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000733
734 // TODO: could also be applied to methods?
735 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
736 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
737 << "weak" << 2 /*variable and function*/;
738 return;
739 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000740
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000741 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000742}
743
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000744static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
745 // check the attribute arguments.
746 if (Attr.getNumArgs() != 0) {
747 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
748 return;
749 }
750
751 // weak_import only applies to variable & function declarations.
752 bool isDef = false;
753 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
754 isDef = (!VD->hasExternalStorage() || VD->getInit());
755 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
756 isDef = FD->getBody();
757 } else {
758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
759 << "weak_import" << 2 /*variable and function*/;
760 return;
761 }
762
763 // Merge should handle any subsequent violations.
764 if (isDef) {
765 S.Diag(Attr.getLoc(),
766 diag::warn_attribute_weak_import_invalid_on_definition)
767 << "weak_import" << 2 /*variable and function*/;
768 return;
769 }
770
771 D->addAttr(::new (S.Context) WeakImportAttr());
772}
773
Chris Lattner026dc962009-02-14 07:37:35 +0000774static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000775 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000776 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000778 return;
779 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000780
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000781 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000782 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000783 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000784 return;
785 }
786
Chris Lattner026dc962009-02-14 07:37:35 +0000787 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000788 if (!FD) {
789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000790 << "dllimport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000791 return;
792 }
793
794 // Currently, the dllimport attribute is ignored for inlined functions.
795 // Warning is emitted.
796 if (FD->isInline()) {
797 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
798 return;
799 }
800
801 // The attribute is also overridden by a subsequent declaration as dllexport.
802 // Warning is emitted.
803 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
804 nextAttr = nextAttr->getNext()) {
805 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
806 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
807 return;
808 }
809 }
810
Chris Lattner026dc962009-02-14 07:37:35 +0000811 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000812 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
813 return;
814 }
815
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000816 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000817}
818
Chris Lattner026dc962009-02-14 07:37:35 +0000819static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000820 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000821 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000822 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000823 return;
824 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000825
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000826 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000827 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000828 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000829 return;
830 }
831
Chris Lattner026dc962009-02-14 07:37:35 +0000832 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000833 if (!FD) {
834 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000835 << "dllexport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000836 return;
837 }
838
839 // Currently, the dllexport attribute is ignored for inlined functions,
840 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
841 if (FD->isInline()) {
842 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
844 return;
845 }
846
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000847 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000848}
849
Chris Lattner026dc962009-02-14 07:37:35 +0000850static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000851 // Attribute has no arguments.
852 if (Attr.getNumArgs() != 1) {
853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
854 return;
855 }
856
857 // Make sure that there is a string literal as the sections's single
858 // argument.
859 StringLiteral *SE =
860 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
861 if (!SE) {
862 // FIXME
863 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
864 return;
865 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000866 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
867 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000868}
869
Chris Lattner803d0802008-06-29 00:43:07 +0000870static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000871 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000872 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000874 return;
875 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000876
877 // Attribute can be applied only to functions.
878 if (!isa<FunctionDecl>(d)) {
879 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000880 << "stdcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000881 return;
882 }
883
884 // stdcall and fastcall attributes are mutually incompatible.
885 if (d->getAttr<FastCallAttr>()) {
886 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
887 << "stdcall" << "fastcall";
888 return;
889 }
890
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000891 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000892}
893
Chris Lattner803d0802008-06-29 00:43:07 +0000894static void HandleFastCallAttr(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 if (!isa<FunctionDecl>(d)) {
902 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000903 << "fastcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000904 return;
905 }
906
907 // stdcall and fastcall attributes are mutually incompatible.
908 if (d->getAttr<StdCallAttr>()) {
909 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
910 << "fastcall" << "stdcall";
911 return;
912 }
913
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000914 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000915}
916
Chris Lattner803d0802008-06-29 00:43:07 +0000917static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000918 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000919 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000920 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921 return;
922 }
923
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000924 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000925}
926
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000927static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
928 // check the attribute arguments.
929 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000930 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000931 return;
932 }
933
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000934 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000935}
936
937static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
938 // check the attribute arguments.
939 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000940 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000941 return;
942 }
943
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000944 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000945}
946
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000947static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000948 // Match gcc which ignores cleanup attrs when compiling C++.
949 if (S.getLangOptions().CPlusPlus)
950 return;
951
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000952 if (!Attr.getParameterName()) {
953 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
954 return;
955 }
956
957 if (Attr.getNumArgs() != 0) {
958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
959 return;
960 }
961
962 VarDecl *VD = dyn_cast<VarDecl>(d);
963
964 if (!VD || !VD->hasLocalStorage()) {
965 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
966 return;
967 }
968
969 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000970 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
971 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000972 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000973 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000974 Attr.getParameterName();
975 return;
976 }
977
978 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
979 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000980 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000981 Attr.getParameterName();
982 return;
983 }
984
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000985 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000986 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000987 Attr.getParameterName();
988 return;
989 }
990
Anders Carlsson89941c12009-02-07 23:16:50 +0000991 // We're currently more strict than GCC about what function types we accept.
992 // If this ever proves to be a problem it should be easy to fix.
993 QualType Ty = S.Context.getPointerType(VD->getType());
994 QualType ParamTy = FD->getParamDecl(0)->getType();
Anders Carlssonb90052a2009-02-25 17:19:08 +0000995 if (S.CheckAssignmentConstraints(Ty, ParamTy) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000996 S.Diag(Attr.getLoc(),
997 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
998 Attr.getParameterName() << ParamTy << Ty;
999 return;
1000 }
1001
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001002 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001003}
1004
Chris Lattner6b6b5372008-06-26 18:38:35 +00001005/// Handle __attribute__((format(type,idx,firstarg))) attributes
1006/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001007static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001008
Chris Lattner545dd342008-06-28 23:36:30 +00001009 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001011 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001012 return;
1013 }
1014
Chris Lattner545dd342008-06-28 23:36:30 +00001015 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001016 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001017 return;
1018 }
1019
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001020 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001021 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001022 << "format" << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001023 return;
1024 }
1025
1026 // FIXME: in C++ the implicit 'this' function parameter also counts.
1027 // this is needed in order to be compatible with GCC
1028 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001029 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001030 unsigned FirstIdx = 1;
1031
Chris Lattner545dd342008-06-28 23:36:30 +00001032 const char *Format = Attr.getParameterName()->getName();
1033 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001034
1035 // Normalize the argument, __foo__ becomes foo.
1036 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1037 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1038 Format += 2;
1039 FormatLen -= 4;
1040 }
1041
1042 bool Supported = false;
1043 bool is_NSString = false;
1044 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001045 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001046
1047 switch (FormatLen) {
1048 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001049 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1050 case 6: Supported = !memcmp(Format, "printf", 6); break;
1051 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001052 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001053 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1054 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1055 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001056 break;
1057 }
1058
1059 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001060 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1061 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001062 return;
1063 }
1064
1065 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001066 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001067 llvm::APSInt Idx(32);
1068 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001069 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001070 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001071 return;
1072 }
1073
1074 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001075 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001076 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001077 return;
1078 }
1079
1080 // FIXME: Do we need to bounds check?
1081 unsigned ArgIdx = Idx.getZExtValue() - 1;
1082
1083 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001084 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001085
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001086 if (is_CFString) {
1087 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001088 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1089 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001090 return;
1091 }
1092 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001093 // FIXME: do we need to check if the type is NSString*? What are
1094 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001095 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001096 // FIXME: Should highlight the actual expression that has the
1097 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001098 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1099 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001100 return;
1101 }
1102 } else if (!Ty->isPointerType() ||
1103 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1104 // FIXME: Should highlight the actual expression that has the
1105 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001106 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1107 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001108 return;
1109 }
1110
1111 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001112 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001113 llvm::APSInt FirstArg(32);
1114 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001115 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001116 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001117 return;
1118 }
1119
1120 // check if the function is variadic if the 3rd argument non-zero
1121 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001122 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001123 ++NumArgs; // +1 for ...
1124 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001125 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001126 return;
1127 }
1128 }
1129
Chris Lattner3c73c412008-11-19 08:23:25 +00001130 // strftime requires FirstArg to be 0 because it doesn't read from any
1131 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001132 if (is_strftime) {
1133 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001134 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1135 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001136 return;
1137 }
1138 // if 0 it disables parameter checking (to use with e.g. va_list)
1139 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001141 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001142 return;
1143 }
1144
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001145 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001146 Idx.getZExtValue(), FirstArg.getZExtValue()));
1147}
1148
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001149static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1150 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001151 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001152 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001153 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001154 return;
1155 }
1156
Eli Friedmanbc887452008-09-02 05:19:23 +00001157 // FIXME: This shouldn't be restricted to typedefs
1158 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1159 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001160 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001161 << "transparent_union" << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001162 return;
1163 }
1164
Eli Friedmanbc887452008-09-02 05:19:23 +00001165 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166
Eli Friedmanbc887452008-09-02 05:19:23 +00001167 // FIXME: Should we do a check for RD->isDefinition()?
1168
1169 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
1170 // we might silently generate incorrect code; see following code
Douglas Gregor44b43212008-12-11 16:49:14 +00001171 for (RecordDecl::field_iterator Field = RD->field_begin(),
1172 FieldEnd = RD->field_end();
1173 Field != FieldEnd; ++Field) {
1174 if (!Field->getType()->isPointerType()) {
Eli Friedmanbc887452008-09-02 05:19:23 +00001175 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
1176 return;
1177 }
1178 }
1179
1180 // FIXME: This is a complete hack; we should be properly propagating
1181 // transparent_union through Sema. That said, this is close enough to
1182 // correctly compile all the common cases of transparent_union without
1183 // errors or warnings
1184 QualType NewTy = S.Context.VoidPtrTy;
1185 NewTy.addConst();
1186 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001187}
1188
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001189static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001190 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001191 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001192 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001193 return;
1194 }
Chris Lattner545dd342008-06-28 23:36:30 +00001195 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001196 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1197
1198 // Make sure that there is a string literal as the annotation's single
1199 // argument.
1200 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001201 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001202 return;
1203 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001204 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1205 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001206}
1207
Chris Lattner803d0802008-06-29 00:43:07 +00001208static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001209 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001210 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001211 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001212 return;
1213 }
1214
1215 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001216 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001217 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001218 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001219 Align = 128;
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001220 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001221 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001222 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001223
1224 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1225 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001226 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1228 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001229 return;
1230 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001231 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1232 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1233 << alignmentExpr->getSourceRange();
1234 return;
1235 }
1236
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001237 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001238}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001239
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001240/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001241/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001242///
1243/// Despite what would be logical, the mode attribute is a decl attribute,
1244/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1245/// 'G' be HImode, not an intermediate pointer.
1246///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001247static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001248 // This attribute isn't documented, but glibc uses it. It changes
1249 // the width of an int or unsigned int to the specified size.
1250
1251 // Check that there aren't any arguments
1252 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001254 return;
1255 }
1256
1257 IdentifierInfo *Name = Attr.getParameterName();
1258 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001259 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001260 return;
1261 }
1262 const char *Str = Name->getName();
1263 unsigned Len = Name->getLength();
1264
1265 // Normalize the attribute name, __foo__ becomes foo.
1266 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1267 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1268 Str += 2;
1269 Len -= 4;
1270 }
1271
1272 unsigned DestWidth = 0;
1273 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001274 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001275 switch (Len) {
1276 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001277 switch (Str[0]) {
1278 case 'Q': DestWidth = 8; break;
1279 case 'H': DestWidth = 16; break;
1280 case 'S': DestWidth = 32; break;
1281 case 'D': DestWidth = 64; break;
1282 case 'X': DestWidth = 96; break;
1283 case 'T': DestWidth = 128; break;
1284 }
1285 if (Str[1] == 'F') {
1286 IntegerMode = false;
1287 } else if (Str[1] == 'C') {
1288 IntegerMode = false;
1289 ComplexMode = true;
1290 } else if (Str[1] != 'I') {
1291 DestWidth = 0;
1292 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001293 break;
1294 case 4:
1295 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1296 // pointer on PIC16 and other embedded platforms.
1297 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001298 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001299 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001300 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001301 break;
1302 case 7:
1303 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001304 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001305 break;
1306 }
1307
1308 QualType OldTy;
1309 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1310 OldTy = TD->getUnderlyingType();
1311 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1312 OldTy = VD->getType();
1313 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001314 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1315 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001316 return;
1317 }
Eli Friedman73397492009-03-03 06:41:03 +00001318
1319 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1320 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1321 else if (IntegerMode) {
1322 if (!OldTy->isIntegralType())
1323 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1324 } else if (ComplexMode) {
1325 if (!OldTy->isComplexType())
1326 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1327 } else {
1328 if (!OldTy->isFloatingType())
1329 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1330 }
1331
Eli Friedmanf98aba32009-02-13 02:31:07 +00001332 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1333 // int8_t and friends, at least with glibc.
1334 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1335 // the wrong width on unusual platforms.
1336 // FIXME: Make sure floating-point mappings are accurate
1337 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001338 QualType NewTy;
1339 switch (DestWidth) {
1340 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001341 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001342 return;
1343 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001344 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001345 return;
1346 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001347 if (!IntegerMode) {
1348 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1349 return;
1350 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001351 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001352 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001353 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001354 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001355 break;
1356 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001357 if (!IntegerMode) {
1358 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1359 return;
1360 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001361 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001362 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001363 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001364 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001365 break;
1366 case 32:
1367 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001368 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001369 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001370 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001371 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001372 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001373 break;
1374 case 64:
1375 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001376 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001377 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001378 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001379 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001380 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001381 break;
Eli Friedman73397492009-03-03 06:41:03 +00001382 case 96:
1383 NewTy = S.Context.LongDoubleTy;
1384 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001385 case 128:
1386 if (!IntegerMode) {
1387 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1388 return;
1389 }
1390 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001391 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001392 }
1393
Eli Friedman73397492009-03-03 06:41:03 +00001394 if (ComplexMode) {
1395 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001396 }
1397
1398 // Install the new type.
1399 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1400 TD->setUnderlyingType(NewTy);
1401 else
1402 cast<ValueDecl>(D)->setType(NewTy);
1403}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001404
Anders Carlssond87df372009-02-13 06:46:13 +00001405static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1406 // check the attribute arguments.
1407 if (Attr.getNumArgs() > 0) {
1408 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1409 return;
1410 }
Anders Carlssone896d982009-02-13 08:11:52 +00001411
Anders Carlsson5bab7882009-02-19 19:16:48 +00001412 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001413 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001414 << "nodebug" << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001415 return;
1416 }
1417
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001418 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001419}
1420
Anders Carlsson5bab7882009-02-19 19:16:48 +00001421static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1422 // check the attribute arguments.
1423 if (Attr.getNumArgs() != 0) {
1424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1425 return;
1426 }
1427
1428 if (!isFunctionOrMethod(d)) {
1429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1430 << "noinline" << 0 /*function*/;
1431 return;
1432 }
1433
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001434 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001435}
1436
Chris Lattner0744e5f2008-06-29 00:23:49 +00001437//===----------------------------------------------------------------------===//
1438// Top Level Sema Entry Points
1439//===----------------------------------------------------------------------===//
1440
Sebastian Redla89d82c2008-12-21 19:24:58 +00001441/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001442/// the attribute applies to decls. If the attribute is a type attribute, just
1443/// silently ignore it.
1444static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1445 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001446 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001447 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001448 case AttributeList::AT_objc_gc:
1449 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001450 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001451 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1452 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001453 case AttributeList::AT_always_inline:
1454 HandleAlwaysInlineAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001455 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1456 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1457 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1458 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1459 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1460 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001461 case AttributeList::AT_ext_vector_type:
1462 HandleExtVectorTypeAttr(D, Attr, S);
1463 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001464 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1465 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001466 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001467 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1468 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1469 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1470 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001471 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001472 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001473 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001474 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001475 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001476 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001477 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001478 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1479 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001480 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001481 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001482 case AttributeList::AT_transparent_union:
1483 HandleTransparentUnionAttr(D, Attr, S);
1484 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001485 case AttributeList::AT_objc_exception:
1486 HandleObjCExceptionAttr(D, Attr, S);
1487 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001488 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001489 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001490 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001491 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001492 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1493 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001494 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001495 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001496 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001497 case AttributeList::IgnoredAttribute:
1498 // Just ignore
1499 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001500 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001501 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001502 break;
1503 }
1504}
1505
1506/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1507/// attribute list to the specified decl, ignoring any type attributes.
1508void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1509 while (AttrList) {
1510 ProcessDeclAttribute(D, *AttrList, *this);
1511 AttrList = AttrList->getNext();
1512 }
1513}
1514
1515
Chris Lattner0744e5f2008-06-29 00:23:49 +00001516/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1517/// it, apply them to D. This is a bit tricky because PD can have attributes
1518/// specified in many different places, and we need to find and apply them all.
1519void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1520 // Apply decl attributes from the DeclSpec if present.
1521 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1522 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001523
Chris Lattner0744e5f2008-06-29 00:23:49 +00001524 // Walk the declarator structure, applying decl attributes that were in a type
1525 // position to the decl itself. This handles cases like:
1526 // int *__attr__(x)** D;
1527 // when X is a decl attribute.
1528 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1529 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1530 ProcessDeclAttributeList(D, Attrs);
1531
1532 // Finally, apply any attributes on the decl itself.
1533 if (const AttributeList *Attrs = PD.getAttributes())
1534 ProcessDeclAttributeList(D, Attrs);
1535}
1536