blob: 2ceea33d07d976c08605d1dff3a6ca98122a1d13 [file] [log] [blame]
Chris Lattner6953a072008-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 Dunbare0ad2152008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerdc789562008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek26151d12008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6953a072008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattner2024f0a2008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Daniel Dunbar0ea20472008-10-19 02:04:16 +000027static const FunctionType *getFunctionType(Decl *d) {
Chris Lattner6953a072008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6953a072008-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 Dunbar0ea20472008-10-19 02:04:16 +000040
41 return Ty->getAsFunctionType();
Chris Lattner6953a072008-06-26 18:38:35 +000042}
43
Daniel Dunbard1d847c2008-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 Dunbar0ea20472008-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 Dunbard1d847c2008-09-26 04:12:28 +000050static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000051 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000052}
53
Daniel Dunbar0ea20472008-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)) {
59 return isa<FunctionTypeProto>(FnTy);
60 } 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 Dunbard1d847c2008-09-26 04:12:28 +000069static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000070 if (const FunctionType *FnTy = getFunctionType(d)) {
71 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000072 return proto->getNumArgs();
73 } else {
74 return cast<ObjCMethodDecl>(d)->getNumParams();
75 }
76}
77
78static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000079 if (const FunctionType *FnTy = getFunctionType(d)) {
80 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000081 return proto->getArgType(Idx);
82 } else {
83 return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType();
84 }
85}
86
87static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000088 if (const FunctionType *FnTy = getFunctionType(d)) {
89 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000090 return proto->isVariadic();
91 } else {
92 return cast<ObjCMethodDecl>(d)->isVariadic();
93 }
94}
95
Chris Lattner6953a072008-06-26 18:38:35 +000096static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000097 const PointerType *PT = T->getAsPointerType();
98 if (!PT)
Chris Lattner6953a072008-06-26 18:38:35 +000099 return false;
100
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000101 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6953a072008-06-26 18:38:35 +0000102 if (!ClsT)
103 return false;
104
105 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
106
107 // FIXME: Should we walk the chain of classes?
108 return ClsName == &Ctx.Idents.get("NSString") ||
109 ClsName == &Ctx.Idents.get("NSMutableString");
110}
111
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000112static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
113 const PointerType *PT = T->getAsPointerType();
114 if (!PT)
115 return false;
116
117 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
118 if (!RT)
119 return false;
120
121 const RecordDecl *RD = RT->getDecl();
122 if (RD->getTagKind() != TagDecl::TK_struct)
123 return false;
124
125 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
126}
127
Chris Lattner2024f0a2008-06-29 00:16:31 +0000128//===----------------------------------------------------------------------===//
Chris Lattner2024f0a2008-06-29 00:16:31 +0000129// Attribute Implementations
130//===----------------------------------------------------------------------===//
131
Daniel Dunbar8716a012008-07-31 22:40:48 +0000132// FIXME: All this manual attribute parsing code is gross. At the
133// least add some helper functions to check most argument patterns (#
134// and types of args).
135
Chris Lattner703c52d2008-06-29 00:43:07 +0000136static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
137 Sema &S) {
Chris Lattner1c151132008-06-28 23:36:30 +0000138 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
139 if (tDecl == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000140 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner1c151132008-06-28 23:36:30 +0000141 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000142 }
143
Chris Lattner6953a072008-06-26 18:38:35 +0000144 QualType curType = tDecl->getUnderlyingType();
145 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000146 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000148 return;
149 }
Chris Lattner1c151132008-06-28 23:36:30 +0000150 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000151 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000152 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000153 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
154 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000155 return;
156 }
157 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
158 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000159 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000160 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6953a072008-06-26 18:38:35 +0000161 return;
162 }
163 // unlike gcc's vector_size attribute, the size is specified as the
164 // number of elements, not the number of bytes.
165 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
166
167 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
169 << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000170 return;
171 }
172 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner703c52d2008-06-29 00:43:07 +0000173 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6953a072008-06-26 18:38:35 +0000174 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner703c52d2008-06-29 00:43:07 +0000175 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6953a072008-06-26 18:38:35 +0000176}
177
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000178
179/// HandleVectorSizeAttribute - this attribute is only applicable to
180/// integral and float scalars, although arrays, pointers, and function
181/// return values are allowed in conjunction with this construct. Aggregates
182/// with this attribute are invalid, even if they are of the same size as a
183/// corresponding scalar.
184/// The raw attribute should contain precisely 1 argument, the vector size
185/// for the variable, measured in bytes. If curType and rawAttr are well
186/// formed, this routine will return a new vector type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000187static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000188 QualType CurType;
189 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
190 CurType = VD->getType();
191 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
192 CurType = TD->getUnderlyingType();
193 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000194 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
195 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000196 return;
197 }
198
199 // Check the attribute arugments.
Chris Lattner1c151132008-06-28 23:36:30 +0000200 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000202 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000203 }
Chris Lattner1c151132008-06-28 23:36:30 +0000204 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000205 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000206 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
208 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000209 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000210 }
211 // navigate to the base type - we need to provide for vector pointers,
212 // vector arrays, and functions returning vectors.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000213 if (CurType->isPointerType() || CurType->isArrayType() ||
214 CurType->isFunctionType()) {
Chris Lattner6953a072008-06-26 18:38:35 +0000215 assert(0 && "HandleVector(): Complex type construction unimplemented");
216 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
217 do {
218 if (PointerType *PT = dyn_cast<PointerType>(canonType))
219 canonType = PT->getPointeeType().getTypePtr();
220 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
221 canonType = AT->getElementType().getTypePtr();
222 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
223 canonType = FT->getResultType().getTypePtr();
224 } while (canonType->isPointerType() || canonType->isArrayType() ||
225 canonType->isFunctionType());
226 */
227 }
228 // the base type must be integer or float.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000229 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000231 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000232 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000233 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-06-26 18:38:35 +0000234 // vecSize is specified in bytes - convert to bits.
235 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
236
237 // the vector size needs to be an integral multiple of the type size.
238 if (vectorSize % typeSize) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
240 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000241 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000242 }
243 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000244 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
245 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000246 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000247 }
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000248
249 // Success! Instantiate the vector type, the number of elements is > 0, and
250 // not required to be a power of 2, unlike GCC.
Chris Lattner703c52d2008-06-29 00:43:07 +0000251 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000252
253 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
254 VD->setType(CurType);
255 else
256 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6953a072008-06-26 18:38:35 +0000257}
258
Chris Lattner703c52d2008-06-29 00:43:07 +0000259static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000260 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000261 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000262 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000263 return;
264 }
265
266 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000267 TD->addAttr(new PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000268 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
269 // If the alignment is less than or equal to 8 bits, the packed attribute
270 // has no effect.
271 if (!FD->getType()->isIncompleteType() &&
Chris Lattner703c52d2008-06-29 00:43:07 +0000272 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000274 << Attr.getName() << FD->getType();
Chris Lattner6953a072008-06-26 18:38:35 +0000275 else
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000276 FD->addAttr(new PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000277 } else
Chris Lattner65cae292008-11-19 08:23:25 +0000278 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000279}
280
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000281static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
282 // check the attribute arguments.
283 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000285 return;
286 }
287
288 // The IBOutlet attribute only applies to instance variables of Objective-C
289 // classes.
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000290 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
291 d->addAttr(new IBOutletAttr());
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000292 else
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000293 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000294}
295
Ted Kremenekc0af2542008-07-21 21:53:04 +0000296static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000297 // GCC ignores the nonnull attribute on K&R style function
298 // prototypes, so we ignore it as well
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000299 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000301 << "nonnull" << 0 /*function*/;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000302 return;
303 }
304
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000305 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000306
307 // The nonnull attribute only applies to pointers.
308 llvm::SmallVector<unsigned, 10> NonNullArgs;
309
310 for (AttributeList::arg_iterator I=Attr.arg_begin(),
311 E=Attr.arg_end(); I!=E; ++I) {
312
313
314 // The argument must be an integer constant expression.
Ted Kremenek73dc0652008-12-04 19:38:33 +0000315 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000316 llvm::APSInt ArgNum(32);
317 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000318 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
319 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000320 return;
321 }
322
323 unsigned x = (unsigned) ArgNum.getZExtValue();
324
325 if (x < 1 || x > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner68f621c2008-11-19 07:22:31 +0000327 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000328 return;
329 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000330
331 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000332
333 // Is the function argument a pointer type?
Ted Kremenekba57bd92008-11-18 06:52:58 +0000334 QualType T = getFunctionOrMethodArgType(d, x);
335 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000336 // FIXME: Should also highlight argument in decl.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7af441b2008-09-01 19:57:52 +0000339 continue;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000340 }
341
342 NonNullArgs.push_back(x);
343 }
344
Ted Kremenek7af441b2008-09-01 19:57:52 +0000345 // If no arguments were specified to __attribute__((nonnull)) then all
346 // pointer arguments have a nonnull attribute.
347 if (NonNullArgs.empty()) {
Ted Kremenekba57bd92008-11-18 06:52:58 +0000348 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
349 QualType T = getFunctionOrMethodArgType(d, I);
350 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000351 NonNullArgs.push_back(I);
Ted Kremenekba57bd92008-11-18 06:52:58 +0000352 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000353
354 if (NonNullArgs.empty()) {
355 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
356 return;
357 }
Ted Kremenekc0af2542008-07-21 21:53:04 +0000358 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000359
360 unsigned* start = &NonNullArgs[0];
361 unsigned size = NonNullArgs.size();
362 std::sort(start, start + size);
363 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekc0af2542008-07-21 21:53:04 +0000364}
365
Chris Lattner703c52d2008-06-29 00:43:07 +0000366static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000367 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000368 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000370 return;
371 }
372
Chris Lattner1c151132008-06-28 23:36:30 +0000373 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000374 Arg = Arg->IgnoreParenCasts();
375 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
376
377 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000378 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000379 << "alias" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000380 return;
381 }
382
383 const char *Alias = Str->getStrData();
384 unsigned AliasLen = Str->getByteLength();
385
386 // FIXME: check if target symbol exists in current file
387
388 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
389}
390
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000391static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
392 Sema &S) {
393 // check the attribute arguments.
394 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000396 return;
397 }
Anders Carlssoneb12e782009-02-19 19:16:48 +0000398
399 if (!isFunctionOrMethod(d)) {
400 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
401 << "always_inline" << 0 /*function*/;
402 return;
403 }
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000404
405 d->addAttr(new AlwaysInlineAttr());
406}
407
Chris Lattner703c52d2008-06-29 00:43:07 +0000408static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000409 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000410 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000411 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000412 return;
413 }
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000414
415 if (!isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000417 << "noreturn" << 0 /*function*/;
Chris Lattner6953a072008-06-26 18:38:35 +0000418 return;
419 }
420
421 d->addAttr(new NoReturnAttr());
422}
423
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000424static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
425 // check the attribute arguments.
426 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000427 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000428 return;
429 }
430
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000431 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000432 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000433 << "unused" << 2 /*variable and function*/;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000434 return;
435 }
436
437 d->addAttr(new UnusedAttr());
438}
439
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000440static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
441 // check the attribute arguments.
442 if (Attr.getNumArgs() != 0) {
443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
444 return;
445 }
446
447 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbard0cc5242009-02-13 22:48:56 +0000448 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000449 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
450 return;
451 }
452 } else if (!isFunctionOrMethod(d)) {
453 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000454 << "used" << 2 /*variable and function*/;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000455 return;
456 }
457
458 d->addAttr(new UsedAttr());
459}
460
Daniel Dunbar8716a012008-07-31 22:40:48 +0000461static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
462 // check the attribute arguments.
463 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000464 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
465 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000466 return;
467 }
468
469 int priority = 65535; // FIXME: Do not hardcode such constants.
470 if (Attr.getNumArgs() > 0) {
471 Expr *E = static_cast<Expr *>(Attr.getArg(0));
472 llvm::APSInt Idx(32);
473 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000474 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000475 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000476 return;
477 }
478 priority = Idx.getZExtValue();
479 }
480
481 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
482 if (!Fn) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000484 << "constructor" << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000485 return;
486 }
487
488 d->addAttr(new ConstructorAttr(priority));
489}
490
491static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
492 // check the attribute arguments.
493 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
495 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000496 return;
497 }
498
499 int priority = 65535; // FIXME: Do not hardcode such constants.
500 if (Attr.getNumArgs() > 0) {
501 Expr *E = static_cast<Expr *>(Attr.getArg(0));
502 llvm::APSInt Idx(32);
503 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000504 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000505 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000506 return;
507 }
508 priority = Idx.getZExtValue();
509 }
510
Anders Carlsson03e49652008-08-22 22:10:48 +0000511 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000512 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000513 << "destructor" << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000514 return;
515 }
516
517 d->addAttr(new DestructorAttr(priority));
518}
519
Chris Lattner703c52d2008-06-29 00:43:07 +0000520static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000521 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000522 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000524 return;
525 }
526
527 d->addAttr(new DeprecatedAttr());
528}
529
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000530static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
531 // check the attribute arguments.
532 if (Attr.getNumArgs() != 0) {
533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
534 return;
535 }
536
537 d->addAttr(new UnavailableAttr());
538}
539
Chris Lattner703c52d2008-06-29 00:43:07 +0000540static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000541 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000542 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000544 return;
545 }
546
Chris Lattner1c151132008-06-28 23:36:30 +0000547 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000548 Arg = Arg->IgnoreParenCasts();
549 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
550
551 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000553 << "visibility" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000554 return;
555 }
556
557 const char *TypeStr = Str->getStrData();
558 unsigned TypeLen = Str->getByteLength();
559 VisibilityAttr::VisibilityTypes type;
560
561 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
562 type = VisibilityAttr::DefaultVisibility;
563 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
564 type = VisibilityAttr::HiddenVisibility;
565 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
566 type = VisibilityAttr::HiddenVisibility; // FIXME
567 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
568 type = VisibilityAttr::ProtectedVisibility;
569 else {
Chris Lattnerb1753422008-11-23 21:45:46 +0000570 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6953a072008-06-26 18:38:35 +0000571 return;
572 }
573
574 d->addAttr(new VisibilityAttr(type));
575}
576
Chris Lattnera9b98462009-02-14 08:09:34 +0000577static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
578 Sema &S) {
579 if (Attr.getNumArgs() != 0) {
580 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
581 return;
582 }
583
584 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
585 if (OCI == 0) {
586 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
587 return;
588 }
589
590 D->addAttr(new ObjCExceptionAttr());
591}
592
593static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000594 if (Attr.getNumArgs() != 0) {
595 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
596 return;
597 }
Chris Lattnera9b98462009-02-14 08:09:34 +0000598 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000599 QualType T = TD->getUnderlyingType();
600 if (!T->isPointerType() ||
601 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
602 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
603 return;
604 }
605 }
Chris Lattnera9b98462009-02-14 08:09:34 +0000606 D->addAttr(new ObjCNSObjectAttr);
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000607}
608
Douglas Gregorfcb19192009-02-11 23:02:49 +0000609static void
610HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
611 if (Attr.getNumArgs() != 0) {
612 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
613 return;
614 }
615
616 if (!isa<FunctionDecl>(D)) {
617 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
618 return;
619 }
620
621 D->addAttr(new OverloadableAttr);
622}
623
Steve Naroffe1cecba2008-09-18 16:44:58 +0000624static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
625 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000626 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000627 << "blocks" << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000628 return;
629 }
630
631 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000632 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000633 return;
634 }
Steve Naroffe1cecba2008-09-18 16:44:58 +0000635
636 BlocksAttr::BlocksAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000637 if (Attr.getParameterName()->isStr("byref"))
Steve Naroffe1cecba2008-09-18 16:44:58 +0000638 type = BlocksAttr::ByRef;
639 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000640 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000641 << "blocks" << Attr.getParameterName();
Steve Naroffe1cecba2008-09-18 16:44:58 +0000642 return;
643 }
644
645 d->addAttr(new BlocksAttr(type));
646}
647
Anders Carlsson244d99b2008-10-05 18:05:59 +0000648static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
649 // check the attribute arguments.
650 if (Attr.getNumArgs() > 2) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
652 << "0, 1 or 2";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000653 return;
654 }
655
656 int sentinel = 0;
657 if (Attr.getNumArgs() > 0) {
658 Expr *E = static_cast<Expr *>(Attr.getArg(0));
659 llvm::APSInt Idx(32);
660 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000662 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000663 return;
664 }
665 sentinel = Idx.getZExtValue();
666
667 if (sentinel < 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000668 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
669 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000670 return;
671 }
672 }
673
674 int nullPos = 0;
675 if (Attr.getNumArgs() > 1) {
676 Expr *E = static_cast<Expr *>(Attr.getArg(1));
677 llvm::APSInt Idx(32);
678 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000679 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000680 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000681 return;
682 }
683 nullPos = Idx.getZExtValue();
684
685 if (nullPos > 1 || nullPos < 0) {
686 // FIXME: This error message could be improved, it would be nice
687 // to say what the bounds actually are.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000688 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
689 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000690 return;
691 }
692 }
693
694 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
695 QualType FT = FD->getType();
696 if (!FT->getAsFunctionTypeProto()->isVariadic()) {
697 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
698 return;
699 }
700 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
701 if (!MD->isVariadic()) {
702 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
703 return;
704 }
705 } else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000707 << "sentinel" << 3 /*function or method*/;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000708 return;
709 }
710
711 // FIXME: Actually create the attribute.
712}
713
Chris Lattnerd2c66552009-02-14 07:37:35 +0000714static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
715 // check the attribute arguments.
716 if (Attr.getNumArgs() != 0) {
717 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
718 return;
719 }
720
721 // TODO: could also be applied to methods?
722 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
723 if (!Fn) {
724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
725 << "warn_unused_result" << 0 /*function*/;
726 return;
727 }
728
729 Fn->addAttr(new WarnUnusedResultAttr());
730}
731
732static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000733 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000734 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000735 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000736 return;
737 }
738
Chris Lattnerd2c66552009-02-14 07:37:35 +0000739 D->addAttr(new WeakAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000740}
741
Chris Lattnerd2c66552009-02-14 07:37:35 +0000742static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000743 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000744 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000745 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000746 return;
747 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000748
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000749 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000750 if (isa<VarDecl>(D)) {
751 D->addAttr(new DLLImportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000752 return;
753 }
754
Chris Lattnerd2c66552009-02-14 07:37:35 +0000755 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000756 if (!FD) {
757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000758 << "dllimport" << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000759 return;
760 }
761
762 // Currently, the dllimport attribute is ignored for inlined functions.
763 // Warning is emitted.
764 if (FD->isInline()) {
765 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
766 return;
767 }
768
769 // The attribute is also overridden by a subsequent declaration as dllexport.
770 // Warning is emitted.
771 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
772 nextAttr = nextAttr->getNext()) {
773 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
774 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
775 return;
776 }
777 }
778
Chris Lattnerd2c66552009-02-14 07:37:35 +0000779 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000780 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
781 return;
782 }
783
Chris Lattnerd2c66552009-02-14 07:37:35 +0000784 D->addAttr(new DLLImportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000785}
786
Chris Lattnerd2c66552009-02-14 07:37:35 +0000787static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000788 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000789 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000791 return;
792 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000793
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000794 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000795 if (isa<VarDecl>(D)) {
796 D->addAttr(new DLLExportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000797 return;
798 }
799
Chris Lattnerd2c66552009-02-14 07:37:35 +0000800 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000801 if (!FD) {
802 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000803 << "dllexport" << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000804 return;
805 }
806
807 // Currently, the dllexport attribute is ignored for inlined functions,
808 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
809 if (FD->isInline()) {
810 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
811 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
812 return;
813 }
814
Chris Lattnerd2c66552009-02-14 07:37:35 +0000815 D->addAttr(new DLLExportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000816}
817
Chris Lattnerd2c66552009-02-14 07:37:35 +0000818static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar97509722009-02-12 17:28:23 +0000819 // Attribute has no arguments.
820 if (Attr.getNumArgs() != 1) {
821 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
822 return;
823 }
824
825 // Make sure that there is a string literal as the sections's single
826 // argument.
827 StringLiteral *SE =
828 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
829 if (!SE) {
830 // FIXME
831 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
832 return;
833 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000834 D->addAttr(new SectionAttr(std::string(SE->getStrData(),
Daniel Dunbar97509722009-02-12 17:28:23 +0000835 SE->getByteLength())));
836}
837
Chris Lattner703c52d2008-06-29 00:43:07 +0000838static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000839 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000840 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000841 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000842 return;
843 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000844
845 // Attribute can be applied only to functions.
846 if (!isa<FunctionDecl>(d)) {
847 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000848 << "stdcall" << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000849 return;
850 }
851
852 // stdcall and fastcall attributes are mutually incompatible.
853 if (d->getAttr<FastCallAttr>()) {
854 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
855 << "stdcall" << "fastcall";
856 return;
857 }
858
Chris Lattner6953a072008-06-26 18:38:35 +0000859 d->addAttr(new StdCallAttr());
860}
861
Chris Lattner703c52d2008-06-29 00:43:07 +0000862static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000863 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000864 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000865 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000866 return;
867 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000868
869 if (!isa<FunctionDecl>(d)) {
870 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000871 << "fastcall" << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000872 return;
873 }
874
875 // stdcall and fastcall attributes are mutually incompatible.
876 if (d->getAttr<StdCallAttr>()) {
877 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
878 << "fastcall" << "stdcall";
879 return;
880 }
881
Chris Lattner6953a072008-06-26 18:38:35 +0000882 d->addAttr(new FastCallAttr());
883}
884
Chris Lattner703c52d2008-06-29 00:43:07 +0000885static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000886 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000887 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000889 return;
890 }
891
892 d->addAttr(new NoThrowAttr());
893}
894
Anders Carlssondd6791c2008-10-05 23:32:53 +0000895static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
896 // check the attribute arguments.
897 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000898 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000899 return;
900 }
901
902 d->addAttr(new ConstAttr());
903}
904
905static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
906 // check the attribute arguments.
907 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000908 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000909 return;
910 }
911
912 d->addAttr(new PureAttr());
913}
914
Anders Carlsson677e38f2009-01-31 01:16:18 +0000915static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000916 // Match gcc which ignores cleanup attrs when compiling C++.
917 if (S.getLangOptions().CPlusPlus)
918 return;
919
Anders Carlsson677e38f2009-01-31 01:16:18 +0000920 if (!Attr.getParameterName()) {
921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
922 return;
923 }
924
925 if (Attr.getNumArgs() != 0) {
926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
927 return;
928 }
929
930 VarDecl *VD = dyn_cast<VarDecl>(d);
931
932 if (!VD || !VD->hasLocalStorage()) {
933 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
934 return;
935 }
936
937 // Look up the function
Douglas Gregor09be81b2009-02-04 17:27:36 +0000938 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
939 Sema::LookupOrdinaryName);
Anders Carlsson677e38f2009-01-31 01:16:18 +0000940 if (!CleanupDecl) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000941 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +0000942 Attr.getParameterName();
943 return;
944 }
945
946 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
947 if (!FD) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000948 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +0000949 Attr.getParameterName();
950 return;
951 }
952
Anders Carlsson677e38f2009-01-31 01:16:18 +0000953 if (FD->getNumParams() != 1) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000954 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +0000955 Attr.getParameterName();
956 return;
957 }
958
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000959 // We're currently more strict than GCC about what function types we accept.
960 // If this ever proves to be a problem it should be easy to fix.
961 QualType Ty = S.Context.getPointerType(VD->getType());
962 QualType ParamTy = FD->getParamDecl(0)->getType();
963 if (Ty != ParamTy) {
964 S.Diag(Attr.getLoc(),
965 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
966 Attr.getParameterName() << ParamTy << Ty;
967 return;
968 }
969
Anders Carlsson677e38f2009-01-31 01:16:18 +0000970 d->addAttr(new CleanupAttr(FD));
971}
972
Chris Lattner6953a072008-06-26 18:38:35 +0000973/// Handle __attribute__((format(type,idx,firstarg))) attributes
974/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +0000975static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000976
Chris Lattner1c151132008-06-28 23:36:30 +0000977 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000978 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000979 << "format" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000980 return;
981 }
982
Chris Lattner1c151132008-06-28 23:36:30 +0000983 if (Attr.getNumArgs() != 2) {
Chris Lattner65cae292008-11-19 08:23:25 +0000984 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6953a072008-06-26 18:38:35 +0000985 return;
986 }
987
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000988 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000989 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000990 << "format" << 0 /*function*/;
Chris Lattner6953a072008-06-26 18:38:35 +0000991 return;
992 }
993
994 // FIXME: in C++ the implicit 'this' function parameter also counts.
995 // this is needed in order to be compatible with GCC
996 // the index must start in 1 and the limit is numargs+1
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000997 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6953a072008-06-26 18:38:35 +0000998 unsigned FirstIdx = 1;
999
Chris Lattner1c151132008-06-28 23:36:30 +00001000 const char *Format = Attr.getParameterName()->getName();
1001 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +00001002
1003 // Normalize the argument, __foo__ becomes foo.
1004 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1005 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1006 Format += 2;
1007 FormatLen -= 4;
1008 }
1009
1010 bool Supported = false;
1011 bool is_NSString = false;
1012 bool is_strftime = false;
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001013 bool is_CFString = false;
Chris Lattner6953a072008-06-26 18:38:35 +00001014
1015 switch (FormatLen) {
1016 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001017 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1018 case 6: Supported = !memcmp(Format, "printf", 6); break;
1019 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +00001020 case 8:
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001021 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1022 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1023 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001024 break;
1025 }
1026
1027 if (!Supported) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001028 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1029 << "format" << Attr.getParameterName()->getName();
Chris Lattner6953a072008-06-26 18:38:35 +00001030 return;
1031 }
1032
1033 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001034 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +00001035 llvm::APSInt Idx(32);
1036 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001037 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001038 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001039 return;
1040 }
1041
1042 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001043 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001044 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001045 return;
1046 }
1047
1048 // FIXME: Do we need to bounds check?
1049 unsigned ArgIdx = Idx.getZExtValue() - 1;
1050
1051 // make sure the format string is really a string
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001052 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6953a072008-06-26 18:38:35 +00001053
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001054 if (is_CFString) {
1055 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001056 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1057 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001058 return;
1059 }
1060 } else if (is_NSString) {
Chris Lattner6953a072008-06-26 18:38:35 +00001061 // FIXME: do we need to check if the type is NSString*? What are
1062 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +00001063 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001064 // FIXME: Should highlight the actual expression that has the
1065 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001066 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1067 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001068 return;
1069 }
1070 } else if (!Ty->isPointerType() ||
1071 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1072 // FIXME: Should highlight the actual expression that has the
1073 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001074 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1075 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001076 return;
1077 }
1078
1079 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001080 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +00001081 llvm::APSInt FirstArg(32);
1082 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001083 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001084 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001085 return;
1086 }
1087
1088 // check if the function is variadic if the 3rd argument non-zero
1089 if (FirstArg != 0) {
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001090 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001091 ++NumArgs; // +1 for ...
1092 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +00001093 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +00001094 return;
1095 }
1096 }
1097
Chris Lattner65cae292008-11-19 08:23:25 +00001098 // strftime requires FirstArg to be 0 because it doesn't read from any
1099 // variable the input is just the current time + the format string.
Chris Lattner6953a072008-06-26 18:38:35 +00001100 if (is_strftime) {
1101 if (FirstArg != 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001102 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1103 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001104 return;
1105 }
1106 // if 0 it disables parameter checking (to use with e.g. va_list)
1107 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001108 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001109 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001110 return;
1111 }
1112
1113 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
1114 Idx.getZExtValue(), FirstArg.getZExtValue()));
1115}
1116
Chris Lattnerf6690152008-06-29 00:28:59 +00001117static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1118 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001119 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001120 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001121 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +00001122 return;
1123 }
1124
Eli Friedman283b3622008-09-02 05:19:23 +00001125 // FIXME: This shouldn't be restricted to typedefs
1126 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1127 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001128 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +00001129 << "transparent_union" << 1 /*union*/;
Chris Lattner6953a072008-06-26 18:38:35 +00001130 return;
1131 }
1132
Eli Friedman283b3622008-09-02 05:19:23 +00001133 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6953a072008-06-26 18:38:35 +00001134
Eli Friedman283b3622008-09-02 05:19:23 +00001135 // FIXME: Should we do a check for RD->isDefinition()?
1136
1137 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
1138 // we might silently generate incorrect code; see following code
Douglas Gregor8acb7272008-12-11 16:49:14 +00001139 for (RecordDecl::field_iterator Field = RD->field_begin(),
1140 FieldEnd = RD->field_end();
1141 Field != FieldEnd; ++Field) {
1142 if (!Field->getType()->isPointerType()) {
Eli Friedman283b3622008-09-02 05:19:23 +00001143 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
1144 return;
1145 }
1146 }
1147
1148 // FIXME: This is a complete hack; we should be properly propagating
1149 // transparent_union through Sema. That said, this is close enough to
1150 // correctly compile all the common cases of transparent_union without
1151 // errors or warnings
1152 QualType NewTy = S.Context.VoidPtrTy;
1153 NewTy.addConst();
1154 TD->setUnderlyingType(NewTy);
Chris Lattner6953a072008-06-26 18:38:35 +00001155}
1156
Chris Lattnerf6690152008-06-29 00:28:59 +00001157static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001158 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001159 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001160 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001161 return;
1162 }
Chris Lattner1c151132008-06-28 23:36:30 +00001163 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +00001164 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1165
1166 // Make sure that there is a string literal as the annotation's single
1167 // argument.
1168 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001169 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +00001170 return;
1171 }
1172 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
1173 SE->getByteLength())));
1174}
1175
Chris Lattner703c52d2008-06-29 00:43:07 +00001176static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001177 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001178 if (Attr.getNumArgs() > 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001179 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001180 return;
1181 }
1182
1183 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +00001184 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +00001185 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbare892c042009-02-18 20:06:09 +00001186 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6953a072008-06-26 18:38:35 +00001187 Align = 128;
Daniel Dunbare892c042009-02-18 20:06:09 +00001188 d->addAttr(new AlignedAttr(Align));
Chris Lattner6953a072008-06-26 18:38:35 +00001189 return;
Chris Lattner6953a072008-06-26 18:38:35 +00001190 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001191
1192 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1193 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +00001194 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001195 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1196 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001197 return;
1198 }
Daniel Dunbar3ed413a2009-02-16 23:37:57 +00001199 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1200 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1201 << alignmentExpr->getSourceRange();
1202 return;
1203 }
1204
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001205 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001206}
Chris Lattnerdc789562008-06-27 22:18:37 +00001207
Chris Lattnerf6690152008-06-29 00:28:59 +00001208/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +00001209/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +00001210///
1211/// Despite what would be logical, the mode attribute is a decl attribute,
1212/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1213/// 'G' be HImode, not an intermediate pointer.
1214///
Chris Lattnerf6690152008-06-29 00:28:59 +00001215static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +00001216 // This attribute isn't documented, but glibc uses it. It changes
1217 // the width of an int or unsigned int to the specified size.
1218
1219 // Check that there aren't any arguments
1220 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001221 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerdc789562008-06-27 22:18:37 +00001222 return;
1223 }
1224
1225 IdentifierInfo *Name = Attr.getParameterName();
1226 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +00001228 return;
1229 }
1230 const char *Str = Name->getName();
1231 unsigned Len = Name->getLength();
1232
1233 // Normalize the attribute name, __foo__ becomes foo.
1234 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1235 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1236 Str += 2;
1237 Len -= 4;
1238 }
1239
1240 unsigned DestWidth = 0;
1241 bool IntegerMode = true;
1242 switch (Len) {
1243 case 2:
1244 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
1245 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
1246 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
1247 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
1248 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
1249 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
1250 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
1251 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
1252 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
1253 break;
1254 case 4:
1255 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1256 // pointer on PIC16 and other embedded platforms.
1257 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001258 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001259 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001260 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +00001261 break;
1262 case 7:
1263 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +00001264 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001265 break;
1266 }
1267
1268 QualType OldTy;
1269 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1270 OldTy = TD->getUnderlyingType();
1271 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1272 OldTy = VD->getType();
1273 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001274 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1275 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerdc789562008-06-27 22:18:37 +00001276 return;
1277 }
1278
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001279 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1280 // int8_t and friends, at least with glibc.
1281 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1282 // the wrong width on unusual platforms.
1283 // FIXME: Make sure floating-point mappings are accurate
1284 // FIXME: Support XF and TF types
Chris Lattnerdc789562008-06-27 22:18:37 +00001285 QualType NewTy;
1286 switch (DestWidth) {
1287 case 0:
Chris Lattner65cae292008-11-19 08:23:25 +00001288 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001289 return;
1290 default:
Chris Lattner65cae292008-11-19 08:23:25 +00001291 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001292 return;
1293 case 8:
1294 assert(IntegerMode);
1295 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001296 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001297 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001298 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001299 break;
1300 case 16:
1301 assert(IntegerMode);
1302 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001303 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001304 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001305 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001306 break;
1307 case 32:
1308 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001309 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001310 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001311 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001312 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001313 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001314 break;
1315 case 64:
1316 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001317 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001318 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001319 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001320 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001321 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001322 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001323 case 128:
1324 if (!IntegerMode) {
1325 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1326 return;
1327 }
1328 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Chris Lattnerdc789562008-06-27 22:18:37 +00001329 }
1330
1331 if (!OldTy->getAsBuiltinType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001332 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerdc789562008-06-27 22:18:37 +00001333 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1334 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001335 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerdc789562008-06-27 22:18:37 +00001336 }
1337
1338 // Install the new type.
1339 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1340 TD->setUnderlyingType(NewTy);
1341 else
1342 cast<ValueDecl>(D)->setType(NewTy);
1343}
Chris Lattnera72440d2008-06-29 00:23:49 +00001344
Anders Carlssonf607f532009-02-13 06:46:13 +00001345static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1346 // check the attribute arguments.
1347 if (Attr.getNumArgs() > 0) {
1348 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1349 return;
1350 }
Anders Carlsson73007792009-02-13 08:11:52 +00001351
Anders Carlssoneb12e782009-02-19 19:16:48 +00001352 if (!isFunctionOrMethod(d)) {
Anders Carlssonf607f532009-02-13 06:46:13 +00001353 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +00001354 << "nodebug" << 0 /*function*/;
Anders Carlssonf607f532009-02-13 06:46:13 +00001355 return;
1356 }
1357
1358 d->addAttr(new NodebugAttr());
1359}
1360
Anders Carlssoneb12e782009-02-19 19:16:48 +00001361static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1362 // check the attribute arguments.
1363 if (Attr.getNumArgs() != 0) {
1364 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1365 return;
1366 }
1367
1368 if (!isFunctionOrMethod(d)) {
1369 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1370 << "noinline" << 0 /*function*/;
1371 return;
1372 }
1373
1374 d->addAttr(new NoinlineAttr());
1375}
1376
Chris Lattnera72440d2008-06-29 00:23:49 +00001377//===----------------------------------------------------------------------===//
1378// Top Level Sema Entry Points
1379//===----------------------------------------------------------------------===//
1380
Sebastian Redl6e67d9b2008-12-21 19:24:58 +00001381/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner703c52d2008-06-29 00:43:07 +00001382/// the attribute applies to decls. If the attribute is a type attribute, just
1383/// silently ignore it.
1384static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1385 switch (Attr.getKind()) {
Daniel Dunbar8716a012008-07-31 22:40:48 +00001386 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001387 case AttributeList::AT_address_space:
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +00001388 case AttributeList::AT_objc_gc:
1389 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner703c52d2008-06-29 00:43:07 +00001390 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001391 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1392 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbar0a2da712008-10-28 00:17:57 +00001393 case AttributeList::AT_always_inline:
1394 HandleAlwaysInlineAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001395 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1396 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1397 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1398 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1399 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1400 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001401 case AttributeList::AT_ext_vector_type:
1402 HandleExtVectorTypeAttr(D, Attr, S);
1403 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001404 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1405 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001406 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001407 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1408 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1409 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1410 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar97509722009-02-12 17:28:23 +00001411 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001412 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +00001413 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001414 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +00001415 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001416 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001417 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattnerd2c66552009-02-14 07:37:35 +00001418 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1419 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001420 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001421 case AttributeList::AT_transparent_union:
1422 HandleTransparentUnionAttr(D, Attr, S);
1423 break;
Chris Lattnera9b98462009-02-14 08:09:34 +00001424 case AttributeList::AT_objc_exception:
1425 HandleObjCExceptionAttr(D, Attr, S);
1426 break;
Douglas Gregorfcb19192009-02-11 23:02:49 +00001427 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanian82f54962009-01-13 23:34:40 +00001428 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroffe1cecba2008-09-18 16:44:58 +00001429 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson244d99b2008-10-05 18:05:59 +00001430 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001431 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1432 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlsson677e38f2009-01-31 01:16:18 +00001433 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssonf607f532009-02-13 06:46:13 +00001434 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlssoneb12e782009-02-19 19:16:48 +00001435 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Anders Carlsson402735d2009-02-13 08:16:43 +00001436 case AttributeList::IgnoredAttribute:
1437 // Just ignore
1438 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001439 default:
Anders Carlssonf607f532009-02-13 06:46:13 +00001440 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner703c52d2008-06-29 00:43:07 +00001441 break;
1442 }
1443}
1444
1445/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1446/// attribute list to the specified decl, ignoring any type attributes.
1447void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1448 while (AttrList) {
1449 ProcessDeclAttribute(D, *AttrList, *this);
1450 AttrList = AttrList->getNext();
1451 }
1452}
1453
1454
Chris Lattnera72440d2008-06-29 00:23:49 +00001455/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1456/// it, apply them to D. This is a bit tricky because PD can have attributes
1457/// specified in many different places, and we need to find and apply them all.
1458void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1459 // Apply decl attributes from the DeclSpec if present.
1460 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1461 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +00001462
Chris Lattnera72440d2008-06-29 00:23:49 +00001463 // Walk the declarator structure, applying decl attributes that were in a type
1464 // position to the decl itself. This handles cases like:
1465 // int *__attr__(x)** D;
1466 // when X is a decl attribute.
1467 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1468 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1469 ProcessDeclAttributeList(D, Attrs);
1470
1471 // Finally, apply any attributes on the decl itself.
1472 if (const AttributeList *Attrs = PD.getAttributes())
1473 ProcessDeclAttributeList(D, Attrs);
1474}
1475