blob: 57d1cc7fa0b3b4999a14cd5b0fbe9c346896ac68 [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"
Daniel Dunbare4858a62008-08-11 03:45:03 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000021#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Daniel Dunbard3f2c102008-10-19 02:04:16 +000028static const FunctionType *getFunctionType(Decl *d) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
31 Ty = decl->getType();
32 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
33 Ty = decl->getType();
34 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
35 Ty = decl->getUnderlyingType();
36 else
37 return 0;
38
39 if (Ty->isFunctionPointerType())
40 Ty = Ty->getAsPointerType()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000041
42 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000043}
44
Daniel Dunbar35682492008-09-26 04:12:28 +000045// FIXME: We should provide an abstraction around a method or function
46// to provide the following bits of information.
47
Daniel Dunbard3f2c102008-10-19 02:04:16 +000048/// isFunctionOrMethod - Return true if the given decl has function
49/// type (function or function-typed variable) or an Objective-C
50/// method.
Daniel Dunbar35682492008-09-26 04:12:28 +000051static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000052 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000053}
54
Daniel Dunbard3f2c102008-10-19 02:04:16 +000055/// hasFunctionProto - Return true if the given decl has a argument
56/// information. This decl should have already passed
57/// isFunctionOrMethod.
58static bool hasFunctionProto(Decl *d) {
59 if (const FunctionType *FnTy = getFunctionType(d)) {
60 return isa<FunctionTypeProto>(FnTy);
61 } else {
62 assert(isa<ObjCMethodDecl>(d));
63 return true;
64 }
65}
66
67/// getFunctionOrMethodNumArgs - Return number of function or method
68/// arguments. It is an error to call this on a K&R function (use
69/// hasFunctionProto first).
Daniel Dunbar35682492008-09-26 04:12:28 +000070static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000071 if (const FunctionType *FnTy = getFunctionType(d)) {
72 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000073 return proto->getNumArgs();
74 } else {
75 return cast<ObjCMethodDecl>(d)->getNumParams();
76 }
77}
78
79static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000080 if (const FunctionType *FnTy = getFunctionType(d)) {
81 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000082 return proto->getArgType(Idx);
83 } else {
84 return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType();
85 }
86}
87
88static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000089 if (const FunctionType *FnTy = getFunctionType(d)) {
90 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000091 return proto->isVariadic();
92 } else {
93 return cast<ObjCMethodDecl>(d)->isVariadic();
94 }
95}
96
Chris Lattner6b6b5372008-06-26 18:38:35 +000097static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000098 const PointerType *PT = T->getAsPointerType();
99 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000100 return false;
101
Chris Lattnerb77792e2008-07-26 22:17:49 +0000102 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000103 if (!ClsT)
104 return false;
105
106 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
107
108 // FIXME: Should we walk the chain of classes?
109 return ClsName == &Ctx.Idents.get("NSString") ||
110 ClsName == &Ctx.Idents.get("NSMutableString");
111}
112
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000113static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
114 const PointerType *PT = T->getAsPointerType();
115 if (!PT)
116 return false;
117
118 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
119 if (!RT)
120 return false;
121
122 const RecordDecl *RD = RT->getDecl();
123 if (RD->getTagKind() != TagDecl::TK_struct)
124 return false;
125
126 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
127}
128
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000129//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000130// Attribute Implementations
131//===----------------------------------------------------------------------===//
132
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000133// FIXME: All this manual attribute parsing code is gross. At the
134// least add some helper functions to check most argument patterns (#
135// and types of args).
136
Chris Lattner803d0802008-06-29 00:43:07 +0000137static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
138 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000139 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
140 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000141 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000142 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000143 }
144
Chris Lattner6b6b5372008-06-26 18:38:35 +0000145 QualType curType = tDecl->getUnderlyingType();
146 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000147 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000149 return;
150 }
Chris Lattner545dd342008-06-28 23:36:30 +0000151 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000152 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000153 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000154 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
155 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000156 return;
157 }
158 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
159 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000160 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000161 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type)
162 << curType.getAsString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000163 return;
164 }
165 // unlike gcc's vector_size attribute, the size is specified as the
166 // number of elements, not the number of bytes.
167 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
168
169 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000170 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
171 << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 return;
173 }
174 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000175 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000177 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178}
179
Chris Lattner065c5a82008-06-28 23:48:25 +0000180
181/// HandleVectorSizeAttribute - this attribute is only applicable to
182/// integral and float scalars, although arrays, pointers, and function
183/// return values are allowed in conjunction with this construct. Aggregates
184/// with this attribute are invalid, even if they are of the same size as a
185/// corresponding scalar.
186/// The raw attribute should contain precisely 1 argument, the vector size
187/// for the variable, measured in bytes. If curType and rawAttr are well
188/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000189static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000190 QualType CurType;
191 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
192 CurType = VD->getType();
193 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
194 CurType = TD->getUnderlyingType();
195 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000196 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
197 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000198 return;
199 }
200
201 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000202 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000204 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 }
Chris Lattner545dd342008-06-28 23:36:30 +0000206 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000208 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
210 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000211 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000212 }
213 // navigate to the base type - we need to provide for vector pointers,
214 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000215 if (CurType->isPointerType() || CurType->isArrayType() ||
216 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000217 assert(0 && "HandleVector(): Complex type construction unimplemented");
218 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
219 do {
220 if (PointerType *PT = dyn_cast<PointerType>(canonType))
221 canonType = PT->getPointeeType().getTypePtr();
222 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
223 canonType = AT->getElementType().getTypePtr();
224 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
225 canonType = FT->getResultType().getTypePtr();
226 } while (canonType->isPointerType() || canonType->isArrayType() ||
227 canonType->isFunctionType());
228 */
229 }
230 // the base type must be integer or float.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000231 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000232 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type)
233 << CurType.getAsString();
Chris Lattner065c5a82008-06-28 23:48:25 +0000234 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000235 }
Chris Lattner803d0802008-06-29 00:43:07 +0000236 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000237 // vecSize is specified in bytes - convert to bits.
238 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
239
240 // the vector size needs to be an integral multiple of the type size.
241 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000242 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
243 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000244 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000245 }
246 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000247 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
248 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000249 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000250 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000251
252 // Success! Instantiate the vector type, the number of elements is > 0, and
253 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000254 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000255
256 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
257 VD->setType(CurType);
258 else
259 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000260}
261
Chris Lattner803d0802008-06-29 00:43:07 +0000262static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000263 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000264 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000266 return;
267 }
268
269 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000270 TD->addAttr(new PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000271 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
272 // If the alignment is less than or equal to 8 bits, the packed attribute
273 // has no effect.
274 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000275 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000276 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner3c73c412008-11-19 08:23:25 +0000277 << Attr.getName() << FD->getType().getAsString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000278 else
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000279 FD->addAttr(new PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000280 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000282}
283
Ted Kremenek96329d42008-07-15 22:26:48 +0000284static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
285 // check the attribute arguments.
286 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000287 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000288 return;
289 }
290
291 // The IBOutlet attribute only applies to instance variables of Objective-C
292 // classes.
293 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
294 ID->addAttr(new IBOutletAttr());
295 else
296 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
297}
298
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000299static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000300 // GCC ignores the nonnull attribute on K&R style function
301 // prototypes, so we ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000302 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
304 << "nonnull" << "function";
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000305 return;
306 }
307
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000308 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000309
310 // The nonnull attribute only applies to pointers.
311 llvm::SmallVector<unsigned, 10> NonNullArgs;
312
313 for (AttributeList::arg_iterator I=Attr.arg_begin(),
314 E=Attr.arg_end(); I!=E; ++I) {
315
316
317 // The argument must be an integer constant expression.
318 Expr *Ex = static_cast<Expr *>(Attr.getArg(0));
319 llvm::APSInt ArgNum(32);
320 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
322 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000323 return;
324 }
325
326 unsigned x = (unsigned) ArgNum.getZExtValue();
327
328 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000329 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000330 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000331 return;
332 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000333
334 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000335
336 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000337 QualType T = getFunctionOrMethodArgType(d, x);
338 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000339 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000340 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
341 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000342 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000343 }
344
345 NonNullArgs.push_back(x);
346 }
347
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000348 // If no arguments were specified to __attribute__((nonnull)) then all
349 // pointer arguments have a nonnull attribute.
350 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000351 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
352 QualType T = getFunctionOrMethodArgType(d, I);
353 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000354 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000355 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000356
357 if (NonNullArgs.empty()) {
358 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
359 return;
360 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000362
363 unsigned* start = &NonNullArgs[0];
364 unsigned size = NonNullArgs.size();
365 std::sort(start, start + size);
366 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000367}
368
Chris Lattner803d0802008-06-29 00:43:07 +0000369static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000370 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000371 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000373 return;
374 }
375
Chris Lattner545dd342008-06-28 23:36:30 +0000376 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000377 Arg = Arg->IgnoreParenCasts();
378 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
379
380 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000381 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000382 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000383 return;
384 }
385
386 const char *Alias = Str->getStrData();
387 unsigned AliasLen = Str->getByteLength();
388
389 // FIXME: check if target symbol exists in current file
390
391 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
392}
393
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000394static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
395 Sema &S) {
396 // check the attribute arguments.
397 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000399 return;
400 }
401
402 d->addAttr(new AlwaysInlineAttr());
403}
404
Chris Lattner803d0802008-06-29 00:43:07 +0000405static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000406 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000407 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000408 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000409 return;
410 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000411
412 if (!isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000413 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
414 << "noreturn" << "function";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000415 return;
416 }
417
418 d->addAttr(new NoReturnAttr());
419}
420
Ted Kremenek73798892008-07-25 04:39:19 +0000421static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
422 // check the attribute arguments.
423 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000425 return;
426 }
427
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000428 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
430 << "unused" << "variable and function";
Ted Kremenek73798892008-07-25 04:39:19 +0000431 return;
432 }
433
434 d->addAttr(new UnusedAttr());
435}
436
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000437static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
438 // check the attribute arguments.
439 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000440 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
441 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000442 return;
443 }
444
445 int priority = 65535; // FIXME: Do not hardcode such constants.
446 if (Attr.getNumArgs() > 0) {
447 Expr *E = static_cast<Expr *>(Attr.getArg(0));
448 llvm::APSInt Idx(32);
449 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000450 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000451 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000452 return;
453 }
454 priority = Idx.getZExtValue();
455 }
456
457 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
458 if (!Fn) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000459 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
460 << "constructor" << "function";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000461 return;
462 }
463
464 d->addAttr(new ConstructorAttr(priority));
465}
466
467static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
468 // check the attribute arguments.
469 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000470 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
471 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000472 return;
473 }
474
475 int priority = 65535; // FIXME: Do not hardcode such constants.
476 if (Attr.getNumArgs() > 0) {
477 Expr *E = static_cast<Expr *>(Attr.getArg(0));
478 llvm::APSInt Idx(32);
479 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000481 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000482 return;
483 }
484 priority = Idx.getZExtValue();
485 }
486
Anders Carlsson6782fc62008-08-22 22:10:48 +0000487 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000488 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
489 << "destructor" << "function";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000490 return;
491 }
492
493 d->addAttr(new DestructorAttr(priority));
494}
495
Chris Lattner803d0802008-06-29 00:43:07 +0000496static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000497 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000498 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000499 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000500 return;
501 }
502
503 d->addAttr(new DeprecatedAttr());
504}
505
Chris Lattner803d0802008-06-29 00:43:07 +0000506static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000507 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000508 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000510 return;
511 }
512
Chris Lattner545dd342008-06-28 23:36:30 +0000513 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000514 Arg = Arg->IgnoreParenCasts();
515 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
516
517 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000518 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000519 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000520 return;
521 }
522
523 const char *TypeStr = Str->getStrData();
524 unsigned TypeLen = Str->getByteLength();
525 VisibilityAttr::VisibilityTypes type;
526
527 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
528 type = VisibilityAttr::DefaultVisibility;
529 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
530 type = VisibilityAttr::HiddenVisibility;
531 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
532 type = VisibilityAttr::HiddenVisibility; // FIXME
533 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
534 type = VisibilityAttr::ProtectedVisibility;
535 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000536 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
537 << "visibility" << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000538 return;
539 }
540
541 d->addAttr(new VisibilityAttr(type));
542}
543
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000544static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000545 if (!Attr.getParameterName()) {
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 << "objc_gc" << 1;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000548 return;
549 }
550
551 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000553 return;
554 }
555
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000556
557 ObjCGCAttr::GCAttrTypes type;
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000558 if (Attr.getParameterName()->isName("weak")) {
559 if (isa<FieldDecl>(d))
560 S.Diag(Attr.getLoc(), diag::warn_attribute_weak_on_field);
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000561 type = ObjCGCAttr::Weak;
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000562 }
Chris Lattner3c73c412008-11-19 08:23:25 +0000563 else if (Attr.getParameterName()->isName("strong"))
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000564 type = ObjCGCAttr::Strong;
565 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000566 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000567 << "objc_gc" << Attr.getParameterName();
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000568 return;
569 }
570
571 d->addAttr(new ObjCGCAttr(type));
572}
573
Steve Naroff9eae5762008-09-18 16:44:58 +0000574static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
575 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000576 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000577 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000578 return;
579 }
580
581 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000583 return;
584 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000585
586 BlocksAttr::BlocksAttrTypes type;
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 if (Attr.getParameterName()->isName("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000588 type = BlocksAttr::ByRef;
589 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000590 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000591 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000592 return;
593 }
594
595 d->addAttr(new BlocksAttr(type));
596}
597
Anders Carlsson77091822008-10-05 18:05:59 +0000598static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
599 // check the attribute arguments.
600 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
602 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000603 return;
604 }
605
606 int sentinel = 0;
607 if (Attr.getNumArgs() > 0) {
608 Expr *E = static_cast<Expr *>(Attr.getArg(0));
609 llvm::APSInt Idx(32);
610 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000611 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000612 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000613 return;
614 }
615 sentinel = Idx.getZExtValue();
616
617 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000618 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
619 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000620 return;
621 }
622 }
623
624 int nullPos = 0;
625 if (Attr.getNumArgs() > 1) {
626 Expr *E = static_cast<Expr *>(Attr.getArg(1));
627 llvm::APSInt Idx(32);
628 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000629 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000630 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000631 return;
632 }
633 nullPos = Idx.getZExtValue();
634
635 if (nullPos > 1 || nullPos < 0) {
636 // FIXME: This error message could be improved, it would be nice
637 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
639 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000640 return;
641 }
642 }
643
644 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
645 QualType FT = FD->getType();
646 if (!FT->getAsFunctionTypeProto()->isVariadic()) {
647 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
648 return;
649 }
650 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
651 if (!MD->isVariadic()) {
652 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
653 return;
654 }
655 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000656 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
657 << "sentinel" << "function or method";
Anders Carlsson77091822008-10-05 18:05:59 +0000658 return;
659 }
660
661 // FIXME: Actually create the attribute.
662}
663
Chris Lattner803d0802008-06-29 00:43:07 +0000664static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000665 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000666 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000668 return;
669 }
670
671 d->addAttr(new WeakAttr());
672}
673
Chris Lattner803d0802008-06-29 00:43:07 +0000674static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000675 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000676 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000678 return;
679 }
680
681 d->addAttr(new DLLImportAttr());
682}
683
Chris Lattner803d0802008-06-29 00:43:07 +0000684static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000685 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000686 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000687 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000688 return;
689 }
690
691 d->addAttr(new DLLExportAttr());
692}
693
Chris Lattner803d0802008-06-29 00:43:07 +0000694static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000695 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000696 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000697 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000698 return;
699 }
700
701 d->addAttr(new StdCallAttr());
702}
703
Chris Lattner803d0802008-06-29 00:43:07 +0000704static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000705 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000706 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000708 return;
709 }
710
711 d->addAttr(new FastCallAttr());
712}
713
Chris Lattner803d0802008-06-29 00:43:07 +0000714static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000715 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000716 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000717 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000718 return;
719 }
720
721 d->addAttr(new NoThrowAttr());
722}
723
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000724static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
725 // check the attribute arguments.
726 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000727 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000728 return;
729 }
730
731 d->addAttr(new ConstAttr());
732}
733
734static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
735 // check the attribute arguments.
736 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000737 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000738 return;
739 }
740
741 d->addAttr(new PureAttr());
742}
743
Chris Lattner6b6b5372008-06-26 18:38:35 +0000744/// Handle __attribute__((format(type,idx,firstarg))) attributes
745/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000746static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000747
Chris Lattner545dd342008-06-28 23:36:30 +0000748 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000749 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000750 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000751 return;
752 }
753
Chris Lattner545dd342008-06-28 23:36:30 +0000754 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000755 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000756 return;
757 }
758
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000759 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000760 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
761 << "format" << "function";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000762 return;
763 }
764
765 // FIXME: in C++ the implicit 'this' function parameter also counts.
766 // this is needed in order to be compatible with GCC
767 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +0000768 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000769 unsigned FirstIdx = 1;
770
Chris Lattner545dd342008-06-28 23:36:30 +0000771 const char *Format = Attr.getParameterName()->getName();
772 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000773
774 // Normalize the argument, __foo__ becomes foo.
775 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
776 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
777 Format += 2;
778 FormatLen -= 4;
779 }
780
781 bool Supported = false;
782 bool is_NSString = false;
783 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000784 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000785
786 switch (FormatLen) {
787 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000788 case 5: Supported = !memcmp(Format, "scanf", 5); break;
789 case 6: Supported = !memcmp(Format, "printf", 6); break;
790 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000791 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000792 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
793 (is_NSString = !memcmp(Format, "NSString", 8)) ||
794 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000795 break;
796 }
797
798 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000799 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
800 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000801 return;
802 }
803
804 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000805 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000806 llvm::APSInt Idx(32);
807 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000808 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000809 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000810 return;
811 }
812
813 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000814 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +0000815 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000816 return;
817 }
818
819 // FIXME: Do we need to bounds check?
820 unsigned ArgIdx = Idx.getZExtValue() - 1;
821
822 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +0000823 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000824
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000825 if (is_CFString) {
826 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000827 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
828 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000829 return;
830 }
831 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000832 // FIXME: do we need to check if the type is NSString*? What are
833 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000834 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000835 // FIXME: Should highlight the actual expression that has the
836 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000837 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
838 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000839 return;
840 }
841 } else if (!Ty->isPointerType() ||
842 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
843 // FIXME: Should highlight the actual expression that has the
844 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000845 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
846 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000847 return;
848 }
849
850 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000851 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000852 llvm::APSInt FirstArg(32);
853 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000854 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000855 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000856 return;
857 }
858
859 // check if the function is variadic if the 3rd argument non-zero
860 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +0000861 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000862 ++NumArgs; // +1 for ...
863 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000864 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000865 return;
866 }
867 }
868
Chris Lattner3c73c412008-11-19 08:23:25 +0000869 // strftime requires FirstArg to be 0 because it doesn't read from any
870 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +0000871 if (is_strftime) {
872 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000873 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
874 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000875 return;
876 }
877 // if 0 it disables parameter checking (to use with e.g. va_list)
878 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000879 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +0000880 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000881 return;
882 }
883
884 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
885 Idx.getZExtValue(), FirstArg.getZExtValue()));
886}
887
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000888static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
889 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000890 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000891 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000893 return;
894 }
895
Eli Friedmanbc887452008-09-02 05:19:23 +0000896 // FIXME: This shouldn't be restricted to typedefs
897 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
898 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000899 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
900 << "transparent_union" << "union";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000901 return;
902 }
903
Eli Friedmanbc887452008-09-02 05:19:23 +0000904 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000905
Eli Friedmanbc887452008-09-02 05:19:23 +0000906 // FIXME: Should we do a check for RD->isDefinition()?
907
908 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
909 // we might silently generate incorrect code; see following code
910 for (int i = 0; i < RD->getNumMembers(); i++) {
911 if (!RD->getMember(i)->getType()->isPointerType()) {
912 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
913 return;
914 }
915 }
916
917 // FIXME: This is a complete hack; we should be properly propagating
918 // transparent_union through Sema. That said, this is close enough to
919 // correctly compile all the common cases of transparent_union without
920 // errors or warnings
921 QualType NewTy = S.Context.VoidPtrTy;
922 NewTy.addConst();
923 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000924}
925
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000926static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000927 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000928 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000930 return;
931 }
Chris Lattner545dd342008-06-28 23:36:30 +0000932 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000933 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
934
935 // Make sure that there is a string literal as the annotation's single
936 // argument.
937 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000938 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000939 return;
940 }
941 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
942 SE->getByteLength())));
943}
944
Chris Lattner803d0802008-06-29 00:43:07 +0000945static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000946 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000947 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000948 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000949 return;
950 }
951
952 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000953 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000954 // FIXME: This should be the target specific maximum alignment.
955 // (For now we just use 128 bits which is the maximum on X86.
956 Align = 128;
957 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000958 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000959
960 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
961 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000962 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000963 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
964 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +0000965 return;
966 }
967 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000968}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000969
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000970/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000971/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000972///
973/// Despite what would be logical, the mode attribute is a decl attribute,
974/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
975/// 'G' be HImode, not an intermediate pointer.
976///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000977static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000978 // This attribute isn't documented, but glibc uses it. It changes
979 // the width of an int or unsigned int to the specified size.
980
981 // Check that there aren't any arguments
982 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000983 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000984 return;
985 }
986
987 IdentifierInfo *Name = Attr.getParameterName();
988 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000989 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000990 return;
991 }
992 const char *Str = Name->getName();
993 unsigned Len = Name->getLength();
994
995 // Normalize the attribute name, __foo__ becomes foo.
996 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
997 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
998 Str += 2;
999 Len -= 4;
1000 }
1001
1002 unsigned DestWidth = 0;
1003 bool IntegerMode = true;
1004 switch (Len) {
1005 case 2:
1006 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
1007 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
1008 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
1009 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
1010 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
1011 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
1012 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
1013 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
1014 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
1015 break;
1016 case 4:
1017 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1018 // pointer on PIC16 and other embedded platforms.
1019 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001020 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001021 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001022 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001023 break;
1024 case 7:
1025 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001026 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001027 break;
1028 }
1029
1030 QualType OldTy;
1031 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1032 OldTy = TD->getUnderlyingType();
1033 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1034 OldTy = VD->getType();
1035 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001036 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1037 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001038 return;
1039 }
1040
1041 // FIXME: Need proper fixed-width types
1042 QualType NewTy;
1043 switch (DestWidth) {
1044 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001045 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001046 return;
1047 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001048 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001049 return;
1050 case 8:
1051 assert(IntegerMode);
1052 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001053 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001054 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001055 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001056 break;
1057 case 16:
1058 assert(IntegerMode);
1059 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001060 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001061 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001062 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001063 break;
1064 case 32:
1065 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001066 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001067 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001068 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001069 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001070 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001071 break;
1072 case 64:
1073 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001074 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001075 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001076 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001077 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001078 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001079 break;
1080 }
1081
1082 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001083 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001084 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1085 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001086 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001087 }
1088
1089 // Install the new type.
1090 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1091 TD->setUnderlyingType(NewTy);
1092 else
1093 cast<ValueDecl>(D)->setType(NewTy);
1094}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001095
1096//===----------------------------------------------------------------------===//
1097// Top Level Sema Entry Points
1098//===----------------------------------------------------------------------===//
1099
Chris Lattner803d0802008-06-29 00:43:07 +00001100/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
1101/// the attribute applies to decls. If the attribute is a type attribute, just
1102/// silently ignore it.
1103static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1104 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001105 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001106 case AttributeList::AT_address_space:
1107 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
1108 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001109 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1110 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001111 case AttributeList::AT_always_inline:
1112 HandleAlwaysInlineAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001113 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1114 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1115 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1116 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1117 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1118 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001119 case AttributeList::AT_ext_vector_type:
1120 HandleExtVectorTypeAttr(D, Attr, S);
1121 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001122 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1123 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001124 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001125 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1126 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1127 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1128 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1129 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1130 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1131 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001132 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1133 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001134 case AttributeList::AT_transparent_union:
1135 HandleTransparentUnionAttr(D, Attr, S);
1136 break;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +00001137 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001138 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001139 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001140 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1141 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001142 default:
1143#if 0
1144 // TODO: when we have the full set of attributes, warn about unknown ones.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001145 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored)
1146 << Attr->getName()->getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001147#endif
1148 break;
1149 }
1150}
1151
1152/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1153/// attribute list to the specified decl, ignoring any type attributes.
1154void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1155 while (AttrList) {
1156 ProcessDeclAttribute(D, *AttrList, *this);
1157 AttrList = AttrList->getNext();
1158 }
1159}
1160
1161
Chris Lattner0744e5f2008-06-29 00:23:49 +00001162/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1163/// it, apply them to D. This is a bit tricky because PD can have attributes
1164/// specified in many different places, and we need to find and apply them all.
1165void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1166 // Apply decl attributes from the DeclSpec if present.
1167 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1168 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001169
Chris Lattner0744e5f2008-06-29 00:23:49 +00001170 // Walk the declarator structure, applying decl attributes that were in a type
1171 // position to the decl itself. This handles cases like:
1172 // int *__attr__(x)** D;
1173 // when X is a decl attribute.
1174 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1175 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1176 ProcessDeclAttributeList(D, Attrs);
1177
1178 // Finally, apply any attributes on the decl itself.
1179 if (const AttributeList *Attrs = PD.getAttributes())
1180 ProcessDeclAttributeList(D, Attrs);
1181}
1182