blob: 42a177a497c2adc25c4a0cb673e04aa6e8670f58 [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 }
Chris Lattner2a15f8e2009-05-13 05:13:44 +0000224 // the base type must be integer or float, and can't already be a vector.
225 if (CurType->isVectorType() ||
226 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000227 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000228 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000229 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000230 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-06-26 18:38:35 +0000231 // vecSize is specified in bytes - convert to bits.
232 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
233
234 // the vector size needs to be an integral multiple of the type size.
235 if (vectorSize % typeSize) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000236 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
237 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000238 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000239 }
240 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000241 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
242 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000243 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000244 }
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000245
246 // Success! Instantiate the vector type, the number of elements is > 0, and
247 // not required to be a power of 2, unlike GCC.
Chris Lattner703c52d2008-06-29 00:43:07 +0000248 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000249
250 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
251 VD->setType(CurType);
252 else
253 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6953a072008-06-26 18:38:35 +0000254}
255
Chris Lattner703c52d2008-06-29 00:43:07 +0000256static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000257 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000258 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000259 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000260 return;
261 }
262
263 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner16ded572009-03-04 06:34:08 +0000264 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000265 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
266 // If the alignment is less than or equal to 8 bits, the packed attribute
267 // has no effect.
268 if (!FD->getType()->isIncompleteType() &&
Chris Lattner703c52d2008-06-29 00:43:07 +0000269 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000270 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000271 << Attr.getName() << FD->getType();
Chris Lattner6953a072008-06-26 18:38:35 +0000272 else
Chris Lattner16ded572009-03-04 06:34:08 +0000273 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000274 } else
Chris Lattner65cae292008-11-19 08:23:25 +0000275 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000276}
277
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000278static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
279 // check the attribute arguments.
280 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000282 return;
283 }
284
285 // The IBOutlet attribute only applies to instance variables of Objective-C
286 // classes.
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000287 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner16ded572009-03-04 06:34:08 +0000288 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000289 else
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000290 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000291}
292
Ted Kremenekc0af2542008-07-21 21:53:04 +0000293static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000294 // GCC ignores the nonnull attribute on K&R style function
295 // prototypes, so we ignore it as well
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000296 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000297 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000298 << Attr.getName() << 0 /*function*/;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000299 return;
300 }
301
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000302 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000303
304 // The nonnull attribute only applies to pointers.
305 llvm::SmallVector<unsigned, 10> NonNullArgs;
306
307 for (AttributeList::arg_iterator I=Attr.arg_begin(),
308 E=Attr.arg_end(); I!=E; ++I) {
309
310
311 // The argument must be an integer constant expression.
Ted Kremenek73dc0652008-12-04 19:38:33 +0000312 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000313 llvm::APSInt ArgNum(32);
314 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000315 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
316 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000317 return;
318 }
319
320 unsigned x = (unsigned) ArgNum.getZExtValue();
321
322 if (x < 1 || x > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000323 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner68f621c2008-11-19 07:22:31 +0000324 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000325 return;
326 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000327
328 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000329
330 // Is the function argument a pointer type?
Ted Kremenekba57bd92008-11-18 06:52:58 +0000331 QualType T = getFunctionOrMethodArgType(d, x);
332 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000333 // FIXME: Should also highlight argument in decl.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000334 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
335 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7af441b2008-09-01 19:57:52 +0000336 continue;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000337 }
338
339 NonNullArgs.push_back(x);
340 }
341
Ted Kremenek7af441b2008-09-01 19:57:52 +0000342 // If no arguments were specified to __attribute__((nonnull)) then all
343 // pointer arguments have a nonnull attribute.
344 if (NonNullArgs.empty()) {
Ted Kremenekba57bd92008-11-18 06:52:58 +0000345 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
346 QualType T = getFunctionOrMethodArgType(d, I);
347 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000348 NonNullArgs.push_back(I);
Ted Kremenekba57bd92008-11-18 06:52:58 +0000349 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000350
351 if (NonNullArgs.empty()) {
352 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
353 return;
354 }
Ted Kremenekc0af2542008-07-21 21:53:04 +0000355 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000356
357 unsigned* start = &NonNullArgs[0];
358 unsigned size = NonNullArgs.size();
359 std::sort(start, start + size);
Chris Lattner16ded572009-03-04 06:34:08 +0000360 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekc0af2542008-07-21 21:53:04 +0000361}
362
Chris Lattner703c52d2008-06-29 00:43:07 +0000363static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000364 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000365 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000366 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000367 return;
368 }
369
Chris Lattner1c151132008-06-28 23:36:30 +0000370 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000371 Arg = Arg->IgnoreParenCasts();
372 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
373
374 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000375 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000376 << "alias" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000377 return;
378 }
379
380 const char *Alias = Str->getStrData();
381 unsigned AliasLen = Str->getByteLength();
382
383 // FIXME: check if target symbol exists in current file
384
Chris Lattner16ded572009-03-04 06:34:08 +0000385 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6953a072008-06-26 18:38:35 +0000386}
387
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000388static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
389 Sema &S) {
390 // check the attribute arguments.
391 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000393 return;
394 }
Anders Carlssoneb12e782009-02-19 19:16:48 +0000395
Chris Lattner2a3e43d2009-04-14 17:02:11 +0000396 if (!isa<FunctionDecl>(d)) {
Anders Carlssoneb12e782009-02-19 19:16:48 +0000397 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000398 << Attr.getName() << 0 /*function*/;
Anders Carlssoneb12e782009-02-19 19:16:48 +0000399 return;
400 }
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000401
Chris Lattner16ded572009-03-04 06:34:08 +0000402 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000403}
404
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000405static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000406 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000407 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000408 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000409 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000410 return false;
Chris Lattner6953a072008-06-26 18:38:35 +0000411 }
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000412
Mike Stump115a0722009-04-29 19:03:13 +0000413 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
414 ValueDecl *VD = dyn_cast<ValueDecl>(d);
415 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000417 << Attr.getName() << 0 /*function*/;
Mike Stump115a0722009-04-29 19:03:13 +0000418 return false;
419 }
Chris Lattner6953a072008-06-26 18:38:35 +0000420 }
421
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000422 return true;
423}
424
425static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000426 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000427 d->addAttr(::new (S.Context) NoReturnAttr());
428}
429
430static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
431 Sema &S) {
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000432 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000433 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000434}
435
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000436static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
437 // check the attribute arguments.
438 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000440 return;
441 }
442
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000443 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000445 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000446 return;
447 }
448
Chris Lattner16ded572009-03-04 06:34:08 +0000449 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000450}
451
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000452static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
453 // check the attribute arguments.
454 if (Attr.getNumArgs() != 0) {
455 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
456 return;
457 }
458
459 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbard0cc5242009-02-13 22:48:56 +0000460 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000461 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
462 return;
463 }
464 } else if (!isFunctionOrMethod(d)) {
465 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000466 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000467 return;
468 }
469
Chris Lattner16ded572009-03-04 06:34:08 +0000470 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000471}
472
Daniel Dunbar8716a012008-07-31 22:40:48 +0000473static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
474 // check the attribute arguments.
475 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000476 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
477 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000478 return;
479 }
480
481 int priority = 65535; // FIXME: Do not hardcode such constants.
482 if (Attr.getNumArgs() > 0) {
483 Expr *E = static_cast<Expr *>(Attr.getArg(0));
484 llvm::APSInt Idx(32);
485 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000486 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000487 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000488 return;
489 }
490 priority = Idx.getZExtValue();
491 }
492
Chris Lattner2a3e43d2009-04-14 17:02:11 +0000493 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000494 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000495 << Attr.getName() << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000496 return;
497 }
498
Chris Lattner16ded572009-03-04 06:34:08 +0000499 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar8716a012008-07-31 22:40:48 +0000500}
501
502static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
503 // check the attribute arguments.
504 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000505 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
506 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000507 return;
508 }
509
510 int priority = 65535; // FIXME: Do not hardcode such constants.
511 if (Attr.getNumArgs() > 0) {
512 Expr *E = static_cast<Expr *>(Attr.getArg(0));
513 llvm::APSInt Idx(32);
514 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000515 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000516 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000517 return;
518 }
519 priority = Idx.getZExtValue();
520 }
521
Anders Carlsson03e49652008-08-22 22:10:48 +0000522 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000523 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000524 << Attr.getName() << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000525 return;
526 }
527
Chris Lattner16ded572009-03-04 06:34:08 +0000528 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar8716a012008-07-31 22:40:48 +0000529}
530
Chris Lattner703c52d2008-06-29 00:43:07 +0000531static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000532 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000533 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000534 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000535 return;
536 }
537
Chris Lattner16ded572009-03-04 06:34:08 +0000538 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000539}
540
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000541static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
542 // check the attribute arguments.
543 if (Attr.getNumArgs() != 0) {
544 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
545 return;
546 }
547
Chris Lattner16ded572009-03-04 06:34:08 +0000548 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000549}
550
Chris Lattner703c52d2008-06-29 00:43:07 +0000551static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000552 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000553 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000554 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000555 return;
556 }
557
Chris Lattner1c151132008-06-28 23:36:30 +0000558 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000559 Arg = Arg->IgnoreParenCasts();
560 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
561
562 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000563 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000564 << "visibility" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000565 return;
566 }
567
568 const char *TypeStr = Str->getStrData();
569 unsigned TypeLen = Str->getByteLength();
570 VisibilityAttr::VisibilityTypes type;
571
572 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
573 type = VisibilityAttr::DefaultVisibility;
574 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
575 type = VisibilityAttr::HiddenVisibility;
576 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
577 type = VisibilityAttr::HiddenVisibility; // FIXME
578 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
579 type = VisibilityAttr::ProtectedVisibility;
580 else {
Chris Lattnerb1753422008-11-23 21:45:46 +0000581 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6953a072008-06-26 18:38:35 +0000582 return;
583 }
584
Chris Lattner16ded572009-03-04 06:34:08 +0000585 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6953a072008-06-26 18:38:35 +0000586}
587
Chris Lattnera9b98462009-02-14 08:09:34 +0000588static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
589 Sema &S) {
590 if (Attr.getNumArgs() != 0) {
591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
592 return;
593 }
594
595 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
596 if (OCI == 0) {
597 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
598 return;
599 }
600
Chris Lattner16ded572009-03-04 06:34:08 +0000601 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattnera9b98462009-02-14 08:09:34 +0000602}
603
604static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000605 if (Attr.getNumArgs() != 0) {
606 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
607 return;
608 }
Chris Lattnera9b98462009-02-14 08:09:34 +0000609 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000610 QualType T = TD->getUnderlyingType();
611 if (!T->isPointerType() ||
612 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
613 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
614 return;
615 }
616 }
Chris Lattner16ded572009-03-04 06:34:08 +0000617 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000618}
619
Douglas Gregorfcb19192009-02-11 23:02:49 +0000620static void
621HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
622 if (Attr.getNumArgs() != 0) {
623 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
624 return;
625 }
626
627 if (!isa<FunctionDecl>(D)) {
628 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
629 return;
630 }
631
Chris Lattner16ded572009-03-04 06:34:08 +0000632 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorfcb19192009-02-11 23:02:49 +0000633}
634
Steve Naroffe1cecba2008-09-18 16:44:58 +0000635static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
636 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000637 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000638 << "blocks" << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000639 return;
640 }
641
642 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000644 return;
645 }
Steve Naroffe1cecba2008-09-18 16:44:58 +0000646
647 BlocksAttr::BlocksAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000648 if (Attr.getParameterName()->isStr("byref"))
Steve Naroffe1cecba2008-09-18 16:44:58 +0000649 type = BlocksAttr::ByRef;
650 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000651 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000652 << "blocks" << Attr.getParameterName();
Steve Naroffe1cecba2008-09-18 16:44:58 +0000653 return;
654 }
655
Chris Lattner16ded572009-03-04 06:34:08 +0000656 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroffe1cecba2008-09-18 16:44:58 +0000657}
658
Anders Carlsson244d99b2008-10-05 18:05:59 +0000659static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
660 // check the attribute arguments.
661 if (Attr.getNumArgs() > 2) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000662 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
663 << "0, 1 or 2";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000664 return;
665 }
666
667 int sentinel = 0;
668 if (Attr.getNumArgs() > 0) {
669 Expr *E = static_cast<Expr *>(Attr.getArg(0));
670 llvm::APSInt Idx(32);
671 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000672 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000673 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000674 return;
675 }
676 sentinel = Idx.getZExtValue();
677
678 if (sentinel < 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000679 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
680 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000681 return;
682 }
683 }
684
685 int nullPos = 0;
686 if (Attr.getNumArgs() > 1) {
687 Expr *E = static_cast<Expr *>(Attr.getArg(1));
688 llvm::APSInt Idx(32);
689 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000691 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000692 return;
693 }
694 nullPos = Idx.getZExtValue();
695
696 if (nullPos > 1 || nullPos < 0) {
697 // FIXME: This error message could be improved, it would be nice
698 // to say what the bounds actually are.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000699 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
700 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000701 return;
702 }
703 }
704
705 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner19252782009-03-17 23:03:47 +0000706 const FunctionType *FT = FD->getType()->getAsFunctionType();
707 assert(FT && "FunctionDecl has non-function type?");
708
709 if (isa<FunctionNoProtoType>(FT)) {
710 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
711 return;
712 }
713
714 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6dac16d2009-05-15 21:18:04 +0000715 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000716 return;
717 }
718 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
719 if (!MD->isVariadic()) {
Fariborz Jahanian6dac16d2009-05-15 21:18:04 +0000720 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000721 return;
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000722 }
723 } else if (isa<BlockDecl>(d)) {
724 // Note! BlockDecl is typeless. Variadic diagnostics
725 // will be issued by the caller.
726 ;
727 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000728 QualType Ty = V->getType();
Fariborz Jahanianc10357d2009-05-15 20:33:25 +0000729 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
730 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
731 : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000732 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6dac16d2009-05-15 21:18:04 +0000733 int m = Ty->isFunctionPointerType() ? 0 : 1;
734 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000735 return;
736 }
737 }
738 else {
739 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian8dde59b2009-05-14 20:57:28 +0000740 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000741 return;
742 }
Anders Carlsson244d99b2008-10-05 18:05:59 +0000743 } else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian8dde59b2009-05-14 20:57:28 +0000745 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000746 return;
747 }
Fariborz Jahanian79d29e72009-05-13 23:20:50 +0000748 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson244d99b2008-10-05 18:05:59 +0000749}
750
Chris Lattnerd2c66552009-02-14 07:37:35 +0000751static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
752 // check the attribute arguments.
753 if (Attr.getNumArgs() != 0) {
754 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
755 return;
756 }
757
758 // TODO: could also be applied to methods?
759 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
760 if (!Fn) {
761 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000762 << Attr.getName() << 0 /*function*/;
Chris Lattnerd2c66552009-02-14 07:37:35 +0000763 return;
764 }
765
Chris Lattner16ded572009-03-04 06:34:08 +0000766 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattnerd2c66552009-02-14 07:37:35 +0000767}
768
769static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000770 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000771 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000772 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000773 return;
774 }
Daniel Dunbar42e41392009-03-06 06:39:57 +0000775
776 // TODO: could also be applied to methods?
777 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
778 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000779 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar42e41392009-03-06 06:39:57 +0000780 return;
781 }
Chris Lattner6953a072008-06-26 18:38:35 +0000782
Chris Lattner16ded572009-03-04 06:34:08 +0000783 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000784}
785
Daniel Dunbar42e41392009-03-06 06:39:57 +0000786static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
787 // check the attribute arguments.
788 if (Attr.getNumArgs() != 0) {
789 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
790 return;
791 }
792
793 // weak_import only applies to variable & function declarations.
794 bool isDef = false;
795 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
796 isDef = (!VD->hasExternalStorage() || VD->getInit());
797 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregore3241e92009-04-18 00:02:19 +0000798 isDef = FD->getBody(S.Context);
Fariborz Jahaniane1d5e5a2009-05-04 19:35:12 +0000799 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
800 // We ignore weak import on properties and methods
Mike Stump1ba365c2009-03-18 17:39:31 +0000801 return;
Daniel Dunbar42e41392009-03-06 06:39:57 +0000802 } else {
803 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000804 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar42e41392009-03-06 06:39:57 +0000805 return;
806 }
807
808 // Merge should handle any subsequent violations.
809 if (isDef) {
810 S.Diag(Attr.getLoc(),
811 diag::warn_attribute_weak_import_invalid_on_definition)
812 << "weak_import" << 2 /*variable and function*/;
813 return;
814 }
815
816 D->addAttr(::new (S.Context) WeakImportAttr());
817}
818
Chris Lattnerd2c66552009-02-14 07:37:35 +0000819static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000820 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000821 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000822 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000823 return;
824 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000825
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000826 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000827 if (isa<VarDecl>(D)) {
Chris Lattner16ded572009-03-04 06:34:08 +0000828 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000829 return;
830 }
831
Chris Lattnerd2c66552009-02-14 07:37:35 +0000832 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000833 if (!FD) {
834 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000835 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000836 return;
837 }
838
839 // Currently, the dllimport attribute is ignored for inlined functions.
840 // Warning is emitted.
841 if (FD->isInline()) {
842 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
843 return;
844 }
845
846 // The attribute is also overridden by a subsequent declaration as dllexport.
847 // Warning is emitted.
848 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
849 nextAttr = nextAttr->getNext()) {
850 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
851 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
852 return;
853 }
854 }
855
Chris Lattnerd2c66552009-02-14 07:37:35 +0000856 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000857 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
858 return;
859 }
860
Chris Lattner16ded572009-03-04 06:34:08 +0000861 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000862}
863
Chris Lattnerd2c66552009-02-14 07:37:35 +0000864static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000865 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000866 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000867 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000868 return;
869 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000870
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000871 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000872 if (isa<VarDecl>(D)) {
Chris Lattner16ded572009-03-04 06:34:08 +0000873 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000874 return;
875 }
876
Chris Lattnerd2c66552009-02-14 07:37:35 +0000877 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000878 if (!FD) {
879 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000880 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000881 return;
882 }
883
884 // Currently, the dllexport attribute is ignored for inlined functions,
885 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
886 if (FD->isInline()) {
887 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
888 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
889 return;
890 }
891
Chris Lattner16ded572009-03-04 06:34:08 +0000892 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000893}
894
Chris Lattnerd2c66552009-02-14 07:37:35 +0000895static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar97509722009-02-12 17:28:23 +0000896 // Attribute has no arguments.
897 if (Attr.getNumArgs() != 1) {
898 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
899 return;
900 }
901
902 // Make sure that there is a string literal as the sections's single
903 // argument.
904 StringLiteral *SE =
905 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
906 if (!SE) {
907 // FIXME
908 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
909 return;
910 }
Chris Lattner16ded572009-03-04 06:34:08 +0000911 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
912 SE->getByteLength())));
Daniel Dunbar97509722009-02-12 17:28:23 +0000913}
914
Chris Lattner703c52d2008-06-29 00:43:07 +0000915static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000916 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000917 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000919 return;
920 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000921
922 // Attribute can be applied only to functions.
923 if (!isa<FunctionDecl>(d)) {
924 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000925 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000926 return;
927 }
928
929 // stdcall and fastcall attributes are mutually incompatible.
930 if (d->getAttr<FastCallAttr>()) {
931 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
932 << "stdcall" << "fastcall";
933 return;
934 }
935
Chris Lattner16ded572009-03-04 06:34:08 +0000936 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000937}
938
Chris Lattner703c52d2008-06-29 00:43:07 +0000939static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000940 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000941 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000943 return;
944 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000945
946 if (!isa<FunctionDecl>(d)) {
947 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000948 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000949 return;
950 }
951
952 // stdcall and fastcall attributes are mutually incompatible.
953 if (d->getAttr<StdCallAttr>()) {
954 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
955 << "fastcall" << "stdcall";
956 return;
957 }
958
Chris Lattner16ded572009-03-04 06:34:08 +0000959 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000960}
961
Chris Lattner703c52d2008-06-29 00:43:07 +0000962static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000963 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000964 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000966 return;
967 }
968
Chris Lattner16ded572009-03-04 06:34:08 +0000969 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000970}
971
Anders Carlssondd6791c2008-10-05 23:32:53 +0000972static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
973 // check the attribute arguments.
974 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000976 return;
977 }
978
Chris Lattner16ded572009-03-04 06:34:08 +0000979 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssondd6791c2008-10-05 23:32:53 +0000980}
981
982static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
983 // check the attribute arguments.
984 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000985 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000986 return;
987 }
988
Chris Lattner16ded572009-03-04 06:34:08 +0000989 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssondd6791c2008-10-05 23:32:53 +0000990}
991
Anders Carlsson677e38f2009-01-31 01:16:18 +0000992static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +0000993 // Match gcc which ignores cleanup attrs when compiling C++.
994 if (S.getLangOptions().CPlusPlus)
995 return;
996
Anders Carlsson677e38f2009-01-31 01:16:18 +0000997 if (!Attr.getParameterName()) {
998 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
999 return;
1000 }
1001
1002 if (Attr.getNumArgs() != 0) {
1003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1004 return;
1005 }
1006
1007 VarDecl *VD = dyn_cast<VarDecl>(d);
1008
1009 if (!VD || !VD->hasLocalStorage()) {
1010 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1011 return;
1012 }
1013
1014 // Look up the function
Douglas Gregor09be81b2009-02-04 17:27:36 +00001015 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1016 Sema::LookupOrdinaryName);
Anders Carlsson677e38f2009-01-31 01:16:18 +00001017 if (!CleanupDecl) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001018 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001019 Attr.getParameterName();
1020 return;
1021 }
1022
1023 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1024 if (!FD) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001025 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001026 Attr.getParameterName();
1027 return;
1028 }
1029
Anders Carlsson677e38f2009-01-31 01:16:18 +00001030 if (FD->getNumParams() != 1) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001031 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001032 Attr.getParameterName();
1033 return;
1034 }
1035
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001036 // We're currently more strict than GCC about what function types we accept.
1037 // If this ever proves to be a problem it should be easy to fix.
1038 QualType Ty = S.Context.getPointerType(VD->getType());
1039 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedman7ce98682009-04-26 01:30:08 +00001040 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001041 S.Diag(Attr.getLoc(),
1042 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1043 Attr.getParameterName() << ParamTy << Ty;
1044 return;
1045 }
1046
Chris Lattner16ded572009-03-04 06:34:08 +00001047 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlsson677e38f2009-01-31 01:16:18 +00001048}
1049
Chris Lattner6953a072008-06-26 18:38:35 +00001050/// Handle __attribute__((format(type,idx,firstarg))) attributes
1051/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +00001052static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001053
Chris Lattner1c151132008-06-28 23:36:30 +00001054 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001055 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +00001056 << "format" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001057 return;
1058 }
1059
Chris Lattner1c151132008-06-28 23:36:30 +00001060 if (Attr.getNumArgs() != 2) {
Chris Lattner65cae292008-11-19 08:23:25 +00001061 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6953a072008-06-26 18:38:35 +00001062 return;
1063 }
1064
Daniel Dunbar0ea20472008-10-19 02:04:16 +00001065 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001066 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001067 << Attr.getName() << 0 /*function*/;
Chris Lattner6953a072008-06-26 18:38:35 +00001068 return;
1069 }
1070
1071 // FIXME: in C++ the implicit 'this' function parameter also counts.
1072 // this is needed in order to be compatible with GCC
1073 // the index must start in 1 and the limit is numargs+1
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001074 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6953a072008-06-26 18:38:35 +00001075 unsigned FirstIdx = 1;
1076
Chris Lattner1c151132008-06-28 23:36:30 +00001077 const char *Format = Attr.getParameterName()->getName();
1078 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +00001079
1080 // Normalize the argument, __foo__ becomes foo.
1081 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1082 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1083 Format += 2;
1084 FormatLen -= 4;
1085 }
1086
1087 bool Supported = false;
1088 bool is_NSString = false;
1089 bool is_strftime = false;
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001090 bool is_CFString = false;
Chris Lattner6953a072008-06-26 18:38:35 +00001091
1092 switch (FormatLen) {
1093 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001094 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1095 case 6: Supported = !memcmp(Format, "printf", 6); break;
1096 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +00001097 case 8:
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001098 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1099 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1100 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001101 break;
1102 }
1103
1104 if (!Supported) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001105 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1106 << "format" << Attr.getParameterName()->getName();
Chris Lattner6953a072008-06-26 18:38:35 +00001107 return;
1108 }
1109
1110 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001111 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +00001112 llvm::APSInt Idx(32);
1113 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001114 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001115 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001116 return;
1117 }
1118
1119 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001121 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001122 return;
1123 }
1124
1125 // FIXME: Do we need to bounds check?
1126 unsigned ArgIdx = Idx.getZExtValue() - 1;
1127
1128 // make sure the format string is really a string
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001129 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6953a072008-06-26 18:38:35 +00001130
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001131 if (is_CFString) {
1132 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001133 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1134 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001135 return;
1136 }
1137 } else if (is_NSString) {
Chris Lattner6953a072008-06-26 18:38:35 +00001138 // FIXME: do we need to check if the type is NSString*? What are
1139 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +00001140 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001141 // FIXME: Should highlight the actual expression that has the
1142 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001143 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1144 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001145 return;
1146 }
1147 } else if (!Ty->isPointerType() ||
1148 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1149 // FIXME: Should highlight the actual expression that has the
1150 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001151 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1152 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001153 return;
1154 }
1155
1156 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001157 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +00001158 llvm::APSInt FirstArg(32);
1159 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001161 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001162 return;
1163 }
1164
1165 // check if the function is variadic if the 3rd argument non-zero
1166 if (FirstArg != 0) {
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001167 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001168 ++NumArgs; // +1 for ...
1169 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +00001170 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +00001171 return;
1172 }
1173 }
1174
Chris Lattner65cae292008-11-19 08:23:25 +00001175 // strftime requires FirstArg to be 0 because it doesn't read from any
1176 // variable the input is just the current time + the format string.
Chris Lattner6953a072008-06-26 18:38:35 +00001177 if (is_strftime) {
1178 if (FirstArg != 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001179 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1180 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001181 return;
1182 }
1183 // if 0 it disables parameter checking (to use with e.g. va_list)
1184 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001186 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001187 return;
1188 }
1189
Chris Lattner16ded572009-03-04 06:34:08 +00001190 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6953a072008-06-26 18:38:35 +00001191 Idx.getZExtValue(), FirstArg.getZExtValue()));
1192}
1193
Chris Lattnerf6690152008-06-29 00:28:59 +00001194static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1195 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001196 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001197 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001198 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +00001199 return;
1200 }
1201
Douglas Gregor144b06c2009-04-29 22:16:16 +00001202 // Try to find the underlying union declaration.
1203 RecordDecl *RD = 0;
Eli Friedman283b3622008-09-02 05:19:23 +00001204 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor144b06c2009-04-29 22:16:16 +00001205 if (TD && TD->getUnderlyingType()->isUnionType())
1206 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1207 else
1208 RD = dyn_cast<RecordDecl>(d);
1209
1210 if (!RD || !RD->isUnion()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001212 << Attr.getName() << 1 /*union*/;
Chris Lattner6953a072008-06-26 18:38:35 +00001213 return;
1214 }
1215
Douglas Gregor144b06c2009-04-29 22:16:16 +00001216 if (!RD->isDefinition()) {
1217 S.Diag(Attr.getLoc(),
1218 diag::warn_transparent_union_attribute_not_definition);
1219 return;
1220 }
Chris Lattner6953a072008-06-26 18:38:35 +00001221
Douglas Gregor144b06c2009-04-29 22:16:16 +00001222 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1223 FieldEnd = RD->field_end(S.Context);
1224 if (Field == FieldEnd) {
1225 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1226 return;
1227 }
Eli Friedman283b3622008-09-02 05:19:23 +00001228
Douglas Gregor144b06c2009-04-29 22:16:16 +00001229 FieldDecl *FirstField = *Field;
1230 QualType FirstType = FirstField->getType();
1231 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1232 S.Diag(FirstField->getLocation(),
1233 diag::warn_transparent_union_attribute_floating);
1234 return;
1235 }
1236
1237 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1238 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1239 for (; Field != FieldEnd; ++Field) {
1240 QualType FieldType = Field->getType();
1241 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1242 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1243 // Warn if we drop the attribute.
1244 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1245 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1246 : S.Context.getTypeAlign(FieldType);
1247 S.Diag(Field->getLocation(),
1248 diag::warn_transparent_union_attribute_field_size_align)
1249 << isSize << Field->getDeclName() << FieldBits;
1250 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1251 S.Diag(FirstField->getLocation(),
1252 diag::note_transparent_union_first_field_size_align)
1253 << isSize << FirstBits;
Eli Friedman283b3622008-09-02 05:19:23 +00001254 return;
1255 }
1256 }
1257
Douglas Gregor144b06c2009-04-29 22:16:16 +00001258 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6953a072008-06-26 18:38:35 +00001259}
1260
Chris Lattnerf6690152008-06-29 00:28:59 +00001261static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001262 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001263 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001265 return;
1266 }
Chris Lattner1c151132008-06-28 23:36:30 +00001267 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +00001268 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1269
1270 // Make sure that there is a string literal as the annotation's single
1271 // argument.
1272 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +00001274 return;
1275 }
Chris Lattner16ded572009-03-04 06:34:08 +00001276 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1277 SE->getByteLength())));
Chris Lattner6953a072008-06-26 18:38:35 +00001278}
1279
Chris Lattner703c52d2008-06-29 00:43:07 +00001280static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001281 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001282 if (Attr.getNumArgs() > 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001284 return;
1285 }
1286
1287 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +00001288 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +00001289 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbare892c042009-02-18 20:06:09 +00001290 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6953a072008-06-26 18:38:35 +00001291 Align = 128;
Chris Lattner16ded572009-03-04 06:34:08 +00001292 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6953a072008-06-26 18:38:35 +00001293 return;
Chris Lattner6953a072008-06-26 18:38:35 +00001294 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001295
1296 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1297 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +00001298 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001299 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1300 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001301 return;
1302 }
Daniel Dunbar3ed413a2009-02-16 23:37:57 +00001303 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1304 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1305 << alignmentExpr->getSourceRange();
1306 return;
1307 }
1308
Chris Lattner16ded572009-03-04 06:34:08 +00001309 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001310}
Chris Lattnerdc789562008-06-27 22:18:37 +00001311
Chris Lattnerf6690152008-06-29 00:28:59 +00001312/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +00001313/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +00001314///
1315/// Despite what would be logical, the mode attribute is a decl attribute,
1316/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1317/// 'G' be HImode, not an intermediate pointer.
1318///
Chris Lattnerf6690152008-06-29 00:28:59 +00001319static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +00001320 // This attribute isn't documented, but glibc uses it. It changes
1321 // the width of an int or unsigned int to the specified size.
1322
1323 // Check that there aren't any arguments
1324 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerdc789562008-06-27 22:18:37 +00001326 return;
1327 }
1328
1329 IdentifierInfo *Name = Attr.getParameterName();
1330 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001331 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +00001332 return;
1333 }
1334 const char *Str = Name->getName();
1335 unsigned Len = Name->getLength();
1336
1337 // Normalize the attribute name, __foo__ becomes foo.
1338 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1339 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1340 Str += 2;
1341 Len -= 4;
1342 }
1343
1344 unsigned DestWidth = 0;
1345 bool IntegerMode = true;
Eli Friedman29fa4262009-03-03 06:41:03 +00001346 bool ComplexMode = false;
Chris Lattnerdc789562008-06-27 22:18:37 +00001347 switch (Len) {
1348 case 2:
Eli Friedman29fa4262009-03-03 06:41:03 +00001349 switch (Str[0]) {
1350 case 'Q': DestWidth = 8; break;
1351 case 'H': DestWidth = 16; break;
1352 case 'S': DestWidth = 32; break;
1353 case 'D': DestWidth = 64; break;
1354 case 'X': DestWidth = 96; break;
1355 case 'T': DestWidth = 128; break;
1356 }
1357 if (Str[1] == 'F') {
1358 IntegerMode = false;
1359 } else if (Str[1] == 'C') {
1360 IntegerMode = false;
1361 ComplexMode = true;
1362 } else if (Str[1] != 'I') {
1363 DestWidth = 0;
1364 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001365 break;
1366 case 4:
1367 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1368 // pointer on PIC16 and other embedded platforms.
1369 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001370 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001371 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001372 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +00001373 break;
1374 case 7:
1375 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +00001376 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001377 break;
1378 }
1379
1380 QualType OldTy;
1381 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1382 OldTy = TD->getUnderlyingType();
1383 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1384 OldTy = VD->getType();
1385 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001386 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1387 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerdc789562008-06-27 22:18:37 +00001388 return;
1389 }
Eli Friedman29fa4262009-03-03 06:41:03 +00001390
1391 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1392 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1393 else if (IntegerMode) {
1394 if (!OldTy->isIntegralType())
1395 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1396 } else if (ComplexMode) {
1397 if (!OldTy->isComplexType())
1398 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1399 } else {
1400 if (!OldTy->isFloatingType())
1401 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1402 }
1403
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001404 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1405 // int8_t and friends, at least with glibc.
1406 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1407 // the wrong width on unusual platforms.
1408 // FIXME: Make sure floating-point mappings are accurate
1409 // FIXME: Support XF and TF types
Chris Lattnerdc789562008-06-27 22:18:37 +00001410 QualType NewTy;
1411 switch (DestWidth) {
1412 case 0:
Chris Lattner65cae292008-11-19 08:23:25 +00001413 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001414 return;
1415 default:
Chris Lattner65cae292008-11-19 08:23:25 +00001416 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001417 return;
1418 case 8:
Eli Friedman29fa4262009-03-03 06:41:03 +00001419 if (!IntegerMode) {
1420 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1421 return;
1422 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001423 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001424 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001425 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001426 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001427 break;
1428 case 16:
Eli Friedman29fa4262009-03-03 06:41:03 +00001429 if (!IntegerMode) {
1430 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1431 return;
1432 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001433 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001434 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001435 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001436 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001437 break;
1438 case 32:
1439 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001440 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001441 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001442 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001443 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001444 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001445 break;
1446 case 64:
1447 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001448 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001449 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001450 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001451 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001452 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001453 break;
Eli Friedman29fa4262009-03-03 06:41:03 +00001454 case 96:
1455 NewTy = S.Context.LongDoubleTy;
1456 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001457 case 128:
1458 if (!IntegerMode) {
1459 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1460 return;
1461 }
1462 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman29fa4262009-03-03 06:41:03 +00001463 break;
Chris Lattnerdc789562008-06-27 22:18:37 +00001464 }
1465
Eli Friedman29fa4262009-03-03 06:41:03 +00001466 if (ComplexMode) {
1467 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerdc789562008-06-27 22:18:37 +00001468 }
1469
1470 // Install the new type.
1471 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1472 TD->setUnderlyingType(NewTy);
1473 else
1474 cast<ValueDecl>(D)->setType(NewTy);
1475}
Chris Lattnera72440d2008-06-29 00:23:49 +00001476
Anders Carlssonf607f532009-02-13 06:46:13 +00001477static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1478 // check the attribute arguments.
1479 if (Attr.getNumArgs() > 0) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1481 return;
1482 }
Anders Carlsson73007792009-02-13 08:11:52 +00001483
Anders Carlssoneb12e782009-02-19 19:16:48 +00001484 if (!isFunctionOrMethod(d)) {
Anders Carlssonf607f532009-02-13 06:46:13 +00001485 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001486 << Attr.getName() << 0 /*function*/;
Anders Carlssonf607f532009-02-13 06:46:13 +00001487 return;
1488 }
1489
Chris Lattner16ded572009-03-04 06:34:08 +00001490 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssonf607f532009-02-13 06:46:13 +00001491}
1492
Anders Carlssoneb12e782009-02-19 19:16:48 +00001493static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1494 // check the attribute arguments.
1495 if (Attr.getNumArgs() != 0) {
1496 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1497 return;
1498 }
1499
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001500 if (!isa<FunctionDecl>(d)) {
Anders Carlssoneb12e782009-02-19 19:16:48 +00001501 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001502 << Attr.getName() << 0 /*function*/;
Anders Carlssoneb12e782009-02-19 19:16:48 +00001503 return;
1504 }
1505
Chris Lattner16ded572009-03-04 06:34:08 +00001506 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlssoneb12e782009-02-19 19:16:48 +00001507}
1508
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001509static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner429e56a2009-04-14 16:30:50 +00001510 // check the attribute arguments.
1511 if (Attr.getNumArgs() != 0) {
1512 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1513 return;
1514 }
1515
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001516 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1517 if (Fn == 0) {
Chris Lattner429e56a2009-04-14 16:30:50 +00001518 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001519 << Attr.getName() << 0 /*function*/;
Chris Lattner429e56a2009-04-14 16:30:50 +00001520 return;
1521 }
1522
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001523 if (!Fn->isInline()) {
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001524 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001525 return;
1526 }
1527
Douglas Gregor67e11442009-04-28 06:37:30 +00001528 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner429e56a2009-04-14 16:30:50 +00001529}
1530
Fariborz Jahanian97703432009-03-27 18:38:55 +00001531static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1532 // check the attribute arguments.
1533 if (Attr.getNumArgs() != 1) {
Eli Friedman404adca2009-03-27 21:06:47 +00001534 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian97703432009-03-27 18:38:55 +00001535 return;
1536 }
Eli Friedman404adca2009-03-27 21:06:47 +00001537
Fariborz Jahanian97703432009-03-27 18:38:55 +00001538 if (!isFunctionOrMethod(d)) {
1539 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001540 << Attr.getName() << 0 /*function*/;
Fariborz Jahanian97703432009-03-27 18:38:55 +00001541 return;
1542 }
Eli Friedman404adca2009-03-27 21:06:47 +00001543
1544 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1545 llvm::APSInt NumParams(32);
1546 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1547 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1548 << "regparm" << NumParamsExpr->getSourceRange();
1549 return;
1550 }
1551
Anton Korobeynikov8fcffeb2009-04-03 23:38:25 +00001552 if (S.Context.Target.getRegParmMax() == 0) {
1553 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman404adca2009-03-27 21:06:47 +00001554 << NumParamsExpr->getSourceRange();
1555 return;
1556 }
1557
Anton Korobeynikov5f079ba2009-04-04 10:27:50 +00001558 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov8fcffeb2009-04-03 23:38:25 +00001559 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1560 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman404adca2009-03-27 21:06:47 +00001561 return;
1562 }
1563
1564 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanian97703432009-03-27 18:38:55 +00001565}
1566
Chris Lattnera72440d2008-06-29 00:23:49 +00001567//===----------------------------------------------------------------------===//
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001568// Checker-specific attribute handlers.
1569//===----------------------------------------------------------------------===//
1570
1571static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1572 Sema &S) {
1573
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001574 QualType RetTy;
1575
1576 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1577 RetTy = MD->getResultType();
1578 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1579 RetTy = FD->getResultType();
1580 else {
1581 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1582 << Attr.getName() << 3 /* function or method */;
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001583 return;
1584 }
1585
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001586 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1587 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1588 << Attr.getName();
1589 return;
1590 }
1591
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001592 switch (Attr.getKind()) {
1593 default:
1594 assert(0 && "invalid ownership attribute");
1595 return;
1596 case AttributeList::AT_cf_returns_retained:
1597 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1598 return;
1599 case AttributeList::AT_ns_returns_retained:
1600 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1601 return;
1602 };
1603}
1604
1605//===----------------------------------------------------------------------===//
Chris Lattnera72440d2008-06-29 00:23:49 +00001606// Top Level Sema Entry Points
1607//===----------------------------------------------------------------------===//
1608
Sebastian Redl6e67d9b2008-12-21 19:24:58 +00001609/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner703c52d2008-06-29 00:43:07 +00001610/// the attribute applies to decls. If the attribute is a type attribute, just
1611/// silently ignore it.
1612static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1613 switch (Attr.getKind()) {
Daniel Dunbar8716a012008-07-31 22:40:48 +00001614 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001615 case AttributeList::AT_address_space:
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +00001616 case AttributeList::AT_objc_gc:
1617 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner703c52d2008-06-29 00:43:07 +00001618 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001619 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1620 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbar0a2da712008-10-28 00:17:57 +00001621 case AttributeList::AT_always_inline:
1622 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek1ce91f22009-04-10 00:01:14 +00001623 case AttributeList::AT_analyzer_noreturn:
1624 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001625 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1626 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1627 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1628 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1629 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1630 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001631 case AttributeList::AT_ext_vector_type:
1632 HandleExtVectorTypeAttr(D, Attr, S);
1633 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001634 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1635 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001636 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001637 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001638 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1639 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1640 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001641
1642 // Checker-specific.
1643 case AttributeList::AT_ns_returns_retained:
1644 case AttributeList::AT_cf_returns_retained:
1645 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1646
Daniel Dunbar8716a012008-07-31 22:40:48 +00001647 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar97509722009-02-12 17:28:23 +00001648 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001649 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +00001650 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001651 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +00001652 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001653 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001654 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattnerd2c66552009-02-14 07:37:35 +00001655 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1656 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001657 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar42e41392009-03-06 06:39:57 +00001658 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001659 case AttributeList::AT_transparent_union:
1660 HandleTransparentUnionAttr(D, Attr, S);
1661 break;
Chris Lattnera9b98462009-02-14 08:09:34 +00001662 case AttributeList::AT_objc_exception:
1663 HandleObjCExceptionAttr(D, Attr, S);
1664 break;
Douglas Gregorfcb19192009-02-11 23:02:49 +00001665 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanian82f54962009-01-13 23:34:40 +00001666 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroffe1cecba2008-09-18 16:44:58 +00001667 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson244d99b2008-10-05 18:05:59 +00001668 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001669 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1670 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlsson677e38f2009-01-31 01:16:18 +00001671 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssonf607f532009-02-13 06:46:13 +00001672 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlssoneb12e782009-02-19 19:16:48 +00001673 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman404adca2009-03-27 21:06:47 +00001674 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson402735d2009-02-13 08:16:43 +00001675 case AttributeList::IgnoredAttribute:
Chris Lattnerc305c342009-04-25 18:44:54 +00001676 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson402735d2009-02-13 08:16:43 +00001677 // Just ignore
1678 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001679 default:
Anders Carlssonf607f532009-02-13 06:46:13 +00001680 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner703c52d2008-06-29 00:43:07 +00001681 break;
1682 }
1683}
1684
1685/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1686/// attribute list to the specified decl, ignoring any type attributes.
1687void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1688 while (AttrList) {
1689 ProcessDeclAttribute(D, *AttrList, *this);
1690 AttrList = AttrList->getNext();
1691 }
1692}
1693
Chris Lattnera72440d2008-06-29 00:23:49 +00001694/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1695/// it, apply them to D. This is a bit tricky because PD can have attributes
1696/// specified in many different places, and we need to find and apply them all.
1697void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1698 // Apply decl attributes from the DeclSpec if present.
1699 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1700 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +00001701
Chris Lattnera72440d2008-06-29 00:23:49 +00001702 // Walk the declarator structure, applying decl attributes that were in a type
1703 // position to the decl itself. This handles cases like:
1704 // int *__attr__(x)** D;
1705 // when X is a decl attribute.
1706 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1707 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1708 ProcessDeclAttributeList(D, Attrs);
1709
1710 // Finally, apply any attributes on the decl itself.
1711 if (const AttributeList *Attrs = PD.getAttributes())
1712 ProcessDeclAttributeList(D, Attrs);
1713}