blob: 2c2e4d3c0f1860b77a5a4b7bb451723182a6e699 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000027static const FunctionType *getFunctionType(Decl *d, bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30 Ty = decl->getType();
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32 Ty = decl->getType();
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
35 else
36 return 0;
37
38 if (Ty->isFunctionPointerType())
39 Ty = Ty->getAsPointerType()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000040 else if (blocksToo && Ty->isBlockPointerType())
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000041 Ty = Ty->getAsBlockPointerType()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000042
43 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000044}
45
Daniel Dunbar35682492008-09-26 04:12:28 +000046// FIXME: We should provide an abstraction around a method or function
47// to provide the following bits of information.
48
Daniel Dunbard3f2c102008-10-19 02:04:16 +000049/// isFunctionOrMethod - Return true if the given decl has function
50/// type (function or function-typed variable) or an Objective-C
51/// method.
Daniel Dunbar35682492008-09-26 04:12:28 +000052static bool isFunctionOrMethod(Decl *d) {
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000053 return getFunctionType(d, false) || isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000054}
55
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000056/// isFunctionOrMethodOrBlock - Return true if the given decl has function
57/// type (function or function-typed variable) or an Objective-C
58/// method or a block.
59static bool isFunctionOrMethodOrBlock(Decl *d) {
60 if (isFunctionOrMethod(d))
61 return true;
62 // check for block is more involved.
63 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
64 QualType Ty = V->getType();
65 return Ty->isBlockPointerType();
66 }
67 return false;
68}
69
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070/// hasFunctionProto - Return true if the given decl has a argument
71/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000072/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Daniel Dunbard3f2c102008-10-19 02:04:16 +000073static bool hasFunctionProto(Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000074 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000075 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076 else {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000077 assert(isa<ObjCMethodDecl>(d));
78 return true;
79 }
80}
81
82/// getFunctionOrMethodNumArgs - Return number of function or method
83/// arguments. It is an error to call this on a K&R function (use
84/// hasFunctionProto first).
Daniel Dunbar35682492008-09-26 04:12:28 +000085static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000086 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000087 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chris Lattner89951a82009-02-20 18:43:26 +000088 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000089}
90
91static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +000092 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000093 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chris Lattner89951a82009-02-20 18:43:26 +000094
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095
Chris Lattner89951a82009-02-20 18:43:26 +000096 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +000097}
98
99static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000100 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000101 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000102 return proto->isVariadic();
103 } else {
104 return cast<ObjCMethodDecl>(d)->isVariadic();
105 }
106}
107
Chris Lattner6b6b5372008-06-26 18:38:35 +0000108static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000109 const PointerType *PT = T->getAsPointerType();
110 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000111 return false;
112
Chris Lattnerb77792e2008-07-26 22:17:49 +0000113 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000114 if (!ClsT)
115 return false;
116
117 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
118
119 // FIXME: Should we walk the chain of classes?
120 return ClsName == &Ctx.Idents.get("NSString") ||
121 ClsName == &Ctx.Idents.get("NSMutableString");
122}
123
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000124static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
125 const PointerType *PT = T->getAsPointerType();
126 if (!PT)
127 return false;
128
129 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
130 if (!RT)
131 return false;
132
133 const RecordDecl *RD = RT->getDecl();
134 if (RD->getTagKind() != TagDecl::TK_struct)
135 return false;
136
137 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
138}
139
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000140//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000141// Attribute Implementations
142//===----------------------------------------------------------------------===//
143
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000144// FIXME: All this manual attribute parsing code is gross. At the
145// least add some helper functions to check most argument patterns (#
146// and types of args).
147
Chris Lattner803d0802008-06-29 00:43:07 +0000148static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
149 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000150 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
151 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000152 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000153 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000154 }
155
Chris Lattner6b6b5372008-06-26 18:38:35 +0000156 QualType curType = tDecl->getUnderlyingType();
157 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000158 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000159 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000160 return;
161 }
Chris Lattner545dd342008-06-28 23:36:30 +0000162 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000163 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000164 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000165 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
166 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167 return;
168 }
169 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
170 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000171 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000172 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 return;
174 }
175 // unlike gcc's vector_size attribute, the size is specified as the
176 // number of elements, not the number of bytes.
177 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
178
179 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000180 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
181 << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000182 return;
183 }
184 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000185 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000186 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000187 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000188}
189
Chris Lattner065c5a82008-06-28 23:48:25 +0000190
191/// HandleVectorSizeAttribute - this attribute is only applicable to
192/// integral and float scalars, although arrays, pointers, and function
193/// return values are allowed in conjunction with this construct. Aggregates
194/// with this attribute are invalid, even if they are of the same size as a
195/// corresponding scalar.
196/// The raw attribute should contain precisely 1 argument, the vector size
197/// for the variable, measured in bytes. If curType and rawAttr are well
198/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000199static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000200 QualType CurType;
201 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
202 CurType = VD->getType();
203 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
204 CurType = TD->getUnderlyingType();
205 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000206 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
207 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000208 return;
209 }
210
211 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000212 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000214 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 }
Chris Lattner545dd342008-06-28 23:36:30 +0000216 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000217 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000218 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
220 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000221 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 }
223 // navigate to the base type - we need to provide for vector pointers,
224 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000225 if (CurType->isPointerType() || CurType->isArrayType() ||
226 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000227 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
228 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000229 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
230 do {
231 if (PointerType *PT = dyn_cast<PointerType>(canonType))
232 canonType = PT->getPointeeType().getTypePtr();
233 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
234 canonType = AT->getElementType().getTypePtr();
235 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
236 canonType = FT->getResultType().getTypePtr();
237 } while (canonType->isPointerType() || canonType->isArrayType() ||
238 canonType->isFunctionType());
239 */
240 }
Chris Lattner82afa2d2009-05-13 05:13:44 +0000241 // the base type must be integer or float, and can't already be a vector.
242 if (CurType->isVectorType() ||
243 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000244 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000245 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000246 }
Chris Lattner803d0802008-06-29 00:43:07 +0000247 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000248 // vecSize is specified in bytes - convert to bits.
249 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
250
251 // the vector size needs to be an integral multiple of the type size.
252 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000253 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
254 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000255 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000256 }
257 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000258 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
259 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000260 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000261 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000262
263 // Success! Instantiate the vector type, the number of elements is > 0, and
264 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000265 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000266
267 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
268 VD->setType(CurType);
269 else
270 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000271}
272
Chris Lattner803d0802008-06-29 00:43:07 +0000273static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000274 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000275 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000276 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000277 return;
278 }
279
280 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000281 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000282 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
283 // If the alignment is less than or equal to 8 bits, the packed attribute
284 // has no effect.
285 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000286 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000287 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000288 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000289 else
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000290 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000291 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000292 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000293}
294
Ted Kremenek96329d42008-07-15 22:26:48 +0000295static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
296 // check the attribute arguments.
297 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000298 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000299 return;
300 }
301
302 // The IBOutlet attribute only applies to instance variables of Objective-C
303 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000304 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000305 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000306 else
Ted Kremenek32742602009-02-17 22:20:20 +0000307 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000308}
309
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000310static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000311 // GCC ignores the nonnull attribute on K&R style function
312 // prototypes, so we ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000313 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000314 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000315 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000316 return;
317 }
318
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000319 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000320
321 // The nonnull attribute only applies to pointers.
322 llvm::SmallVector<unsigned, 10> NonNullArgs;
323
324 for (AttributeList::arg_iterator I=Attr.arg_begin(),
325 E=Attr.arg_end(); I!=E; ++I) {
326
327
328 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000329 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000330 llvm::APSInt ArgNum(32);
331 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000332 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
333 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000334 return;
335 }
336
337 unsigned x = (unsigned) ArgNum.getZExtValue();
338
339 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000340 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000341 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000342 return;
343 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000344
345 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000346
347 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000348 QualType T = getFunctionOrMethodArgType(d, x);
349 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000350 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000351 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
352 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000353 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000354 }
355
356 NonNullArgs.push_back(x);
357 }
358
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000359 // If no arguments were specified to __attribute__((nonnull)) then all
360 // pointer arguments have a nonnull attribute.
361 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000362 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
363 QualType T = getFunctionOrMethodArgType(d, I);
364 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000365 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000366 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000367
368 if (NonNullArgs.empty()) {
369 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
370 return;
371 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000372 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000373
374 unsigned* start = &NonNullArgs[0];
375 unsigned size = NonNullArgs.size();
376 std::sort(start, start + size);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000377 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000378}
379
Chris Lattner803d0802008-06-29 00:43:07 +0000380static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000381 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000382 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000383 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000384 return;
385 }
386
Chris Lattner545dd342008-06-28 23:36:30 +0000387 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000388 Arg = Arg->IgnoreParenCasts();
389 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
390
391 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000392 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000393 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000394 return;
395 }
396
397 const char *Alias = Str->getStrData();
398 unsigned AliasLen = Str->getByteLength();
399
400 // FIXME: check if target symbol exists in current file
401
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000402 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000403}
404
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000405static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
406 Sema &S) {
407 // check the attribute arguments.
408 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000409 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000410 return;
411 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000412
Chris Lattnerc5197432009-04-14 17:02:11 +0000413 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000414 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000415 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000416 return;
417 }
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000418
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000419 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000420}
421
Ted Kremenekb7252322009-04-10 00:01:14 +0000422static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000423 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000424 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000425 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000427 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000428 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000429
Mike Stump19c30c02009-04-29 19:03:13 +0000430 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
431 ValueDecl *VD = dyn_cast<ValueDecl>(d);
432 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
433 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000434 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000435 return false;
436 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000437 }
438
Ted Kremenekb7252322009-04-10 00:01:14 +0000439 return true;
440}
441
442static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000443 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenekb7252322009-04-10 00:01:14 +0000444 d->addAttr(::new (S.Context) NoReturnAttr());
445}
446
447static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
448 Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000449 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenekb7252322009-04-10 00:01:14 +0000450 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000451}
452
Ted Kremenek73798892008-07-25 04:39:19 +0000453static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
454 // check the attribute arguments.
455 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000456 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000457 return;
458 }
459
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000460 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000461 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000462 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000463 return;
464 }
465
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000466 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000467}
468
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000469static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
470 // check the attribute arguments.
471 if (Attr.getNumArgs() != 0) {
472 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
473 return;
474 }
475
476 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000477 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000478 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
479 return;
480 }
481 } else if (!isFunctionOrMethod(d)) {
482 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000483 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000484 return;
485 }
486
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000487 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000488}
489
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000490static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
491 // check the attribute arguments.
492 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
494 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000495 return;
496 }
497
498 int priority = 65535; // FIXME: Do not hardcode such constants.
499 if (Attr.getNumArgs() > 0) {
500 Expr *E = static_cast<Expr *>(Attr.getArg(0));
501 llvm::APSInt Idx(32);
502 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000503 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000504 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000505 return;
506 }
507 priority = Idx.getZExtValue();
508 }
509
Chris Lattnerc5197432009-04-14 17:02:11 +0000510 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000511 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000512 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000513 return;
514 }
515
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000516 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000517}
518
519static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
520 // check the attribute arguments.
521 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000522 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
523 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000524 return;
525 }
526
527 int priority = 65535; // FIXME: Do not hardcode such constants.
528 if (Attr.getNumArgs() > 0) {
529 Expr *E = static_cast<Expr *>(Attr.getArg(0));
530 llvm::APSInt Idx(32);
531 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000532 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000533 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000534 return;
535 }
536 priority = Idx.getZExtValue();
537 }
538
Anders Carlsson6782fc62008-08-22 22:10:48 +0000539 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000540 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000541 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000542 return;
543 }
544
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000545 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000546}
547
Chris Lattner803d0802008-06-29 00:43:07 +0000548static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000549 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000550 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000551 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000552 return;
553 }
554
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000555 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000556}
557
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000558static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
559 // check the attribute arguments.
560 if (Attr.getNumArgs() != 0) {
561 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
562 return;
563 }
564
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000565 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000566}
567
Chris Lattner803d0802008-06-29 00:43:07 +0000568static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000569 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000570 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000571 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000572 return;
573 }
574
Chris Lattner545dd342008-06-28 23:36:30 +0000575 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000576 Arg = Arg->IgnoreParenCasts();
577 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
578
579 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000580 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000581 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000582 return;
583 }
584
585 const char *TypeStr = Str->getStrData();
586 unsigned TypeLen = Str->getByteLength();
587 VisibilityAttr::VisibilityTypes type;
588
589 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
590 type = VisibilityAttr::DefaultVisibility;
591 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
592 type = VisibilityAttr::HiddenVisibility;
593 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
594 type = VisibilityAttr::HiddenVisibility; // FIXME
595 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
596 type = VisibilityAttr::ProtectedVisibility;
597 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000598 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000599 return;
600 }
601
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000602 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000603}
604
Chris Lattner0db29ec2009-02-14 08:09:34 +0000605static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
606 Sema &S) {
607 if (Attr.getNumArgs() != 0) {
608 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
609 return;
610 }
611
612 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
613 if (OCI == 0) {
614 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
615 return;
616 }
617
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000618 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000619}
620
621static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000622 if (Attr.getNumArgs() != 0) {
623 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
624 return;
625 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000626 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000627 QualType T = TD->getUnderlyingType();
628 if (!T->isPointerType() ||
629 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
630 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
631 return;
632 }
633 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000634 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000635}
636
Douglas Gregorf9201e02009-02-11 23:02:49 +0000637static void
638HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
639 if (Attr.getNumArgs() != 0) {
640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
641 return;
642 }
643
644 if (!isa<FunctionDecl>(D)) {
645 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
646 return;
647 }
648
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000649 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000650}
651
Steve Naroff9eae5762008-09-18 16:44:58 +0000652static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
653 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000654 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000655 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000656 return;
657 }
658
659 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000661 return;
662 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000663
664 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000665 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000666 type = BlocksAttr::ByRef;
667 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000668 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000669 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000670 return;
671 }
672
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000673 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000674}
675
Anders Carlsson77091822008-10-05 18:05:59 +0000676static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
677 // check the attribute arguments.
678 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
680 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000681 return;
682 }
683
684 int sentinel = 0;
685 if (Attr.getNumArgs() > 0) {
686 Expr *E = static_cast<Expr *>(Attr.getArg(0));
687 llvm::APSInt Idx(32);
688 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000689 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000690 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000691 return;
692 }
693 sentinel = Idx.getZExtValue();
694
695 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000696 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
697 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000698 return;
699 }
700 }
701
702 int nullPos = 0;
703 if (Attr.getNumArgs() > 1) {
704 Expr *E = static_cast<Expr *>(Attr.getArg(1));
705 llvm::APSInt Idx(32);
706 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000708 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000709 return;
710 }
711 nullPos = Idx.getZExtValue();
712
713 if (nullPos > 1 || nullPos < 0) {
714 // FIXME: This error message could be improved, it would be nice
715 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000716 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
717 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000718 return;
719 }
720 }
721
722 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000723 const FunctionType *FT = FD->getType()->getAsFunctionType();
724 assert(FT && "FunctionDecl has non-function type?");
725
726 if (isa<FunctionNoProtoType>(FT)) {
727 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
728 return;
729 }
730
731 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000732 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000733 return;
734 }
735 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
736 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000737 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000738 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000739 }
740 } else if (isa<BlockDecl>(d)) {
741 // Note! BlockDecl is typeless. Variadic diagnostics
742 // will be issued by the caller.
743 ;
744 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000745 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000746 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
747 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
748 : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000749 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000750 int m = Ty->isFunctionPointerType() ? 0 : 1;
751 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000752 return;
753 }
754 }
755 else {
756 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000757 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000758 return;
759 }
Anders Carlsson77091822008-10-05 18:05:59 +0000760 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000761 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000762 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000763 return;
764 }
Fariborz Jahanian88f1ba02009-05-13 23:20:50 +0000765 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000766}
767
Chris Lattner026dc962009-02-14 07:37:35 +0000768static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
769 // check the attribute arguments.
770 if (Attr.getNumArgs() != 0) {
771 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
772 return;
773 }
774
775 // TODO: could also be applied to methods?
776 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
777 if (!Fn) {
778 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000779 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000780 return;
781 }
782
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000783 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000784}
785
786static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000787 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000788 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000789 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000790 return;
791 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000792
793 // TODO: could also be applied to methods?
794 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
795 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000796 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000797 return;
798 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000799
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000800 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000801}
802
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000803static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
804 // check the attribute arguments.
805 if (Attr.getNumArgs() != 0) {
806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
807 return;
808 }
809
810 // weak_import only applies to variable & function declarations.
811 bool isDef = false;
812 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
813 isDef = (!VD->hasExternalStorage() || VD->getInit());
814 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor72971342009-04-18 00:02:19 +0000815 isDef = FD->getBody(S.Context);
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000816 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
817 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000818 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000819 } else {
820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000821 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000822 return;
823 }
824
825 // Merge should handle any subsequent violations.
826 if (isDef) {
827 S.Diag(Attr.getLoc(),
828 diag::warn_attribute_weak_import_invalid_on_definition)
829 << "weak_import" << 2 /*variable and function*/;
830 return;
831 }
832
833 D->addAttr(::new (S.Context) WeakImportAttr());
834}
835
Chris Lattner026dc962009-02-14 07:37:35 +0000836static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000837 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000838 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000840 return;
841 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000842
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000843 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000844 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000845 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000846 return;
847 }
848
Chris Lattner026dc962009-02-14 07:37:35 +0000849 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000850 if (!FD) {
851 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000852 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000853 return;
854 }
855
856 // Currently, the dllimport attribute is ignored for inlined functions.
857 // Warning is emitted.
858 if (FD->isInline()) {
859 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
860 return;
861 }
862
863 // The attribute is also overridden by a subsequent declaration as dllexport.
864 // Warning is emitted.
865 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
866 nextAttr = nextAttr->getNext()) {
867 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
868 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
869 return;
870 }
871 }
872
Chris Lattner026dc962009-02-14 07:37:35 +0000873 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000874 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
875 return;
876 }
877
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000878 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000879}
880
Chris Lattner026dc962009-02-14 07:37:35 +0000881static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000882 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000883 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000884 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000885 return;
886 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000887
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000888 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000889 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000890 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000891 return;
892 }
893
Chris Lattner026dc962009-02-14 07:37:35 +0000894 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000895 if (!FD) {
896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000897 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000898 return;
899 }
900
901 // Currently, the dllexport attribute is ignored for inlined functions,
902 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
903 if (FD->isInline()) {
904 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
905 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
906 return;
907 }
908
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000909 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000910}
911
Chris Lattner026dc962009-02-14 07:37:35 +0000912static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000913 // Attribute has no arguments.
914 if (Attr.getNumArgs() != 1) {
915 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
916 return;
917 }
918
919 // Make sure that there is a string literal as the sections's single
920 // argument.
921 StringLiteral *SE =
922 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
923 if (!SE) {
924 // FIXME
925 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
926 return;
927 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000928 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
929 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000930}
931
Chris Lattner803d0802008-06-29 00:43:07 +0000932static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000933 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000934 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000936 return;
937 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000938
939 // Attribute can be applied only to functions.
940 if (!isa<FunctionDecl>(d)) {
941 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000942 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000943 return;
944 }
945
946 // stdcall and fastcall attributes are mutually incompatible.
947 if (d->getAttr<FastCallAttr>()) {
948 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
949 << "stdcall" << "fastcall";
950 return;
951 }
952
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000953 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000954}
955
Chris Lattner803d0802008-06-29 00:43:07 +0000956static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000957 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000958 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000959 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000960 return;
961 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000962
963 if (!isa<FunctionDecl>(d)) {
964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000965 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000966 return;
967 }
968
969 // stdcall and fastcall attributes are mutually incompatible.
970 if (d->getAttr<StdCallAttr>()) {
971 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
972 << "fastcall" << "stdcall";
973 return;
974 }
975
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000976 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000977}
978
Chris Lattner803d0802008-06-29 00:43:07 +0000979static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000980 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000981 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000982 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000983 return;
984 }
985
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000986 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000987}
988
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000989static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
990 // check the attribute arguments.
991 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000993 return;
994 }
995
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000996 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000997}
998
999static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1000 // check the attribute arguments.
1001 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001002 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001003 return;
1004 }
1005
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001006 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001007}
1008
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001009static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001010 // Match gcc which ignores cleanup attrs when compiling C++.
1011 if (S.getLangOptions().CPlusPlus)
1012 return;
1013
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001014 if (!Attr.getParameterName()) {
1015 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1016 return;
1017 }
1018
1019 if (Attr.getNumArgs() != 0) {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1021 return;
1022 }
1023
1024 VarDecl *VD = dyn_cast<VarDecl>(d);
1025
1026 if (!VD || !VD->hasLocalStorage()) {
1027 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1028 return;
1029 }
1030
1031 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001032 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1033 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001034 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001036 Attr.getParameterName();
1037 return;
1038 }
1039
1040 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1041 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001042 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001043 Attr.getParameterName();
1044 return;
1045 }
1046
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001047 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001048 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001049 Attr.getParameterName();
1050 return;
1051 }
1052
Anders Carlsson89941c12009-02-07 23:16:50 +00001053 // We're currently more strict than GCC about what function types we accept.
1054 // If this ever proves to be a problem it should be easy to fix.
1055 QualType Ty = S.Context.getPointerType(VD->getType());
1056 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001057 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001058 S.Diag(Attr.getLoc(),
1059 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1060 Attr.getParameterName() << ParamTy << Ty;
1061 return;
1062 }
1063
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001064 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001065}
1066
Chris Lattner6b6b5372008-06-26 18:38:35 +00001067/// Handle __attribute__((format(type,idx,firstarg))) attributes
1068/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001069static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001070
Chris Lattner545dd342008-06-28 23:36:30 +00001071 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001072 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001073 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001074 return;
1075 }
1076
Chris Lattner545dd342008-06-28 23:36:30 +00001077 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001079 return;
1080 }
1081
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001082 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001083 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001084 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001085 return;
1086 }
1087
1088 // FIXME: in C++ the implicit 'this' function parameter also counts.
1089 // this is needed in order to be compatible with GCC
1090 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001091 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001092 unsigned FirstIdx = 1;
1093
Chris Lattner545dd342008-06-28 23:36:30 +00001094 const char *Format = Attr.getParameterName()->getName();
1095 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001096
1097 // Normalize the argument, __foo__ becomes foo.
1098 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1099 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1100 Format += 2;
1101 FormatLen -= 4;
1102 }
1103
1104 bool Supported = false;
1105 bool is_NSString = false;
1106 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001107 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001108
1109 switch (FormatLen) {
1110 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001111 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1112 case 6: Supported = !memcmp(Format, "printf", 6); break;
1113 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001114 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001115 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1116 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1117 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001118 break;
1119 }
1120
1121 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001122 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1123 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001124 return;
1125 }
1126
1127 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001128 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001129 llvm::APSInt Idx(32);
1130 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001132 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001133 return;
1134 }
1135
1136 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001137 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001138 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001139 return;
1140 }
1141
1142 // FIXME: Do we need to bounds check?
1143 unsigned ArgIdx = Idx.getZExtValue() - 1;
1144
1145 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001146 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001147
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001148 if (is_CFString) {
1149 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001150 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1151 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001152 return;
1153 }
1154 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001155 // FIXME: do we need to check if the type is NSString*? What are the
1156 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001157 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001158 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001159 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1160 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001161 return;
1162 }
1163 } else if (!Ty->isPointerType() ||
1164 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001165 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001166 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1167 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001168 return;
1169 }
1170
1171 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001172 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001173 llvm::APSInt FirstArg(32);
1174 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001175 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001176 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001177 return;
1178 }
1179
1180 // check if the function is variadic if the 3rd argument non-zero
1181 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001182 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001183 ++NumArgs; // +1 for ...
1184 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001185 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001186 return;
1187 }
1188 }
1189
Chris Lattner3c73c412008-11-19 08:23:25 +00001190 // strftime requires FirstArg to be 0 because it doesn't read from any
1191 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001192 if (is_strftime) {
1193 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001194 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1195 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001196 return;
1197 }
1198 // if 0 it disables parameter checking (to use with e.g. va_list)
1199 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001200 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001201 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001202 return;
1203 }
1204
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001205 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001206 Idx.getZExtValue(), FirstArg.getZExtValue()));
1207}
1208
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001209static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1210 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001211 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001212 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001214 return;
1215 }
1216
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001217 // Try to find the underlying union declaration.
1218 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001219 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001220 if (TD && TD->getUnderlyingType()->isUnionType())
1221 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1222 else
1223 RD = dyn_cast<RecordDecl>(d);
1224
1225 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001227 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228 return;
1229 }
1230
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001231 if (!RD->isDefinition()) {
1232 S.Diag(Attr.getLoc(),
1233 diag::warn_transparent_union_attribute_not_definition);
1234 return;
1235 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001236
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001237 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1238 FieldEnd = RD->field_end(S.Context);
1239 if (Field == FieldEnd) {
1240 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1241 return;
1242 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001243
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001244 FieldDecl *FirstField = *Field;
1245 QualType FirstType = FirstField->getType();
1246 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1247 S.Diag(FirstField->getLocation(),
1248 diag::warn_transparent_union_attribute_floating);
1249 return;
1250 }
1251
1252 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1253 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1254 for (; Field != FieldEnd; ++Field) {
1255 QualType FieldType = Field->getType();
1256 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1257 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1258 // Warn if we drop the attribute.
1259 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1260 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1261 : S.Context.getTypeAlign(FieldType);
1262 S.Diag(Field->getLocation(),
1263 diag::warn_transparent_union_attribute_field_size_align)
1264 << isSize << Field->getDeclName() << FieldBits;
1265 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1266 S.Diag(FirstField->getLocation(),
1267 diag::note_transparent_union_first_field_size_align)
1268 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001269 return;
1270 }
1271 }
1272
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001273 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274}
1275
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001276static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001277 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001278 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001280 return;
1281 }
Chris Lattner545dd342008-06-28 23:36:30 +00001282 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001283 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1284
1285 // Make sure that there is a string literal as the annotation's single
1286 // argument.
1287 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001288 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001289 return;
1290 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001291 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1292 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001293}
1294
Chris Lattner803d0802008-06-29 00:43:07 +00001295static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001296 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001297 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001298 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001299 return;
1300 }
1301
1302 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001303 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001304 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001305 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001306 Align = 128;
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001307 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001308 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001309 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001310
1311 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1312 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001313 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1315 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001316 return;
1317 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001318 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1319 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1320 << alignmentExpr->getSourceRange();
1321 return;
1322 }
1323
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001324 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001325}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001326
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001327/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001328/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001329///
1330/// Despite what would be logical, the mode attribute is a decl attribute,
1331/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1332/// 'G' be HImode, not an intermediate pointer.
1333///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001334static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001335 // This attribute isn't documented, but glibc uses it. It changes
1336 // the width of an int or unsigned int to the specified size.
1337
1338 // Check that there aren't any arguments
1339 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001341 return;
1342 }
1343
1344 IdentifierInfo *Name = Attr.getParameterName();
1345 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001346 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001347 return;
1348 }
1349 const char *Str = Name->getName();
1350 unsigned Len = Name->getLength();
1351
1352 // Normalize the attribute name, __foo__ becomes foo.
1353 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1354 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1355 Str += 2;
1356 Len -= 4;
1357 }
1358
1359 unsigned DestWidth = 0;
1360 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001361 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001362 switch (Len) {
1363 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001364 switch (Str[0]) {
1365 case 'Q': DestWidth = 8; break;
1366 case 'H': DestWidth = 16; break;
1367 case 'S': DestWidth = 32; break;
1368 case 'D': DestWidth = 64; break;
1369 case 'X': DestWidth = 96; break;
1370 case 'T': DestWidth = 128; break;
1371 }
1372 if (Str[1] == 'F') {
1373 IntegerMode = false;
1374 } else if (Str[1] == 'C') {
1375 IntegerMode = false;
1376 ComplexMode = true;
1377 } else if (Str[1] != 'I') {
1378 DestWidth = 0;
1379 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001380 break;
1381 case 4:
1382 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1383 // pointer on PIC16 and other embedded platforms.
1384 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001385 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001386 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001387 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001388 break;
1389 case 7:
1390 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001391 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001392 break;
1393 }
1394
1395 QualType OldTy;
1396 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1397 OldTy = TD->getUnderlyingType();
1398 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1399 OldTy = VD->getType();
1400 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001401 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1402 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001403 return;
1404 }
Eli Friedman73397492009-03-03 06:41:03 +00001405
1406 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1407 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1408 else if (IntegerMode) {
1409 if (!OldTy->isIntegralType())
1410 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1411 } else if (ComplexMode) {
1412 if (!OldTy->isComplexType())
1413 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1414 } else {
1415 if (!OldTy->isFloatingType())
1416 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1417 }
1418
Mike Stump390b4cc2009-05-16 07:39:55 +00001419 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1420 // and friends, at least with glibc.
1421 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1422 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001423 // FIXME: Make sure floating-point mappings are accurate
1424 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001425 QualType NewTy;
1426 switch (DestWidth) {
1427 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001428 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001429 return;
1430 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001431 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001432 return;
1433 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001434 if (!IntegerMode) {
1435 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1436 return;
1437 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001438 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001439 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001440 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001441 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001442 break;
1443 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001444 if (!IntegerMode) {
1445 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1446 return;
1447 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001448 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001449 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001450 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001451 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001452 break;
1453 case 32:
1454 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001455 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001456 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001457 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001458 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001459 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001460 break;
1461 case 64:
1462 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001463 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001464 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001465 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001466 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001467 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001468 break;
Eli Friedman73397492009-03-03 06:41:03 +00001469 case 96:
1470 NewTy = S.Context.LongDoubleTy;
1471 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001472 case 128:
1473 if (!IntegerMode) {
1474 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1475 return;
1476 }
1477 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001478 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001479 }
1480
Eli Friedman73397492009-03-03 06:41:03 +00001481 if (ComplexMode) {
1482 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001483 }
1484
1485 // Install the new type.
1486 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1487 TD->setUnderlyingType(NewTy);
1488 else
1489 cast<ValueDecl>(D)->setType(NewTy);
1490}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001491
Anders Carlssond87df372009-02-13 06:46:13 +00001492static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1493 // check the attribute arguments.
1494 if (Attr.getNumArgs() > 0) {
1495 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1496 return;
1497 }
Anders Carlssone896d982009-02-13 08:11:52 +00001498
Anders Carlsson5bab7882009-02-19 19:16:48 +00001499 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001500 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001501 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001502 return;
1503 }
1504
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001505 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001506}
1507
Anders Carlsson5bab7882009-02-19 19:16:48 +00001508static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1509 // check the attribute arguments.
1510 if (Attr.getNumArgs() != 0) {
1511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1512 return;
1513 }
1514
Chris Lattnerc5197432009-04-14 17:02:11 +00001515 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001516 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001517 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001518 return;
1519 }
1520
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001521 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001522}
1523
Chris Lattnercf2a7212009-04-20 19:12:28 +00001524static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001525 // check the attribute arguments.
1526 if (Attr.getNumArgs() != 0) {
1527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1528 return;
1529 }
1530
Chris Lattnerc5197432009-04-14 17:02:11 +00001531 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1532 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001533 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001534 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001535 return;
1536 }
1537
Chris Lattnerc5197432009-04-14 17:02:11 +00001538 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001539 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001540 return;
1541 }
1542
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001543 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001544}
1545
Fariborz Jahanianee760332009-03-27 18:38:55 +00001546static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1547 // check the attribute arguments.
1548 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001549 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001550 return;
1551 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001552
Fariborz Jahanianee760332009-03-27 18:38:55 +00001553 if (!isFunctionOrMethod(d)) {
1554 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001555 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001556 return;
1557 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001558
1559 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1560 llvm::APSInt NumParams(32);
1561 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1562 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1563 << "regparm" << NumParamsExpr->getSourceRange();
1564 return;
1565 }
1566
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001567 if (S.Context.Target.getRegParmMax() == 0) {
1568 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001569 << NumParamsExpr->getSourceRange();
1570 return;
1571 }
1572
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001573 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001574 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1575 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001576 return;
1577 }
1578
1579 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001580}
1581
Chris Lattner0744e5f2008-06-29 00:23:49 +00001582//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001583// Checker-specific attribute handlers.
1584//===----------------------------------------------------------------------===//
1585
1586static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1587 Sema &S) {
1588
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001589 QualType RetTy;
1590
1591 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1592 RetTy = MD->getResultType();
1593 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1594 RetTy = FD->getResultType();
1595 else {
1596 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1597 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001598 return;
1599 }
1600
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001601 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1602 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1603 << Attr.getName();
1604 return;
1605 }
1606
Ted Kremenekb71368d2009-05-09 02:44:38 +00001607 switch (Attr.getKind()) {
1608 default:
1609 assert(0 && "invalid ownership attribute");
1610 return;
1611 case AttributeList::AT_cf_returns_retained:
1612 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1613 return;
1614 case AttributeList::AT_ns_returns_retained:
1615 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1616 return;
1617 };
1618}
1619
1620//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001621// Top Level Sema Entry Points
1622//===----------------------------------------------------------------------===//
1623
Sebastian Redla89d82c2008-12-21 19:24:58 +00001624/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001625/// the attribute applies to decls. If the attribute is a type attribute, just
1626/// silently ignore it.
1627static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1628 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001629 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001630 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001631 case AttributeList::AT_objc_gc:
1632 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001633 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001634 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1635 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001636 case AttributeList::AT_always_inline:
1637 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001638 case AttributeList::AT_analyzer_noreturn:
1639 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001640 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1641 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1642 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1643 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1644 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1645 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001646 case AttributeList::AT_ext_vector_type:
1647 HandleExtVectorTypeAttr(D, Attr, S);
1648 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001649 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1650 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001651 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001652 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001653 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1654 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1655 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001656
1657 // Checker-specific.
1658 case AttributeList::AT_ns_returns_retained:
1659 case AttributeList::AT_cf_returns_retained:
1660 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1661
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001662 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001663 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001664 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001665 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001666 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001667 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001668 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001669 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001670 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1671 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001672 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001673 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001674 case AttributeList::AT_transparent_union:
1675 HandleTransparentUnionAttr(D, Attr, S);
1676 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001677 case AttributeList::AT_objc_exception:
1678 HandleObjCExceptionAttr(D, Attr, S);
1679 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001680 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001681 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001682 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001683 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001684 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1685 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001686 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001687 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001688 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001689 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001690 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001691 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001692 // Just ignore
1693 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001694 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001695 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001696 break;
1697 }
1698}
1699
1700/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1701/// attribute list to the specified decl, ignoring any type attributes.
1702void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1703 while (AttrList) {
1704 ProcessDeclAttribute(D, *AttrList, *this);
1705 AttrList = AttrList->getNext();
1706 }
1707}
1708
Chris Lattner0744e5f2008-06-29 00:23:49 +00001709/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1710/// it, apply them to D. This is a bit tricky because PD can have attributes
1711/// specified in many different places, and we need to find and apply them all.
1712void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1713 // Apply decl attributes from the DeclSpec if present.
1714 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1715 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001716
Chris Lattner0744e5f2008-06-29 00:23:49 +00001717 // Walk the declarator structure, applying decl attributes that were in a type
1718 // position to the decl itself. This handles cases like:
1719 // int *__attr__(x)** D;
1720 // when X is a decl attribute.
1721 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1722 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1723 ProcessDeclAttributeList(D, Attrs);
1724
1725 // Finally, apply any attributes on the decl itself.
1726 if (const AttributeList *Attrs = PD.getAttributes())
1727 ProcessDeclAttributeList(D, Attrs);
1728}