blob: 347d8f5e0739f978e2a0d3b519583e3cfe0e2983 [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)) {
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 Dunbar35682492008-09-26 04:12:28 +000069static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070 if (const FunctionType *FnTy = getFunctionType(d)) {
71 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-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 Dunbard3f2c102008-10-19 02:04:16 +000079 if (const FunctionType *FnTy = getFunctionType(d)) {
80 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-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 Dunbard3f2c102008-10-19 02:04:16 +000088 if (const FunctionType *FnTy = getFunctionType(d)) {
89 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000090 return proto->isVariadic();
91 } else {
92 return cast<ObjCMethodDecl>(d)->isVariadic();
93 }
94}
95
Chris Lattner6b6b5372008-06-26 18:38:35 +000096static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000097 const PointerType *PT = T->getAsPointerType();
98 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000099 return false;
100
Chris Lattnerb77792e2008-07-26 22:17:49 +0000101 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-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 Dunbar085e8f72008-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 Lattnere5c5ee12008-06-29 00:16:31 +0000128//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000129// Attribute Implementations
130//===----------------------------------------------------------------------===//
131
Daniel Dunbar3068ae02008-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 Lattner803d0802008-06-29 00:43:07 +0000136static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
137 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000138 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
139 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000140 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000141 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000142 }
143
Chris Lattner6b6b5372008-06-26 18:38:35 +0000144 QualType curType = tDecl->getUnderlyingType();
145 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000146 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000148 return;
149 }
Chris Lattner545dd342008-06-28 23:36:30 +0000150 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000151 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000152 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000153 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
154 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-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 Lattnerb77792e2008-07-26 22:17:49 +0000159 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000160 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6b6b5372008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
169 << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return;
171 }
172 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000173 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000175 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176}
177
Chris Lattner065c5a82008-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 Lattner803d0802008-06-29 00:43:07 +0000187static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000194 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
195 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000196 return;
197 }
198
199 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000200 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000202 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner545dd342008-06-28 23:36:30 +0000204 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000206 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
208 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000209 return;
Chris Lattner6b6b5372008-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 Lattnerb77792e2008-07-26 22:17:49 +0000213 if (CurType->isPointerType() || CurType->isArrayType() ||
214 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-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 Lattnerb77792e2008-07-26 22:17:49 +0000229 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000231 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000232 }
Chris Lattner803d0802008-06-29 00:43:07 +0000233 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
240 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000241 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000242 }
243 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000244 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
245 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000246 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000247 }
Chris Lattner065c5a82008-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 Lattner803d0802008-06-29 00:43:07 +0000251 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-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 Lattner6b6b5372008-06-26 18:38:35 +0000257}
258
Chris Lattner803d0802008-06-29 00:43:07 +0000259static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000260 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000261 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000262 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000263 return;
264 }
265
266 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000267 TD->addAttr(new PackedAttr(1));
Chris Lattner6b6b5372008-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 Lattner803d0802008-06-29 00:43:07 +0000272 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000274 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000275 else
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000276 FD->addAttr(new PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000277 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000278 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000279}
280
Ted Kremenek96329d42008-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 Lattner3c73c412008-11-19 08:23:25 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000285 return;
286 }
287
288 // The IBOutlet attribute only applies to instance variables of Objective-C
289 // classes.
290 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
291 ID->addAttr(new IBOutletAttr());
292 else
293 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
294}
295
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000296static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-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 Dunbard3f2c102008-10-19 02:04:16 +0000299 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000301 << "nonnull" << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000302 return;
303 }
304
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000305 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-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 Kremenekf5e88342008-12-04 19:38:33 +0000315 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000316 llvm::APSInt ArgNum(32);
317 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000318 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
319 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000320 return;
321 }
322
323 unsigned x = (unsigned) ArgNum.getZExtValue();
324
325 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000327 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000328 return;
329 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000330
331 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332
333 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000334 QualType T = getFunctionOrMethodArgType(d, x);
335 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000339 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 }
341
342 NonNullArgs.push_back(x);
343 }
344
Ted Kremenek7fb43c12008-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 Kremenek46bbaca2008-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 Dunbard3f2c102008-10-19 02:04:16 +0000351 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000352 }
Ted Kremenek7fb43c12008-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 Kremenekeb2b2a32008-07-21 21:53:04 +0000358 }
Ted Kremenek7fb43c12008-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 Kremenekeb2b2a32008-07-21 21:53:04 +0000364}
365
Chris Lattner803d0802008-06-29 00:43:07 +0000366static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000367 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000368 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000370 return;
371 }
372
Chris Lattner545dd342008-06-28 23:36:30 +0000373 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000374 Arg = Arg->IgnoreParenCasts();
375 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
376
377 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000378 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000379 << "alias" << 1;
Chris Lattner6b6b5372008-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 Dunbaraf668b02008-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 Lattner3c73c412008-11-19 08:23:25 +0000395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000396 return;
397 }
398
399 d->addAttr(new AlwaysInlineAttr());
400}
401
Chris Lattner803d0802008-06-29 00:43:07 +0000402static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000403 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000404 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000405 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000406 return;
407 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000408
409 if (!isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000411 << "noreturn" << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000412 return;
413 }
414
415 d->addAttr(new NoReturnAttr());
416}
417
Ted Kremenek73798892008-07-25 04:39:19 +0000418static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
419 // check the attribute arguments.
420 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000422 return;
423 }
424
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000425 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000426 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000427 << "unused" << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000428 return;
429 }
430
431 d->addAttr(new UnusedAttr());
432}
433
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000434static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
435 // check the attribute arguments.
436 if (Attr.getNumArgs() != 0) {
437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
438 return;
439 }
440
441 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000442 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000443 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
444 return;
445 }
446 } else if (!isFunctionOrMethod(d)) {
447 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000448 << "used" << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000449 return;
450 }
451
452 d->addAttr(new UsedAttr());
453}
454
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000455static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
456 // check the attribute arguments.
457 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
459 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000460 return;
461 }
462
463 int priority = 65535; // FIXME: Do not hardcode such constants.
464 if (Attr.getNumArgs() > 0) {
465 Expr *E = static_cast<Expr *>(Attr.getArg(0));
466 llvm::APSInt Idx(32);
467 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000468 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000469 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000470 return;
471 }
472 priority = Idx.getZExtValue();
473 }
474
475 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
476 if (!Fn) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000477 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000478 << "constructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000479 return;
480 }
481
482 d->addAttr(new ConstructorAttr(priority));
483}
484
485static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
486 // check the attribute arguments.
487 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
489 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000490 return;
491 }
492
493 int priority = 65535; // FIXME: Do not hardcode such constants.
494 if (Attr.getNumArgs() > 0) {
495 Expr *E = static_cast<Expr *>(Attr.getArg(0));
496 llvm::APSInt Idx(32);
497 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000498 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000499 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000500 return;
501 }
502 priority = Idx.getZExtValue();
503 }
504
Anders Carlsson6782fc62008-08-22 22:10:48 +0000505 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000506 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000507 << "destructor" << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000508 return;
509 }
510
511 d->addAttr(new DestructorAttr(priority));
512}
513
Chris Lattner803d0802008-06-29 00:43:07 +0000514static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000515 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000516 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000517 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000518 return;
519 }
520
521 d->addAttr(new DeprecatedAttr());
522}
523
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000524static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
525 // check the attribute arguments.
526 if (Attr.getNumArgs() != 0) {
527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
528 return;
529 }
530
531 d->addAttr(new UnavailableAttr());
532}
533
Chris Lattner803d0802008-06-29 00:43:07 +0000534static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000535 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000536 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000537 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000538 return;
539 }
540
Chris Lattner545dd342008-06-28 23:36:30 +0000541 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000542 Arg = Arg->IgnoreParenCasts();
543 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
544
545 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000547 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000548 return;
549 }
550
551 const char *TypeStr = Str->getStrData();
552 unsigned TypeLen = Str->getByteLength();
553 VisibilityAttr::VisibilityTypes type;
554
555 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
556 type = VisibilityAttr::DefaultVisibility;
557 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
558 type = VisibilityAttr::HiddenVisibility;
559 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
560 type = VisibilityAttr::HiddenVisibility; // FIXME
561 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
562 type = VisibilityAttr::ProtectedVisibility;
563 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000564 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000565 return;
566 }
567
568 d->addAttr(new VisibilityAttr(type));
569}
570
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000571static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000572 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000573 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000574 << "objc_gc" << 1;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000575 return;
576 }
577
578 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000579 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000580 return;
581 }
582
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000583
584 ObjCGCAttr::GCAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000585 if (Attr.getParameterName()->isStr("weak")) {
Fariborz Jahanian24b93f22008-11-20 19:35:51 +0000586 if (isa<FieldDecl>(d) && !isa<ObjCIvarDecl>(d))
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000587 S.Diag(Attr.getLoc(), diag::warn_attribute_weak_on_field);
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000588 type = ObjCGCAttr::Weak;
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000589 }
Chris Lattner92e62b02008-11-20 04:42:34 +0000590 else if (Attr.getParameterName()->isStr("strong"))
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000591 type = ObjCGCAttr::Strong;
592 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000593 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000594 << "objc_gc" << Attr.getParameterName();
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000595 return;
596 }
597
598 d->addAttr(new ObjCGCAttr(type));
599}
600
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000601static void HandleObjCNSObject(Decl *d, const AttributeList &Attr, Sema &S) {
602 if (Attr.getNumArgs() != 0) {
603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
604 return;
605 }
606 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(d)) {
607 QualType T = TD->getUnderlyingType();
608 if (!T->isPointerType() ||
609 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
610 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
611 return;
612 }
613 }
614 d->addAttr(new ObjCNSObjectAttr);
615}
616
Douglas Gregorf9201e02009-02-11 23:02:49 +0000617static void
618HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
619 if (Attr.getNumArgs() != 0) {
620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
621 return;
622 }
623
624 if (!isa<FunctionDecl>(D)) {
625 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
626 return;
627 }
628
629 D->addAttr(new OverloadableAttr);
630}
631
Steve Naroff9eae5762008-09-18 16:44:58 +0000632static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
633 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000636 return;
637 }
638
639 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000641 return;
642 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000643
644 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000645 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000646 type = BlocksAttr::ByRef;
647 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000648 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000650 return;
651 }
652
653 d->addAttr(new BlocksAttr(type));
654}
655
Anders Carlsson77091822008-10-05 18:05:59 +0000656static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
657 // check the attribute arguments.
658 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
660 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000661 return;
662 }
663
664 int sentinel = 0;
665 if (Attr.getNumArgs() > 0) {
666 Expr *E = static_cast<Expr *>(Attr.getArg(0));
667 llvm::APSInt Idx(32);
668 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000669 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000670 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000671 return;
672 }
673 sentinel = Idx.getZExtValue();
674
675 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000676 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
677 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000678 return;
679 }
680 }
681
682 int nullPos = 0;
683 if (Attr.getNumArgs() > 1) {
684 Expr *E = static_cast<Expr *>(Attr.getArg(1));
685 llvm::APSInt Idx(32);
686 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000687 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000688 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000689 return;
690 }
691 nullPos = Idx.getZExtValue();
692
693 if (nullPos > 1 || nullPos < 0) {
694 // FIXME: This error message could be improved, it would be nice
695 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000696 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
697 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000698 return;
699 }
700 }
701
702 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
703 QualType FT = FD->getType();
704 if (!FT->getAsFunctionTypeProto()->isVariadic()) {
705 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
706 return;
707 }
708 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
709 if (!MD->isVariadic()) {
710 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
711 return;
712 }
713 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000714 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000715 << "sentinel" << 3 /*function or method*/;
Anders Carlsson77091822008-10-05 18:05:59 +0000716 return;
717 }
718
719 // FIXME: Actually create the attribute.
720}
721
Chris Lattner026dc962009-02-14 07:37:35 +0000722static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
723 // check the attribute arguments.
724 if (Attr.getNumArgs() != 0) {
725 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
726 return;
727 }
728
729 // TODO: could also be applied to methods?
730 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
731 if (!Fn) {
732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
733 << "warn_unused_result" << 0 /*function*/;
734 return;
735 }
736
737 Fn->addAttr(new WarnUnusedResultAttr());
738}
739
740static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000741 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000742 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000743 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000744 return;
745 }
746
Chris Lattner026dc962009-02-14 07:37:35 +0000747 D->addAttr(new WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000748}
749
Chris Lattner026dc962009-02-14 07:37:35 +0000750static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000751 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000752 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000753 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000754 return;
755 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000756
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000757 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000758 if (isa<VarDecl>(D)) {
759 D->addAttr(new DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000760 return;
761 }
762
Chris Lattner026dc962009-02-14 07:37:35 +0000763 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000764 if (!FD) {
765 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000766 << "dllimport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000767 return;
768 }
769
770 // Currently, the dllimport attribute is ignored for inlined functions.
771 // Warning is emitted.
772 if (FD->isInline()) {
773 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
774 return;
775 }
776
777 // The attribute is also overridden by a subsequent declaration as dllexport.
778 // Warning is emitted.
779 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
780 nextAttr = nextAttr->getNext()) {
781 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
782 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
783 return;
784 }
785 }
786
Chris Lattner026dc962009-02-14 07:37:35 +0000787 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000788 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
789 return;
790 }
791
Chris Lattner026dc962009-02-14 07:37:35 +0000792 D->addAttr(new DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000793}
794
Chris Lattner026dc962009-02-14 07:37:35 +0000795static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000796 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000797 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000799 return;
800 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000801
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000802 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000803 if (isa<VarDecl>(D)) {
804 D->addAttr(new DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000805 return;
806 }
807
Chris Lattner026dc962009-02-14 07:37:35 +0000808 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000809 if (!FD) {
810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000811 << "dllexport" << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000812 return;
813 }
814
815 // Currently, the dllexport attribute is ignored for inlined functions,
816 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
817 if (FD->isInline()) {
818 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
819 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
820 return;
821 }
822
Chris Lattner026dc962009-02-14 07:37:35 +0000823 D->addAttr(new DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000824}
825
Chris Lattner026dc962009-02-14 07:37:35 +0000826static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000827 // Attribute has no arguments.
828 if (Attr.getNumArgs() != 1) {
829 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
830 return;
831 }
832
833 // Make sure that there is a string literal as the sections's single
834 // argument.
835 StringLiteral *SE =
836 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
837 if (!SE) {
838 // FIXME
839 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
840 return;
841 }
Chris Lattner026dc962009-02-14 07:37:35 +0000842 D->addAttr(new SectionAttr(std::string(SE->getStrData(),
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000843 SE->getByteLength())));
844}
845
Chris Lattner803d0802008-06-29 00:43:07 +0000846static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000847 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000848 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000850 return;
851 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000852
853 // Attribute can be applied only to functions.
854 if (!isa<FunctionDecl>(d)) {
855 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000856 << "stdcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000857 return;
858 }
859
860 // stdcall and fastcall attributes are mutually incompatible.
861 if (d->getAttr<FastCallAttr>()) {
862 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
863 << "stdcall" << "fastcall";
864 return;
865 }
866
Chris Lattner6b6b5372008-06-26 18:38:35 +0000867 d->addAttr(new StdCallAttr());
868}
869
Chris Lattner803d0802008-06-29 00:43:07 +0000870static void HandleFastCallAttr(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 if (!isa<FunctionDecl>(d)) {
878 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000879 << "fastcall" << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000880 return;
881 }
882
883 // stdcall and fastcall attributes are mutually incompatible.
884 if (d->getAttr<StdCallAttr>()) {
885 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
886 << "fastcall" << "stdcall";
887 return;
888 }
889
Chris Lattner6b6b5372008-06-26 18:38:35 +0000890 d->addAttr(new FastCallAttr());
891}
892
Chris Lattner803d0802008-06-29 00:43:07 +0000893static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000894 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000895 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000897 return;
898 }
899
900 d->addAttr(new NoThrowAttr());
901}
902
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000903static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
904 // check the attribute arguments.
905 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000907 return;
908 }
909
910 d->addAttr(new ConstAttr());
911}
912
913static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
914 // check the attribute arguments.
915 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000917 return;
918 }
919
920 d->addAttr(new PureAttr());
921}
922
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000923static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000924 // Match gcc which ignores cleanup attrs when compiling C++.
925 if (S.getLangOptions().CPlusPlus)
926 return;
927
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000928 if (!Attr.getParameterName()) {
929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
930 return;
931 }
932
933 if (Attr.getNumArgs() != 0) {
934 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
935 return;
936 }
937
938 VarDecl *VD = dyn_cast<VarDecl>(d);
939
940 if (!VD || !VD->hasLocalStorage()) {
941 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
942 return;
943 }
944
945 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000946 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
947 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000948 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000949 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000950 Attr.getParameterName();
951 return;
952 }
953
954 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
955 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000956 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000957 Attr.getParameterName();
958 return;
959 }
960
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000961 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000962 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000963 Attr.getParameterName();
964 return;
965 }
966
Anders Carlsson89941c12009-02-07 23:16:50 +0000967 // We're currently more strict than GCC about what function types we accept.
968 // If this ever proves to be a problem it should be easy to fix.
969 QualType Ty = S.Context.getPointerType(VD->getType());
970 QualType ParamTy = FD->getParamDecl(0)->getType();
971 if (Ty != ParamTy) {
972 S.Diag(Attr.getLoc(),
973 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
974 Attr.getParameterName() << ParamTy << Ty;
975 return;
976 }
977
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000978 d->addAttr(new CleanupAttr(FD));
979}
980
Chris Lattner6b6b5372008-06-26 18:38:35 +0000981/// Handle __attribute__((format(type,idx,firstarg))) attributes
982/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000983static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000984
Chris Lattner545dd342008-06-28 23:36:30 +0000985 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000987 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000988 return;
989 }
990
Chris Lattner545dd342008-06-28 23:36:30 +0000991 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000993 return;
994 }
995
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000996 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +0000998 << "format" << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000999 return;
1000 }
1001
1002 // FIXME: in C++ the implicit 'this' function parameter also counts.
1003 // this is needed in order to be compatible with GCC
1004 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001005 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001006 unsigned FirstIdx = 1;
1007
Chris Lattner545dd342008-06-28 23:36:30 +00001008 const char *Format = Attr.getParameterName()->getName();
1009 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001010
1011 // Normalize the argument, __foo__ becomes foo.
1012 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1013 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1014 Format += 2;
1015 FormatLen -= 4;
1016 }
1017
1018 bool Supported = false;
1019 bool is_NSString = false;
1020 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001021 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001022
1023 switch (FormatLen) {
1024 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001025 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1026 case 6: Supported = !memcmp(Format, "printf", 6); break;
1027 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001028 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001029 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1030 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1031 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001032 break;
1033 }
1034
1035 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001036 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1037 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001038 return;
1039 }
1040
1041 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001042 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001043 llvm::APSInt Idx(32);
1044 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001045 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001046 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001047 return;
1048 }
1049
1050 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001051 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001052 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001053 return;
1054 }
1055
1056 // FIXME: Do we need to bounds check?
1057 unsigned ArgIdx = Idx.getZExtValue() - 1;
1058
1059 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001060 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001061
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001062 if (is_CFString) {
1063 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001064 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1065 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001066 return;
1067 }
1068 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001069 // FIXME: do we need to check if the type is NSString*? What are
1070 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001071 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001072 // FIXME: Should highlight the actual expression that has the
1073 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001074 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1075 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001076 return;
1077 }
1078 } else if (!Ty->isPointerType() ||
1079 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1080 // FIXME: Should highlight the actual expression that has the
1081 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001082 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1083 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001084 return;
1085 }
1086
1087 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001088 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001089 llvm::APSInt FirstArg(32);
1090 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001091 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001092 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001093 return;
1094 }
1095
1096 // check if the function is variadic if the 3rd argument non-zero
1097 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001098 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001099 ++NumArgs; // +1 for ...
1100 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001101 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001102 return;
1103 }
1104 }
1105
Chris Lattner3c73c412008-11-19 08:23:25 +00001106 // strftime requires FirstArg to be 0 because it doesn't read from any
1107 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001108 if (is_strftime) {
1109 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001110 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1111 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001112 return;
1113 }
1114 // if 0 it disables parameter checking (to use with e.g. va_list)
1115 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001117 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001118 return;
1119 }
1120
1121 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
1122 Idx.getZExtValue(), FirstArg.getZExtValue()));
1123}
1124
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001125static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1126 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001127 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001128 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001129 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001130 return;
1131 }
1132
Eli Friedmanbc887452008-09-02 05:19:23 +00001133 // FIXME: This shouldn't be restricted to typedefs
1134 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1135 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001136 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001137 << "transparent_union" << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001138 return;
1139 }
1140
Eli Friedmanbc887452008-09-02 05:19:23 +00001141 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001142
Eli Friedmanbc887452008-09-02 05:19:23 +00001143 // FIXME: Should we do a check for RD->isDefinition()?
1144
1145 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
1146 // we might silently generate incorrect code; see following code
Douglas Gregor44b43212008-12-11 16:49:14 +00001147 for (RecordDecl::field_iterator Field = RD->field_begin(),
1148 FieldEnd = RD->field_end();
1149 Field != FieldEnd; ++Field) {
1150 if (!Field->getType()->isPointerType()) {
Eli Friedmanbc887452008-09-02 05:19:23 +00001151 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
1152 return;
1153 }
1154 }
1155
1156 // FIXME: This is a complete hack; we should be properly propagating
1157 // transparent_union through Sema. That said, this is close enough to
1158 // correctly compile all the common cases of transparent_union without
1159 // errors or warnings
1160 QualType NewTy = S.Context.VoidPtrTy;
1161 NewTy.addConst();
1162 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001163}
1164
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001165static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001167 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001168 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001169 return;
1170 }
Chris Lattner545dd342008-06-28 23:36:30 +00001171 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001172 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1173
1174 // Make sure that there is a string literal as the annotation's single
1175 // argument.
1176 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001178 return;
1179 }
1180 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
1181 SE->getByteLength())));
1182}
1183
Chris Lattner803d0802008-06-29 00:43:07 +00001184static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001185 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001186 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188 return;
1189 }
1190
1191 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001192 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001193 // FIXME: This should be the target specific maximum alignment.
1194 // (For now we just use 128 bits which is the maximum on X86.
1195 Align = 128;
1196 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001197 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001198
1199 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1200 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001201 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001202 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1203 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001204 return;
1205 }
1206 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001207}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001208
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001209/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001210/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001211///
1212/// Despite what would be logical, the mode attribute is a decl attribute,
1213/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1214/// 'G' be HImode, not an intermediate pointer.
1215///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001216static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001217 // This attribute isn't documented, but glibc uses it. It changes
1218 // the width of an int or unsigned int to the specified size.
1219
1220 // Check that there aren't any arguments
1221 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001223 return;
1224 }
1225
1226 IdentifierInfo *Name = Attr.getParameterName();
1227 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001228 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001229 return;
1230 }
1231 const char *Str = Name->getName();
1232 unsigned Len = Name->getLength();
1233
1234 // Normalize the attribute name, __foo__ becomes foo.
1235 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1236 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1237 Str += 2;
1238 Len -= 4;
1239 }
1240
1241 unsigned DestWidth = 0;
1242 bool IntegerMode = true;
1243 switch (Len) {
1244 case 2:
1245 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
1246 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
1247 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
1248 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
1249 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
1250 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
1251 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
1252 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
1253 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
1254 break;
1255 case 4:
1256 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1257 // pointer on PIC16 and other embedded platforms.
1258 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001259 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001260 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001261 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001262 break;
1263 case 7:
1264 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001265 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001266 break;
1267 }
1268
1269 QualType OldTy;
1270 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1271 OldTy = TD->getUnderlyingType();
1272 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1273 OldTy = VD->getType();
1274 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001275 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1276 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001277 return;
1278 }
1279
Eli Friedmanf98aba32009-02-13 02:31:07 +00001280 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1281 // int8_t and friends, at least with glibc.
1282 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1283 // the wrong width on unusual platforms.
1284 // FIXME: Make sure floating-point mappings are accurate
1285 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001286 QualType NewTy;
1287 switch (DestWidth) {
1288 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001289 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001290 return;
1291 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001292 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001293 return;
1294 case 8:
1295 assert(IntegerMode);
1296 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001297 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001298 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001299 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001300 break;
1301 case 16:
1302 assert(IntegerMode);
1303 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001304 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001305 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001306 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001307 break;
1308 case 32:
1309 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001310 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001311 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001312 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001313 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001314 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001315 break;
1316 case 64:
1317 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001318 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001319 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001320 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001321 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001322 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001323 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001324 case 128:
1325 if (!IntegerMode) {
1326 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1327 return;
1328 }
1329 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001330 }
1331
1332 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001333 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001334 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1335 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001336 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001337 }
1338
1339 // Install the new type.
1340 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1341 TD->setUnderlyingType(NewTy);
1342 else
1343 cast<ValueDecl>(D)->setType(NewTy);
1344}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001345
Anders Carlssond87df372009-02-13 06:46:13 +00001346static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1347 // check the attribute arguments.
1348 if (Attr.getNumArgs() > 0) {
1349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1350 return;
1351 }
Anders Carlssone896d982009-02-13 08:11:52 +00001352
1353 if (!isa<FunctionDecl>(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner026dc962009-02-14 07:37:35 +00001355 << "nodebug" << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001356 return;
1357 }
1358
1359 d->addAttr(new NodebugAttr());
1360}
1361
Chris Lattner0744e5f2008-06-29 00:23:49 +00001362//===----------------------------------------------------------------------===//
1363// Top Level Sema Entry Points
1364//===----------------------------------------------------------------------===//
1365
Sebastian Redla89d82c2008-12-21 19:24:58 +00001366/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001367/// the attribute applies to decls. If the attribute is a type attribute, just
1368/// silently ignore it.
1369static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1370 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001371 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001372 case AttributeList::AT_address_space:
1373 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
1374 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001375 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1376 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001377 case AttributeList::AT_always_inline:
1378 HandleAlwaysInlineAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001379 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1380 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1381 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1382 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1383 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1384 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001385 case AttributeList::AT_ext_vector_type:
1386 HandleExtVectorTypeAttr(D, Attr, S);
1387 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001388 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1389 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001390 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001391 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1392 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1393 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1394 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001395 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001396 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001397 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001398 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001399 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001400 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001401 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001402 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1403 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001404 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001405 case AttributeList::AT_transparent_union:
1406 HandleTransparentUnionAttr(D, Attr, S);
1407 break;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +00001408 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001409 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001410 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001411 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001412 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001413 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1414 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001415 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001416 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001417 case AttributeList::IgnoredAttribute:
1418 // Just ignore
1419 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001420 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001421 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001422 break;
1423 }
1424}
1425
1426/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1427/// attribute list to the specified decl, ignoring any type attributes.
1428void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1429 while (AttrList) {
1430 ProcessDeclAttribute(D, *AttrList, *this);
1431 AttrList = AttrList->getNext();
1432 }
1433}
1434
1435
Chris Lattner0744e5f2008-06-29 00:23:49 +00001436/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1437/// it, apply them to D. This is a bit tricky because PD can have attributes
1438/// specified in many different places, and we need to find and apply them all.
1439void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1440 // Apply decl attributes from the DeclSpec if present.
1441 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1442 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001443
Chris Lattner0744e5f2008-06-29 00:23:49 +00001444 // Walk the declarator structure, applying decl attributes that were in a type
1445 // position to the decl itself. This handles cases like:
1446 // int *__attr__(x)** D;
1447 // when X is a decl attribute.
1448 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1449 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1450 ProcessDeclAttributeList(D, Attrs);
1451
1452 // Finally, apply any attributes on the decl itself.
1453 if (const AttributeList *Attrs = PD.getAttributes())
1454 ProcessDeclAttributeList(D, Attrs);
1455}
1456