blob: 6e2cc699a71edf78d21346fd70f61930fb910016 [file] [log] [blame]
Chris Lattner6953a072008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare0ad2152008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerdc789562008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek26151d12008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6953a072008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattner2024f0a2008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Daniel Dunbar0ea20472008-10-19 02:04:16 +000027static const FunctionType *getFunctionType(Decl *d) {
Chris Lattner6953a072008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6953a072008-06-26 18:38:35 +000029 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30 Ty = decl->getType();
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32 Ty = decl->getType();
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
35 else
36 return 0;
37
38 if (Ty->isFunctionPointerType())
39 Ty = Ty->getAsPointerType()->getPointeeType();
Daniel Dunbar0ea20472008-10-19 02:04:16 +000040
41 return Ty->getAsFunctionType();
Chris Lattner6953a072008-06-26 18:38:35 +000042}
43
Daniel Dunbard1d847c2008-09-26 04:12:28 +000044// FIXME: We should provide an abstraction around a method or function
45// to provide the following bits of information.
46
Daniel Dunbar0ea20472008-10-19 02:04:16 +000047/// isFunctionOrMethod - Return true if the given decl has function
48/// type (function or function-typed variable) or an Objective-C
49/// method.
Daniel Dunbard1d847c2008-09-26 04:12:28 +000050static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000051 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000052}
53
Daniel Dunbar0ea20472008-10-19 02:04:16 +000054/// hasFunctionProto - Return true if the given decl has a argument
55/// information. This decl should have already passed
56/// isFunctionOrMethod.
57static bool hasFunctionProto(Decl *d) {
58 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor4fa58902009-02-26 23:50:07 +000059 return isa<FunctionProtoType>(FnTy);
Daniel Dunbar0ea20472008-10-19 02:04:16 +000060 } else {
61 assert(isa<ObjCMethodDecl>(d));
62 return true;
63 }
64}
65
66/// getFunctionOrMethodNumArgs - Return number of function or method
67/// arguments. It is an error to call this on a K&R function (use
68/// hasFunctionProto first).
Daniel Dunbard1d847c2008-09-26 04:12:28 +000069static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Chris Lattner5c6b2c62009-02-20 18:43:26 +000070 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor4fa58902009-02-26 23:50:07 +000071 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chris Lattner5c6b2c62009-02-20 18:43:26 +000072 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbard1d847c2008-09-26 04:12:28 +000073}
74
75static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner5c6b2c62009-02-20 18:43:26 +000076 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor4fa58902009-02-26 23:50:07 +000077 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chris Lattner5c6b2c62009-02-20 18:43:26 +000078
79 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbard1d847c2008-09-26 04:12:28 +000080}
81
82static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000083 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor4fa58902009-02-26 23:50:07 +000084 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000085 return proto->isVariadic();
86 } else {
87 return cast<ObjCMethodDecl>(d)->isVariadic();
88 }
89}
90
Chris Lattner6953a072008-06-26 18:38:35 +000091static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000092 const PointerType *PT = T->getAsPointerType();
93 if (!PT)
Chris Lattner6953a072008-06-26 18:38:35 +000094 return false;
95
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000096 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6953a072008-06-26 18:38:35 +000097 if (!ClsT)
98 return false;
99
100 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
101
102 // FIXME: Should we walk the chain of classes?
103 return ClsName == &Ctx.Idents.get("NSString") ||
104 ClsName == &Ctx.Idents.get("NSMutableString");
105}
106
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000107static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
108 const PointerType *PT = T->getAsPointerType();
109 if (!PT)
110 return false;
111
112 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
113 if (!RT)
114 return false;
115
116 const RecordDecl *RD = RT->getDecl();
117 if (RD->getTagKind() != TagDecl::TK_struct)
118 return false;
119
120 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
121}
122
Chris Lattner2024f0a2008-06-29 00:16:31 +0000123//===----------------------------------------------------------------------===//
Chris Lattner2024f0a2008-06-29 00:16:31 +0000124// Attribute Implementations
125//===----------------------------------------------------------------------===//
126
Daniel Dunbar8716a012008-07-31 22:40:48 +0000127// FIXME: All this manual attribute parsing code is gross. At the
128// least add some helper functions to check most argument patterns (#
129// and types of args).
130
Chris Lattner703c52d2008-06-29 00:43:07 +0000131static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
132 Sema &S) {
Chris Lattner1c151132008-06-28 23:36:30 +0000133 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
134 if (tDecl == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000135 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner1c151132008-06-28 23:36:30 +0000136 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000137 }
138
Chris Lattner6953a072008-06-26 18:38:35 +0000139 QualType curType = tDecl->getUnderlyingType();
140 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000141 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000142 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000143 return;
144 }
Chris Lattner1c151132008-06-28 23:36:30 +0000145 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000146 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000147 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
149 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000150 return;
151 }
152 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
153 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000154 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000155 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6953a072008-06-26 18:38:35 +0000156 return;
157 }
158 // unlike gcc's vector_size attribute, the size is specified as the
159 // number of elements, not the number of bytes.
160 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
161
162 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000163 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
164 << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000165 return;
166 }
167 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner703c52d2008-06-29 00:43:07 +0000168 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6953a072008-06-26 18:38:35 +0000169 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner703c52d2008-06-29 00:43:07 +0000170 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6953a072008-06-26 18:38:35 +0000171}
172
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000173
174/// HandleVectorSizeAttribute - this attribute is only applicable to
175/// integral and float scalars, although arrays, pointers, and function
176/// return values are allowed in conjunction with this construct. Aggregates
177/// with this attribute are invalid, even if they are of the same size as a
178/// corresponding scalar.
179/// The raw attribute should contain precisely 1 argument, the vector size
180/// for the variable, measured in bytes. If curType and rawAttr are well
181/// formed, this routine will return a new vector type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000182static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000183 QualType CurType;
184 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
185 CurType = VD->getType();
186 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
187 CurType = TD->getUnderlyingType();
188 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000189 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
190 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000191 return;
192 }
193
194 // Check the attribute arugments.
Chris Lattner1c151132008-06-28 23:36:30 +0000195 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000196 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000197 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000198 }
Chris Lattner1c151132008-06-28 23:36:30 +0000199 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000200 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000201 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000202 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
203 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000204 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000205 }
206 // navigate to the base type - we need to provide for vector pointers,
207 // vector arrays, and functions returning vectors.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000208 if (CurType->isPointerType() || CurType->isArrayType() ||
209 CurType->isFunctionType()) {
Chris Lattner077fed52009-05-13 04:00:12 +0000210 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
211 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000212 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
213 do {
214 if (PointerType *PT = dyn_cast<PointerType>(canonType))
215 canonType = PT->getPointeeType().getTypePtr();
216 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
217 canonType = AT->getElementType().getTypePtr();
218 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
219 canonType = FT->getResultType().getTypePtr();
220 } while (canonType->isPointerType() || canonType->isArrayType() ||
221 canonType->isFunctionType());
222 */
223 }
224 // the base type must be integer or float.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000225 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000226 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000227 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000228 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000229 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-06-26 18:38:35 +0000230 // vecSize is specified in bytes - convert to bits.
231 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
232
233 // the vector size needs to be an integral multiple of the type size.
234 if (vectorSize % typeSize) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000235 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
236 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000237 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000238 }
239 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
241 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000242 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000243 }
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000244
245 // Success! Instantiate the vector type, the number of elements is > 0, and
246 // not required to be a power of 2, unlike GCC.
Chris Lattner703c52d2008-06-29 00:43:07 +0000247 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000248
249 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
250 VD->setType(CurType);
251 else
252 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6953a072008-06-26 18:38:35 +0000253}
254
Chris Lattner703c52d2008-06-29 00:43:07 +0000255static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000256 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000257 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000258 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000259 return;
260 }
261
262 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner16ded572009-03-04 06:34:08 +0000263 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000264 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
265 // If the alignment is less than or equal to 8 bits, the packed attribute
266 // has no effect.
267 if (!FD->getType()->isIncompleteType() &&
Chris Lattner703c52d2008-06-29 00:43:07 +0000268 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000269 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000270 << Attr.getName() << FD->getType();
Chris Lattner6953a072008-06-26 18:38:35 +0000271 else
Chris Lattner16ded572009-03-04 06:34:08 +0000272 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000273 } else
Chris Lattner65cae292008-11-19 08:23:25 +0000274 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000275}
276
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000277static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
278 // check the attribute arguments.
279 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000280 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000281 return;
282 }
283
284 // The IBOutlet attribute only applies to instance variables of Objective-C
285 // classes.
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000286 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner16ded572009-03-04 06:34:08 +0000287 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000288 else
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000289 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000290}
291
Ted Kremenekc0af2542008-07-21 21:53:04 +0000292static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000293 // GCC ignores the nonnull attribute on K&R style function
294 // prototypes, so we ignore it as well
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000295 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000296 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000297 << "nonnull" << 0 /*function*/;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000298 return;
299 }
300
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000301 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000302
303 // The nonnull attribute only applies to pointers.
304 llvm::SmallVector<unsigned, 10> NonNullArgs;
305
306 for (AttributeList::arg_iterator I=Attr.arg_begin(),
307 E=Attr.arg_end(); I!=E; ++I) {
308
309
310 // The argument must be an integer constant expression.
Ted Kremenek73dc0652008-12-04 19:38:33 +0000311 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000312 llvm::APSInt ArgNum(32);
313 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
315 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000316 return;
317 }
318
319 unsigned x = (unsigned) ArgNum.getZExtValue();
320
321 if (x < 1 || x > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner68f621c2008-11-19 07:22:31 +0000323 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000324 return;
325 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000326
327 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000328
329 // Is the function argument a pointer type?
Ted Kremenekba57bd92008-11-18 06:52:58 +0000330 QualType T = getFunctionOrMethodArgType(d, x);
331 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000332 // FIXME: Should also highlight argument in decl.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000333 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
334 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7af441b2008-09-01 19:57:52 +0000335 continue;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000336 }
337
338 NonNullArgs.push_back(x);
339 }
340
Ted Kremenek7af441b2008-09-01 19:57:52 +0000341 // If no arguments were specified to __attribute__((nonnull)) then all
342 // pointer arguments have a nonnull attribute.
343 if (NonNullArgs.empty()) {
Ted Kremenekba57bd92008-11-18 06:52:58 +0000344 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
345 QualType T = getFunctionOrMethodArgType(d, I);
346 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000347 NonNullArgs.push_back(I);
Ted Kremenekba57bd92008-11-18 06:52:58 +0000348 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000349
350 if (NonNullArgs.empty()) {
351 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
352 return;
353 }
Ted Kremenekc0af2542008-07-21 21:53:04 +0000354 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000355
356 unsigned* start = &NonNullArgs[0];
357 unsigned size = NonNullArgs.size();
358 std::sort(start, start + size);
Chris Lattner16ded572009-03-04 06:34:08 +0000359 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekc0af2542008-07-21 21:53:04 +0000360}
361
Chris Lattner703c52d2008-06-29 00:43:07 +0000362static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000363 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000364 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000365 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000366 return;
367 }
368
Chris Lattner1c151132008-06-28 23:36:30 +0000369 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000370 Arg = Arg->IgnoreParenCasts();
371 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
372
373 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000374 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000375 << "alias" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000376 return;
377 }
378
379 const char *Alias = Str->getStrData();
380 unsigned AliasLen = Str->getByteLength();
381
382 // FIXME: check if target symbol exists in current file
383
Chris Lattner16ded572009-03-04 06:34:08 +0000384 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6953a072008-06-26 18:38:35 +0000385}
386
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000387static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
388 Sema &S) {
389 // check the attribute arguments.
390 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000391 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000392 return;
393 }
Anders Carlssoneb12e782009-02-19 19:16:48 +0000394
Chris Lattner2a3e43d2009-04-14 17:02:11 +0000395 if (!isa<FunctionDecl>(d)) {
Anders Carlssoneb12e782009-02-19 19:16:48 +0000396 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
397 << "always_inline" << 0 /*function*/;
398 return;
399 }
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000400
Chris Lattner16ded572009-03-04 06:34:08 +0000401 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000402}
403
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000404static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
405 Sema &S, const char *attrName) {
Chris Lattner6953a072008-06-26 18:38:35 +0000406 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000407 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000408 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000409 return false;
Chris Lattner6953a072008-06-26 18:38:35 +0000410 }
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000411
Mike Stump115a0722009-04-29 19:03:13 +0000412 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
413 ValueDecl *VD = dyn_cast<ValueDecl>(d);
414 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
416 << attrName << 0 /*function*/;
417 return false;
418 }
Chris Lattner6953a072008-06-26 18:38:35 +0000419 }
420
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000421 return true;
422}
423
424static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
425 if (HandleCommonNoReturnAttr(d, Attr, S, "noreturn"))
426 d->addAttr(::new (S.Context) NoReturnAttr());
427}
428
429static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
430 Sema &S) {
431 if (HandleCommonNoReturnAttr(d, Attr, S, "analyzer_noreturn"))
432 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000433}
434
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000435static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
436 // check the attribute arguments.
437 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000439 return;
440 }
441
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000442 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000444 << "unused" << 2 /*variable and function*/;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000445 return;
446 }
447
Chris Lattner16ded572009-03-04 06:34:08 +0000448 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000449}
450
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000451static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
452 // check the attribute arguments.
453 if (Attr.getNumArgs() != 0) {
454 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
455 return;
456 }
457
458 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbard0cc5242009-02-13 22:48:56 +0000459 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000460 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
461 return;
462 }
463 } else if (!isFunctionOrMethod(d)) {
464 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000465 << "used" << 2 /*variable and function*/;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000466 return;
467 }
468
Chris Lattner16ded572009-03-04 06:34:08 +0000469 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000470}
471
Daniel Dunbar8716a012008-07-31 22:40:48 +0000472static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
473 // check the attribute arguments.
474 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
476 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000477 return;
478 }
479
480 int priority = 65535; // FIXME: Do not hardcode such constants.
481 if (Attr.getNumArgs() > 0) {
482 Expr *E = static_cast<Expr *>(Attr.getArg(0));
483 llvm::APSInt Idx(32);
484 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000486 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000487 return;
488 }
489 priority = Idx.getZExtValue();
490 }
491
Chris Lattner2a3e43d2009-04-14 17:02:11 +0000492 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000493 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000494 << "constructor" << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000495 return;
496 }
497
Chris Lattner16ded572009-03-04 06:34:08 +0000498 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar8716a012008-07-31 22:40:48 +0000499}
500
501static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
502 // check the attribute arguments.
503 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
505 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000506 return;
507 }
508
509 int priority = 65535; // FIXME: Do not hardcode such constants.
510 if (Attr.getNumArgs() > 0) {
511 Expr *E = static_cast<Expr *>(Attr.getArg(0));
512 llvm::APSInt Idx(32);
513 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000514 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000515 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000516 return;
517 }
518 priority = Idx.getZExtValue();
519 }
520
Anders Carlsson03e49652008-08-22 22:10:48 +0000521 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000522 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000523 << "destructor" << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000524 return;
525 }
526
Chris Lattner16ded572009-03-04 06:34:08 +0000527 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar8716a012008-07-31 22:40:48 +0000528}
529
Chris Lattner703c52d2008-06-29 00:43:07 +0000530static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000531 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000532 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000534 return;
535 }
536
Chris Lattner16ded572009-03-04 06:34:08 +0000537 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000538}
539
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000540static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
541 // check the attribute arguments.
542 if (Attr.getNumArgs() != 0) {
543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
544 return;
545 }
546
Chris Lattner16ded572009-03-04 06:34:08 +0000547 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000548}
549
Chris Lattner703c52d2008-06-29 00:43:07 +0000550static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000551 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000552 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000553 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000554 return;
555 }
556
Chris Lattner1c151132008-06-28 23:36:30 +0000557 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000558 Arg = Arg->IgnoreParenCasts();
559 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
560
561 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000562 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000563 << "visibility" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000564 return;
565 }
566
567 const char *TypeStr = Str->getStrData();
568 unsigned TypeLen = Str->getByteLength();
569 VisibilityAttr::VisibilityTypes type;
570
571 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
572 type = VisibilityAttr::DefaultVisibility;
573 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
574 type = VisibilityAttr::HiddenVisibility;
575 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
576 type = VisibilityAttr::HiddenVisibility; // FIXME
577 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
578 type = VisibilityAttr::ProtectedVisibility;
579 else {
Chris Lattnerb1753422008-11-23 21:45:46 +0000580 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6953a072008-06-26 18:38:35 +0000581 return;
582 }
583
Chris Lattner16ded572009-03-04 06:34:08 +0000584 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6953a072008-06-26 18:38:35 +0000585}
586
Chris Lattnera9b98462009-02-14 08:09:34 +0000587static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
588 Sema &S) {
589 if (Attr.getNumArgs() != 0) {
590 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
591 return;
592 }
593
594 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
595 if (OCI == 0) {
596 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
597 return;
598 }
599
Chris Lattner16ded572009-03-04 06:34:08 +0000600 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattnera9b98462009-02-14 08:09:34 +0000601}
602
603static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000604 if (Attr.getNumArgs() != 0) {
605 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
606 return;
607 }
Chris Lattnera9b98462009-02-14 08:09:34 +0000608 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000609 QualType T = TD->getUnderlyingType();
610 if (!T->isPointerType() ||
611 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
612 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
613 return;
614 }
615 }
Chris Lattner16ded572009-03-04 06:34:08 +0000616 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000617}
618
Douglas Gregorfcb19192009-02-11 23:02:49 +0000619static void
620HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
621 if (Attr.getNumArgs() != 0) {
622 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
623 return;
624 }
625
626 if (!isa<FunctionDecl>(D)) {
627 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
628 return;
629 }
630
Chris Lattner16ded572009-03-04 06:34:08 +0000631 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorfcb19192009-02-11 23:02:49 +0000632}
633
Steve Naroffe1cecba2008-09-18 16:44:58 +0000634static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
635 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000637 << "blocks" << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000638 return;
639 }
640
641 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000643 return;
644 }
Steve Naroffe1cecba2008-09-18 16:44:58 +0000645
646 BlocksAttr::BlocksAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000647 if (Attr.getParameterName()->isStr("byref"))
Steve Naroffe1cecba2008-09-18 16:44:58 +0000648 type = BlocksAttr::ByRef;
649 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000650 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000651 << "blocks" << Attr.getParameterName();
Steve Naroffe1cecba2008-09-18 16:44:58 +0000652 return;
653 }
654
Chris Lattner16ded572009-03-04 06:34:08 +0000655 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroffe1cecba2008-09-18 16:44:58 +0000656}
657
Anders Carlsson244d99b2008-10-05 18:05:59 +0000658static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
659 // check the attribute arguments.
660 if (Attr.getNumArgs() > 2) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
662 << "0, 1 or 2";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000663 return;
664 }
665
666 int sentinel = 0;
667 if (Attr.getNumArgs() > 0) {
668 Expr *E = static_cast<Expr *>(Attr.getArg(0));
669 llvm::APSInt Idx(32);
670 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000671 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000672 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000673 return;
674 }
675 sentinel = Idx.getZExtValue();
676
677 if (sentinel < 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000678 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
679 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000680 return;
681 }
682 }
683
684 int nullPos = 0;
685 if (Attr.getNumArgs() > 1) {
686 Expr *E = static_cast<Expr *>(Attr.getArg(1));
687 llvm::APSInt Idx(32);
688 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000689 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000690 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000691 return;
692 }
693 nullPos = Idx.getZExtValue();
694
695 if (nullPos > 1 || nullPos < 0) {
696 // FIXME: This error message could be improved, it would be nice
697 // to say what the bounds actually are.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000698 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
699 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000700 return;
701 }
702 }
703
704 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner19252782009-03-17 23:03:47 +0000705 const FunctionType *FT = FD->getType()->getAsFunctionType();
706 assert(FT && "FunctionDecl has non-function type?");
707
708 if (isa<FunctionNoProtoType>(FT)) {
709 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
710 return;
711 }
712
713 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Anders Carlsson244d99b2008-10-05 18:05:59 +0000714 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
715 return;
716 }
717 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
718 if (!MD->isVariadic()) {
719 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
720 return;
721 }
722 } else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000723 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000724 << "sentinel" << 3 /*function or method*/;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000725 return;
726 }
727
728 // FIXME: Actually create the attribute.
729}
730
Chris Lattnerd2c66552009-02-14 07:37:35 +0000731static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
732 // check the attribute arguments.
733 if (Attr.getNumArgs() != 0) {
734 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
735 return;
736 }
737
738 // TODO: could also be applied to methods?
739 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
740 if (!Fn) {
741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
742 << "warn_unused_result" << 0 /*function*/;
743 return;
744 }
745
Chris Lattner16ded572009-03-04 06:34:08 +0000746 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattnerd2c66552009-02-14 07:37:35 +0000747}
748
749static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000750 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000751 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000753 return;
754 }
Daniel Dunbar42e41392009-03-06 06:39:57 +0000755
756 // TODO: could also be applied to methods?
757 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
759 << "weak" << 2 /*variable and function*/;
760 return;
761 }
Chris Lattner6953a072008-06-26 18:38:35 +0000762
Chris Lattner16ded572009-03-04 06:34:08 +0000763 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000764}
765
Daniel Dunbar42e41392009-03-06 06:39:57 +0000766static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
767 // check the attribute arguments.
768 if (Attr.getNumArgs() != 0) {
769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
770 return;
771 }
772
773 // weak_import only applies to variable & function declarations.
774 bool isDef = false;
775 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
776 isDef = (!VD->hasExternalStorage() || VD->getInit());
777 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregore3241e92009-04-18 00:02:19 +0000778 isDef = FD->getBody(S.Context);
Fariborz Jahaniane1d5e5a2009-05-04 19:35:12 +0000779 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
780 // We ignore weak import on properties and methods
Mike Stump1ba365c2009-03-18 17:39:31 +0000781 return;
Daniel Dunbar42e41392009-03-06 06:39:57 +0000782 } else {
783 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
784 << "weak_import" << 2 /*variable and function*/;
785 return;
786 }
787
788 // Merge should handle any subsequent violations.
789 if (isDef) {
790 S.Diag(Attr.getLoc(),
791 diag::warn_attribute_weak_import_invalid_on_definition)
792 << "weak_import" << 2 /*variable and function*/;
793 return;
794 }
795
796 D->addAttr(::new (S.Context) WeakImportAttr());
797}
798
Chris Lattnerd2c66552009-02-14 07:37:35 +0000799static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000800 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000801 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000802 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000803 return;
804 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000805
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000806 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000807 if (isa<VarDecl>(D)) {
Chris Lattner16ded572009-03-04 06:34:08 +0000808 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000809 return;
810 }
811
Chris Lattnerd2c66552009-02-14 07:37:35 +0000812 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000813 if (!FD) {
814 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000815 << "dllimport" << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000816 return;
817 }
818
819 // Currently, the dllimport attribute is ignored for inlined functions.
820 // Warning is emitted.
821 if (FD->isInline()) {
822 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
823 return;
824 }
825
826 // The attribute is also overridden by a subsequent declaration as dllexport.
827 // Warning is emitted.
828 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
829 nextAttr = nextAttr->getNext()) {
830 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
831 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
832 return;
833 }
834 }
835
Chris Lattnerd2c66552009-02-14 07:37:35 +0000836 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000837 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
838 return;
839 }
840
Chris Lattner16ded572009-03-04 06:34:08 +0000841 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000842}
843
Chris Lattnerd2c66552009-02-14 07:37:35 +0000844static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000845 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000846 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000848 return;
849 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000850
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000851 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000852 if (isa<VarDecl>(D)) {
Chris Lattner16ded572009-03-04 06:34:08 +0000853 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000854 return;
855 }
856
Chris Lattnerd2c66552009-02-14 07:37:35 +0000857 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000858 if (!FD) {
859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000860 << "dllexport" << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000861 return;
862 }
863
864 // Currently, the dllexport attribute is ignored for inlined functions,
865 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
866 if (FD->isInline()) {
867 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
868 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
869 return;
870 }
871
Chris Lattner16ded572009-03-04 06:34:08 +0000872 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000873}
874
Chris Lattnerd2c66552009-02-14 07:37:35 +0000875static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar97509722009-02-12 17:28:23 +0000876 // Attribute has no arguments.
877 if (Attr.getNumArgs() != 1) {
878 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
879 return;
880 }
881
882 // Make sure that there is a string literal as the sections's single
883 // argument.
884 StringLiteral *SE =
885 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
886 if (!SE) {
887 // FIXME
888 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
889 return;
890 }
Chris Lattner16ded572009-03-04 06:34:08 +0000891 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
892 SE->getByteLength())));
Daniel Dunbar97509722009-02-12 17:28:23 +0000893}
894
Chris Lattner703c52d2008-06-29 00:43:07 +0000895static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000896 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000897 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000898 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000899 return;
900 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000901
902 // Attribute can be applied only to functions.
903 if (!isa<FunctionDecl>(d)) {
904 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000905 << "stdcall" << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000906 return;
907 }
908
909 // stdcall and fastcall attributes are mutually incompatible.
910 if (d->getAttr<FastCallAttr>()) {
911 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
912 << "stdcall" << "fastcall";
913 return;
914 }
915
Chris Lattner16ded572009-03-04 06:34:08 +0000916 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000917}
918
Chris Lattner703c52d2008-06-29 00:43:07 +0000919static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000920 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000921 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000922 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000923 return;
924 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000925
926 if (!isa<FunctionDecl>(d)) {
927 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +0000928 << "fastcall" << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000929 return;
930 }
931
932 // stdcall and fastcall attributes are mutually incompatible.
933 if (d->getAttr<StdCallAttr>()) {
934 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
935 << "fastcall" << "stdcall";
936 return;
937 }
938
Chris Lattner16ded572009-03-04 06:34:08 +0000939 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000940}
941
Chris Lattner703c52d2008-06-29 00:43:07 +0000942static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000943 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000944 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000945 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000946 return;
947 }
948
Chris Lattner16ded572009-03-04 06:34:08 +0000949 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000950}
951
Anders Carlssondd6791c2008-10-05 23:32:53 +0000952static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
953 // check the attribute arguments.
954 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000955 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000956 return;
957 }
958
Chris Lattner16ded572009-03-04 06:34:08 +0000959 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssondd6791c2008-10-05 23:32:53 +0000960}
961
962static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
963 // check the attribute arguments.
964 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000966 return;
967 }
968
Chris Lattner16ded572009-03-04 06:34:08 +0000969 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssondd6791c2008-10-05 23:32:53 +0000970}
971
Anders Carlsson677e38f2009-01-31 01:16:18 +0000972static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000973 // Match gcc which ignores cleanup attrs when compiling C++.
974 if (S.getLangOptions().CPlusPlus)
975 return;
976
Anders Carlsson677e38f2009-01-31 01:16:18 +0000977 if (!Attr.getParameterName()) {
978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
979 return;
980 }
981
982 if (Attr.getNumArgs() != 0) {
983 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
984 return;
985 }
986
987 VarDecl *VD = dyn_cast<VarDecl>(d);
988
989 if (!VD || !VD->hasLocalStorage()) {
990 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
991 return;
992 }
993
994 // Look up the function
Douglas Gregor09be81b2009-02-04 17:27:36 +0000995 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
996 Sema::LookupOrdinaryName);
Anders Carlsson677e38f2009-01-31 01:16:18 +0000997 if (!CleanupDecl) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000998 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +0000999 Attr.getParameterName();
1000 return;
1001 }
1002
1003 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1004 if (!FD) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001005 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001006 Attr.getParameterName();
1007 return;
1008 }
1009
Anders Carlsson677e38f2009-01-31 01:16:18 +00001010 if (FD->getNumParams() != 1) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001011 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001012 Attr.getParameterName();
1013 return;
1014 }
1015
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001016 // We're currently more strict than GCC about what function types we accept.
1017 // If this ever proves to be a problem it should be easy to fix.
1018 QualType Ty = S.Context.getPointerType(VD->getType());
1019 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedman7ce98682009-04-26 01:30:08 +00001020 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001021 S.Diag(Attr.getLoc(),
1022 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1023 Attr.getParameterName() << ParamTy << Ty;
1024 return;
1025 }
1026
Chris Lattner16ded572009-03-04 06:34:08 +00001027 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlsson677e38f2009-01-31 01:16:18 +00001028}
1029
Chris Lattner6953a072008-06-26 18:38:35 +00001030/// Handle __attribute__((format(type,idx,firstarg))) attributes
1031/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +00001032static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001033
Chris Lattner1c151132008-06-28 23:36:30 +00001034 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +00001036 << "format" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001037 return;
1038 }
1039
Chris Lattner1c151132008-06-28 23:36:30 +00001040 if (Attr.getNumArgs() != 2) {
Chris Lattner65cae292008-11-19 08:23:25 +00001041 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6953a072008-06-26 18:38:35 +00001042 return;
1043 }
1044
Daniel Dunbar0ea20472008-10-19 02:04:16 +00001045 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +00001047 << "format" << 0 /*function*/;
Chris Lattner6953a072008-06-26 18:38:35 +00001048 return;
1049 }
1050
1051 // FIXME: in C++ the implicit 'this' function parameter also counts.
1052 // this is needed in order to be compatible with GCC
1053 // the index must start in 1 and the limit is numargs+1
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001054 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6953a072008-06-26 18:38:35 +00001055 unsigned FirstIdx = 1;
1056
Chris Lattner1c151132008-06-28 23:36:30 +00001057 const char *Format = Attr.getParameterName()->getName();
1058 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +00001059
1060 // Normalize the argument, __foo__ becomes foo.
1061 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1062 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1063 Format += 2;
1064 FormatLen -= 4;
1065 }
1066
1067 bool Supported = false;
1068 bool is_NSString = false;
1069 bool is_strftime = false;
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001070 bool is_CFString = false;
Chris Lattner6953a072008-06-26 18:38:35 +00001071
1072 switch (FormatLen) {
1073 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001074 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1075 case 6: Supported = !memcmp(Format, "printf", 6); break;
1076 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +00001077 case 8:
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001078 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1079 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1080 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001081 break;
1082 }
1083
1084 if (!Supported) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001085 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1086 << "format" << Attr.getParameterName()->getName();
Chris Lattner6953a072008-06-26 18:38:35 +00001087 return;
1088 }
1089
1090 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001091 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +00001092 llvm::APSInt Idx(32);
1093 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001094 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001095 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001096 return;
1097 }
1098
1099 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001100 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001101 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001102 return;
1103 }
1104
1105 // FIXME: Do we need to bounds check?
1106 unsigned ArgIdx = Idx.getZExtValue() - 1;
1107
1108 // make sure the format string is really a string
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001109 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6953a072008-06-26 18:38:35 +00001110
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001111 if (is_CFString) {
1112 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001113 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1114 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001115 return;
1116 }
1117 } else if (is_NSString) {
Chris Lattner6953a072008-06-26 18:38:35 +00001118 // FIXME: do we need to check if the type is NSString*? What are
1119 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +00001120 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001121 // FIXME: Should highlight the actual expression that has the
1122 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001123 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1124 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001125 return;
1126 }
1127 } else if (!Ty->isPointerType() ||
1128 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1129 // FIXME: Should highlight the actual expression that has the
1130 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001131 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1132 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001133 return;
1134 }
1135
1136 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001137 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +00001138 llvm::APSInt FirstArg(32);
1139 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001141 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001142 return;
1143 }
1144
1145 // check if the function is variadic if the 3rd argument non-zero
1146 if (FirstArg != 0) {
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001147 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001148 ++NumArgs; // +1 for ...
1149 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +00001150 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +00001151 return;
1152 }
1153 }
1154
Chris Lattner65cae292008-11-19 08:23:25 +00001155 // strftime requires FirstArg to be 0 because it doesn't read from any
1156 // variable the input is just the current time + the format string.
Chris Lattner6953a072008-06-26 18:38:35 +00001157 if (is_strftime) {
1158 if (FirstArg != 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001159 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1160 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001161 return;
1162 }
1163 // if 0 it disables parameter checking (to use with e.g. va_list)
1164 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001165 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001166 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001167 return;
1168 }
1169
Chris Lattner16ded572009-03-04 06:34:08 +00001170 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6953a072008-06-26 18:38:35 +00001171 Idx.getZExtValue(), FirstArg.getZExtValue()));
1172}
1173
Chris Lattnerf6690152008-06-29 00:28:59 +00001174static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1175 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001176 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001177 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001178 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +00001179 return;
1180 }
1181
Douglas Gregor144b06c2009-04-29 22:16:16 +00001182 // Try to find the underlying union declaration.
1183 RecordDecl *RD = 0;
Eli Friedman283b3622008-09-02 05:19:23 +00001184 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor144b06c2009-04-29 22:16:16 +00001185 if (TD && TD->getUnderlyingType()->isUnionType())
1186 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1187 else
1188 RD = dyn_cast<RecordDecl>(d);
1189
1190 if (!RD || !RD->isUnion()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +00001192 << "transparent_union" << 1 /*union*/;
Chris Lattner6953a072008-06-26 18:38:35 +00001193 return;
1194 }
1195
Douglas Gregor144b06c2009-04-29 22:16:16 +00001196 if (!RD->isDefinition()) {
1197 S.Diag(Attr.getLoc(),
1198 diag::warn_transparent_union_attribute_not_definition);
1199 return;
1200 }
Chris Lattner6953a072008-06-26 18:38:35 +00001201
Douglas Gregor144b06c2009-04-29 22:16:16 +00001202 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1203 FieldEnd = RD->field_end(S.Context);
1204 if (Field == FieldEnd) {
1205 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1206 return;
1207 }
Eli Friedman283b3622008-09-02 05:19:23 +00001208
Douglas Gregor144b06c2009-04-29 22:16:16 +00001209 FieldDecl *FirstField = *Field;
1210 QualType FirstType = FirstField->getType();
1211 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1212 S.Diag(FirstField->getLocation(),
1213 diag::warn_transparent_union_attribute_floating);
1214 return;
1215 }
1216
1217 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1218 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1219 for (; Field != FieldEnd; ++Field) {
1220 QualType FieldType = Field->getType();
1221 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1222 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1223 // Warn if we drop the attribute.
1224 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1225 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1226 : S.Context.getTypeAlign(FieldType);
1227 S.Diag(Field->getLocation(),
1228 diag::warn_transparent_union_attribute_field_size_align)
1229 << isSize << Field->getDeclName() << FieldBits;
1230 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1231 S.Diag(FirstField->getLocation(),
1232 diag::note_transparent_union_first_field_size_align)
1233 << isSize << FirstBits;
Eli Friedman283b3622008-09-02 05:19:23 +00001234 return;
1235 }
1236 }
1237
Douglas Gregor144b06c2009-04-29 22:16:16 +00001238 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6953a072008-06-26 18:38:35 +00001239}
1240
Chris Lattnerf6690152008-06-29 00:28:59 +00001241static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001242 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001243 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001244 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001245 return;
1246 }
Chris Lattner1c151132008-06-28 23:36:30 +00001247 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +00001248 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1249
1250 // Make sure that there is a string literal as the annotation's single
1251 // argument.
1252 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001253 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +00001254 return;
1255 }
Chris Lattner16ded572009-03-04 06:34:08 +00001256 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1257 SE->getByteLength())));
Chris Lattner6953a072008-06-26 18:38:35 +00001258}
1259
Chris Lattner703c52d2008-06-29 00:43:07 +00001260static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001261 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001262 if (Attr.getNumArgs() > 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001264 return;
1265 }
1266
1267 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +00001268 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +00001269 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbare892c042009-02-18 20:06:09 +00001270 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6953a072008-06-26 18:38:35 +00001271 Align = 128;
Chris Lattner16ded572009-03-04 06:34:08 +00001272 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6953a072008-06-26 18:38:35 +00001273 return;
Chris Lattner6953a072008-06-26 18:38:35 +00001274 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001275
1276 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1277 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +00001278 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001279 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1280 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001281 return;
1282 }
Daniel Dunbar3ed413a2009-02-16 23:37:57 +00001283 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1284 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1285 << alignmentExpr->getSourceRange();
1286 return;
1287 }
1288
Chris Lattner16ded572009-03-04 06:34:08 +00001289 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001290}
Chris Lattnerdc789562008-06-27 22:18:37 +00001291
Chris Lattnerf6690152008-06-29 00:28:59 +00001292/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +00001293/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +00001294///
1295/// Despite what would be logical, the mode attribute is a decl attribute,
1296/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1297/// 'G' be HImode, not an intermediate pointer.
1298///
Chris Lattnerf6690152008-06-29 00:28:59 +00001299static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +00001300 // This attribute isn't documented, but glibc uses it. It changes
1301 // the width of an int or unsigned int to the specified size.
1302
1303 // Check that there aren't any arguments
1304 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001305 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerdc789562008-06-27 22:18:37 +00001306 return;
1307 }
1308
1309 IdentifierInfo *Name = Attr.getParameterName();
1310 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +00001312 return;
1313 }
1314 const char *Str = Name->getName();
1315 unsigned Len = Name->getLength();
1316
1317 // Normalize the attribute name, __foo__ becomes foo.
1318 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1319 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1320 Str += 2;
1321 Len -= 4;
1322 }
1323
1324 unsigned DestWidth = 0;
1325 bool IntegerMode = true;
Eli Friedman29fa4262009-03-03 06:41:03 +00001326 bool ComplexMode = false;
Chris Lattnerdc789562008-06-27 22:18:37 +00001327 switch (Len) {
1328 case 2:
Eli Friedman29fa4262009-03-03 06:41:03 +00001329 switch (Str[0]) {
1330 case 'Q': DestWidth = 8; break;
1331 case 'H': DestWidth = 16; break;
1332 case 'S': DestWidth = 32; break;
1333 case 'D': DestWidth = 64; break;
1334 case 'X': DestWidth = 96; break;
1335 case 'T': DestWidth = 128; break;
1336 }
1337 if (Str[1] == 'F') {
1338 IntegerMode = false;
1339 } else if (Str[1] == 'C') {
1340 IntegerMode = false;
1341 ComplexMode = true;
1342 } else if (Str[1] != 'I') {
1343 DestWidth = 0;
1344 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001345 break;
1346 case 4:
1347 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1348 // pointer on PIC16 and other embedded platforms.
1349 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001350 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001351 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001352 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +00001353 break;
1354 case 7:
1355 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +00001356 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001357 break;
1358 }
1359
1360 QualType OldTy;
1361 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1362 OldTy = TD->getUnderlyingType();
1363 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1364 OldTy = VD->getType();
1365 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001366 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1367 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerdc789562008-06-27 22:18:37 +00001368 return;
1369 }
Eli Friedman29fa4262009-03-03 06:41:03 +00001370
1371 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1372 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1373 else if (IntegerMode) {
1374 if (!OldTy->isIntegralType())
1375 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1376 } else if (ComplexMode) {
1377 if (!OldTy->isComplexType())
1378 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1379 } else {
1380 if (!OldTy->isFloatingType())
1381 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1382 }
1383
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001384 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1385 // int8_t and friends, at least with glibc.
1386 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1387 // the wrong width on unusual platforms.
1388 // FIXME: Make sure floating-point mappings are accurate
1389 // FIXME: Support XF and TF types
Chris Lattnerdc789562008-06-27 22:18:37 +00001390 QualType NewTy;
1391 switch (DestWidth) {
1392 case 0:
Chris Lattner65cae292008-11-19 08:23:25 +00001393 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001394 return;
1395 default:
Chris Lattner65cae292008-11-19 08:23:25 +00001396 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001397 return;
1398 case 8:
Eli Friedman29fa4262009-03-03 06:41:03 +00001399 if (!IntegerMode) {
1400 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1401 return;
1402 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001403 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001404 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001405 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001406 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001407 break;
1408 case 16:
Eli Friedman29fa4262009-03-03 06:41:03 +00001409 if (!IntegerMode) {
1410 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1411 return;
1412 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001413 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001414 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001415 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001416 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001417 break;
1418 case 32:
1419 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001420 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001421 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001422 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001423 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001424 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001425 break;
1426 case 64:
1427 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001428 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001429 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001430 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001431 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001432 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001433 break;
Eli Friedman29fa4262009-03-03 06:41:03 +00001434 case 96:
1435 NewTy = S.Context.LongDoubleTy;
1436 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001437 case 128:
1438 if (!IntegerMode) {
1439 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1440 return;
1441 }
1442 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman29fa4262009-03-03 06:41:03 +00001443 break;
Chris Lattnerdc789562008-06-27 22:18:37 +00001444 }
1445
Eli Friedman29fa4262009-03-03 06:41:03 +00001446 if (ComplexMode) {
1447 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerdc789562008-06-27 22:18:37 +00001448 }
1449
1450 // Install the new type.
1451 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1452 TD->setUnderlyingType(NewTy);
1453 else
1454 cast<ValueDecl>(D)->setType(NewTy);
1455}
Chris Lattnera72440d2008-06-29 00:23:49 +00001456
Anders Carlssonf607f532009-02-13 06:46:13 +00001457static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1458 // check the attribute arguments.
1459 if (Attr.getNumArgs() > 0) {
1460 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1461 return;
1462 }
Anders Carlsson73007792009-02-13 08:11:52 +00001463
Anders Carlssoneb12e782009-02-19 19:16:48 +00001464 if (!isFunctionOrMethod(d)) {
Anders Carlssonf607f532009-02-13 06:46:13 +00001465 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattnerd2c66552009-02-14 07:37:35 +00001466 << "nodebug" << 0 /*function*/;
Anders Carlssonf607f532009-02-13 06:46:13 +00001467 return;
1468 }
1469
Chris Lattner16ded572009-03-04 06:34:08 +00001470 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssonf607f532009-02-13 06:46:13 +00001471}
1472
Anders Carlssoneb12e782009-02-19 19:16:48 +00001473static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1474 // check the attribute arguments.
1475 if (Attr.getNumArgs() != 0) {
1476 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1477 return;
1478 }
1479
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001480 if (!isa<FunctionDecl>(d)) {
Anders Carlssoneb12e782009-02-19 19:16:48 +00001481 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1482 << "noinline" << 0 /*function*/;
1483 return;
1484 }
1485
Chris Lattner16ded572009-03-04 06:34:08 +00001486 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlssoneb12e782009-02-19 19:16:48 +00001487}
1488
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001489static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner429e56a2009-04-14 16:30:50 +00001490 // check the attribute arguments.
1491 if (Attr.getNumArgs() != 0) {
1492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1493 return;
1494 }
1495
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001496 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1497 if (Fn == 0) {
Chris Lattner429e56a2009-04-14 16:30:50 +00001498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001499 << "gnu_inline" << 0 /*function*/;
Chris Lattner429e56a2009-04-14 16:30:50 +00001500 return;
1501 }
1502
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001503 if (!Fn->isInline()) {
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001504 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001505 return;
1506 }
1507
Douglas Gregor67e11442009-04-28 06:37:30 +00001508 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner429e56a2009-04-14 16:30:50 +00001509}
1510
Fariborz Jahanian97703432009-03-27 18:38:55 +00001511static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1512 // check the attribute arguments.
1513 if (Attr.getNumArgs() != 1) {
Eli Friedman404adca2009-03-27 21:06:47 +00001514 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian97703432009-03-27 18:38:55 +00001515 return;
1516 }
Eli Friedman404adca2009-03-27 21:06:47 +00001517
Fariborz Jahanian97703432009-03-27 18:38:55 +00001518 if (!isFunctionOrMethod(d)) {
1519 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1520 << "regparm" << 0 /*function*/;
1521 return;
1522 }
Eli Friedman404adca2009-03-27 21:06:47 +00001523
1524 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1525 llvm::APSInt NumParams(32);
1526 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1527 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1528 << "regparm" << NumParamsExpr->getSourceRange();
1529 return;
1530 }
1531
Anton Korobeynikov8fcffeb2009-04-03 23:38:25 +00001532 if (S.Context.Target.getRegParmMax() == 0) {
1533 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman404adca2009-03-27 21:06:47 +00001534 << NumParamsExpr->getSourceRange();
1535 return;
1536 }
1537
Anton Korobeynikov5f079ba2009-04-04 10:27:50 +00001538 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov8fcffeb2009-04-03 23:38:25 +00001539 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1540 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman404adca2009-03-27 21:06:47 +00001541 return;
1542 }
1543
1544 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanian97703432009-03-27 18:38:55 +00001545}
1546
Chris Lattnera72440d2008-06-29 00:23:49 +00001547//===----------------------------------------------------------------------===//
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001548// Checker-specific attribute handlers.
1549//===----------------------------------------------------------------------===//
1550
1551static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1552 Sema &S) {
1553
1554 if (!isa<ObjCMethodDecl>(d) && !isa<FunctionDecl>(d)) {
1555 const char *name;
1556
1557 switch (Attr.getKind()) {
1558 default:
1559 assert(0 && "invalid ownership attribute");
1560 return;
1561 case AttributeList::AT_cf_returns_retained:
1562 name = "cf_returns_retained"; break;
1563 case AttributeList::AT_ns_returns_retained:
1564 name = "ns_returns_retained"; break;
1565 };
1566
1567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1568 name << 3 /* function or method */;
1569 return;
1570 }
1571
1572 switch (Attr.getKind()) {
1573 default:
1574 assert(0 && "invalid ownership attribute");
1575 return;
1576 case AttributeList::AT_cf_returns_retained:
1577 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1578 return;
1579 case AttributeList::AT_ns_returns_retained:
1580 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1581 return;
1582 };
1583}
1584
1585//===----------------------------------------------------------------------===//
Chris Lattnera72440d2008-06-29 00:23:49 +00001586// Top Level Sema Entry Points
1587//===----------------------------------------------------------------------===//
1588
Sebastian Redl6e67d9b2008-12-21 19:24:58 +00001589/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner703c52d2008-06-29 00:43:07 +00001590/// the attribute applies to decls. If the attribute is a type attribute, just
1591/// silently ignore it.
1592static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1593 switch (Attr.getKind()) {
Daniel Dunbar8716a012008-07-31 22:40:48 +00001594 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001595 case AttributeList::AT_address_space:
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +00001596 case AttributeList::AT_objc_gc:
1597 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner703c52d2008-06-29 00:43:07 +00001598 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001599 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1600 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbar0a2da712008-10-28 00:17:57 +00001601 case AttributeList::AT_always_inline:
1602 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek1ce91f22009-04-10 00:01:14 +00001603 case AttributeList::AT_analyzer_noreturn:
1604 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001605 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1606 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1607 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1608 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1609 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1610 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001611 case AttributeList::AT_ext_vector_type:
1612 HandleExtVectorTypeAttr(D, Attr, S);
1613 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001614 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1615 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001616 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001617 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001618 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1619 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1620 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001621
1622 // Checker-specific.
1623 case AttributeList::AT_ns_returns_retained:
1624 case AttributeList::AT_cf_returns_retained:
1625 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1626
Daniel Dunbar8716a012008-07-31 22:40:48 +00001627 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar97509722009-02-12 17:28:23 +00001628 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001629 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +00001630 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001631 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +00001632 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001633 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001634 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattnerd2c66552009-02-14 07:37:35 +00001635 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1636 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001637 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar42e41392009-03-06 06:39:57 +00001638 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001639 case AttributeList::AT_transparent_union:
1640 HandleTransparentUnionAttr(D, Attr, S);
1641 break;
Chris Lattnera9b98462009-02-14 08:09:34 +00001642 case AttributeList::AT_objc_exception:
1643 HandleObjCExceptionAttr(D, Attr, S);
1644 break;
Douglas Gregorfcb19192009-02-11 23:02:49 +00001645 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanian82f54962009-01-13 23:34:40 +00001646 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroffe1cecba2008-09-18 16:44:58 +00001647 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson244d99b2008-10-05 18:05:59 +00001648 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001649 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1650 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlsson677e38f2009-01-31 01:16:18 +00001651 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssonf607f532009-02-13 06:46:13 +00001652 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlssoneb12e782009-02-19 19:16:48 +00001653 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman404adca2009-03-27 21:06:47 +00001654 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson402735d2009-02-13 08:16:43 +00001655 case AttributeList::IgnoredAttribute:
Chris Lattnerc305c342009-04-25 18:44:54 +00001656 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson402735d2009-02-13 08:16:43 +00001657 // Just ignore
1658 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001659 default:
Anders Carlssonf607f532009-02-13 06:46:13 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner703c52d2008-06-29 00:43:07 +00001661 break;
1662 }
1663}
1664
1665/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1666/// attribute list to the specified decl, ignoring any type attributes.
1667void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1668 while (AttrList) {
1669 ProcessDeclAttribute(D, *AttrList, *this);
1670 AttrList = AttrList->getNext();
1671 }
1672}
1673
Chris Lattnera72440d2008-06-29 00:23:49 +00001674/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1675/// it, apply them to D. This is a bit tricky because PD can have attributes
1676/// specified in many different places, and we need to find and apply them all.
1677void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1678 // Apply decl attributes from the DeclSpec if present.
1679 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1680 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +00001681
Chris Lattnera72440d2008-06-29 00:23:49 +00001682 // Walk the declarator structure, applying decl attributes that were in a type
1683 // position to the decl itself. This handles cases like:
1684 // int *__attr__(x)** D;
1685 // when X is a decl attribute.
1686 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1687 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1688 ProcessDeclAttributeList(D, Attrs);
1689
1690 // Finally, apply any attributes on the decl itself.
1691 if (const AttributeList *Attrs = PD.getAttributes())
1692 ProcessDeclAttributeList(D, Attrs);
1693}