blob: c5099319ddb03b6a3cf7e4c586c3823011ea3aa5 [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 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000067 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068}
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 {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000077 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078 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();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000088 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
89 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000090 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000091}
92
93static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getParamDecl(Idx)->getType();
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098
Chris Lattner89951a82009-02-20 18:43:26 +000099 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000100}
101
102static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000103 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000104 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000105 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000106 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
107 return BD->IsVariadic();
108 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000109 return cast<ObjCMethodDecl>(d)->isVariadic();
110 }
111}
112
Chris Lattner6b6b5372008-06-26 18:38:35 +0000113static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000114 const PointerType *PT = T->getAsPointerType();
115 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000116 return false;
117
Chris Lattnerb77792e2008-07-26 22:17:49 +0000118 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000119 if (!ClsT)
120 return false;
121
122 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
123
124 // FIXME: Should we walk the chain of classes?
125 return ClsName == &Ctx.Idents.get("NSString") ||
126 ClsName == &Ctx.Idents.get("NSMutableString");
127}
128
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000129static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
130 const PointerType *PT = T->getAsPointerType();
131 if (!PT)
132 return false;
133
134 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
135 if (!RT)
136 return false;
137
138 const RecordDecl *RD = RT->getDecl();
139 if (RD->getTagKind() != TagDecl::TK_struct)
140 return false;
141
142 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
143}
144
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000145//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000146// Attribute Implementations
147//===----------------------------------------------------------------------===//
148
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000149// FIXME: All this manual attribute parsing code is gross. At the
150// least add some helper functions to check most argument patterns (#
151// and types of args).
152
Chris Lattner803d0802008-06-29 00:43:07 +0000153static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
154 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000155 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
156 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000157 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000158 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000159 }
160
Chris Lattner6b6b5372008-06-26 18:38:35 +0000161 QualType curType = tDecl->getUnderlyingType();
162 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000163 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000164 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165 return;
166 }
Chris Lattner545dd342008-06-28 23:36:30 +0000167 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000169 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000170 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
171 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 return;
173 }
174 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
175 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000176 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000177 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 return;
179 }
180 // unlike gcc's vector_size attribute, the size is specified as the
181 // number of elements, not the number of bytes.
182 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
183
184 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000185 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
186 << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000187 return;
188 }
189 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000190 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000191 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000192 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193}
194
Chris Lattner065c5a82008-06-28 23:48:25 +0000195
196/// HandleVectorSizeAttribute - this attribute is only applicable to
197/// integral and float scalars, although arrays, pointers, and function
198/// return values are allowed in conjunction with this construct. Aggregates
199/// with this attribute are invalid, even if they are of the same size as a
200/// corresponding scalar.
201/// The raw attribute should contain precisely 1 argument, the vector size
202/// for the variable, measured in bytes. If curType and rawAttr are well
203/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000204static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000205 QualType CurType;
206 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
207 CurType = VD->getType();
208 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
209 CurType = TD->getUnderlyingType();
210 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000211 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
212 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000213 return;
214 }
215
216 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000217 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000218 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000219 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000220 }
Chris Lattner545dd342008-06-28 23:36:30 +0000221 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000223 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
225 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000226 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000227 }
228 // navigate to the base type - we need to provide for vector pointers,
229 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000230 if (CurType->isPointerType() || CurType->isArrayType() ||
231 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000232 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
233 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000234 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
235 do {
236 if (PointerType *PT = dyn_cast<PointerType>(canonType))
237 canonType = PT->getPointeeType().getTypePtr();
238 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
239 canonType = AT->getElementType().getTypePtr();
240 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
241 canonType = FT->getResultType().getTypePtr();
242 } while (canonType->isPointerType() || canonType->isArrayType() ||
243 canonType->isFunctionType());
244 */
245 }
Chris Lattner82afa2d2009-05-13 05:13:44 +0000246 // the base type must be integer or float, and can't already be a vector.
247 if (CurType->isVectorType() ||
248 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000249 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000250 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000251 }
Chris Lattner803d0802008-06-29 00:43:07 +0000252 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000253 // vecSize is specified in bytes - convert to bits.
254 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
255
256 // the vector size needs to be an integral multiple of the type size.
257 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000258 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
259 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000260 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000261 }
262 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000263 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
264 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000265 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000266 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000267
268 // Success! Instantiate the vector type, the number of elements is > 0, and
269 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000270 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000271
272 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
273 VD->setType(CurType);
274 else
275 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000276}
277
Chris Lattner803d0802008-06-29 00:43:07 +0000278static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000279 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000280 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000282 return;
283 }
284
285 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000286 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000287 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
288 // If the alignment is less than or equal to 8 bits, the packed attribute
289 // has no effect.
290 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000291 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000292 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000293 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000294 else
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000295 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000296 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000297 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000298}
299
Ted Kremenek96329d42008-07-15 22:26:48 +0000300static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
301 // check the attribute arguments.
302 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000304 return;
305 }
306
307 // The IBOutlet attribute only applies to instance variables of Objective-C
308 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000309 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000310 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000311 else
Ted Kremenek32742602009-02-17 22:20:20 +0000312 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000313}
314
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000315static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000316 // GCC ignores the nonnull attribute on K&R style function
317 // prototypes, so we ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000318 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000319 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000320 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000321 return;
322 }
323
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000324 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000325
326 // The nonnull attribute only applies to pointers.
327 llvm::SmallVector<unsigned, 10> NonNullArgs;
328
329 for (AttributeList::arg_iterator I=Attr.arg_begin(),
330 E=Attr.arg_end(); I!=E; ++I) {
331
332
333 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000334 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000335 llvm::APSInt ArgNum(32);
336 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000339 return;
340 }
341
342 unsigned x = (unsigned) ArgNum.getZExtValue();
343
344 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000346 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000347 return;
348 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000349
350 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351
352 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000353 QualType T = getFunctionOrMethodArgType(d, x);
354 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000355 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000356 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
357 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000358 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000359 }
360
361 NonNullArgs.push_back(x);
362 }
363
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000364 // If no arguments were specified to __attribute__((nonnull)) then all
365 // pointer arguments have a nonnull attribute.
366 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000367 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
368 QualType T = getFunctionOrMethodArgType(d, I);
369 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000370 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000371 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000372
373 if (NonNullArgs.empty()) {
374 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
375 return;
376 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000377 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000378
379 unsigned* start = &NonNullArgs[0];
380 unsigned size = NonNullArgs.size();
381 std::sort(start, start + size);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000382 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000383}
384
Chris Lattner803d0802008-06-29 00:43:07 +0000385static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000386 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000387 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000388 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000389 return;
390 }
391
Chris Lattner545dd342008-06-28 23:36:30 +0000392 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000393 Arg = Arg->IgnoreParenCasts();
394 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
395
396 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000397 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000398 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000399 return;
400 }
401
402 const char *Alias = Str->getStrData();
403 unsigned AliasLen = Str->getByteLength();
404
405 // FIXME: check if target symbol exists in current file
406
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000407 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000408}
409
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000410static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
411 Sema &S) {
412 // check the attribute arguments.
413 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000415 return;
416 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000417
Chris Lattnerc5197432009-04-14 17:02:11 +0000418 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000420 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000421 return;
422 }
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000423
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000424 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000425}
426
Ted Kremenekb7252322009-04-10 00:01:14 +0000427static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000428 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000429 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000430 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000432 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000433 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000434
Mike Stump19c30c02009-04-29 19:03:13 +0000435 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
436 ValueDecl *VD = dyn_cast<ValueDecl>(d);
437 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000439 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000440 return false;
441 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000442 }
443
Ted Kremenekb7252322009-04-10 00:01:14 +0000444 return true;
445}
446
447static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000448 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenekb7252322009-04-10 00:01:14 +0000449 d->addAttr(::new (S.Context) NoReturnAttr());
450}
451
452static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
453 Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000454 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenekb7252322009-04-10 00:01:14 +0000455 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000456}
457
Ted Kremenek73798892008-07-25 04:39:19 +0000458static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
459 // check the attribute arguments.
460 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000462 return;
463 }
464
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000465 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000467 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000468 return;
469 }
470
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000471 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000472}
473
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000474static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
475 // check the attribute arguments.
476 if (Attr.getNumArgs() != 0) {
477 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
478 return;
479 }
480
481 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000482 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000483 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
484 return;
485 }
486 } else if (!isFunctionOrMethod(d)) {
487 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000488 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000489 return;
490 }
491
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000492 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000493}
494
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000495static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
496 // check the attribute arguments.
497 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000498 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
499 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000500 return;
501 }
502
503 int priority = 65535; // FIXME: Do not hardcode such constants.
504 if (Attr.getNumArgs() > 0) {
505 Expr *E = static_cast<Expr *>(Attr.getArg(0));
506 llvm::APSInt Idx(32);
507 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000509 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000510 return;
511 }
512 priority = Idx.getZExtValue();
513 }
514
Chris Lattnerc5197432009-04-14 17:02:11 +0000515 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000516 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000517 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000518 return;
519 }
520
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000521 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000522}
523
524static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
525 // check the attribute arguments.
526 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
528 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000529 return;
530 }
531
532 int priority = 65535; // FIXME: Do not hardcode such constants.
533 if (Attr.getNumArgs() > 0) {
534 Expr *E = static_cast<Expr *>(Attr.getArg(0));
535 llvm::APSInt Idx(32);
536 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000537 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000538 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000539 return;
540 }
541 priority = Idx.getZExtValue();
542 }
543
Anders Carlsson6782fc62008-08-22 22:10:48 +0000544 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000545 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000546 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000547 return;
548 }
549
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000550 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000551}
552
Chris Lattner803d0802008-06-29 00:43:07 +0000553static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000554 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000555 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000557 return;
558 }
559
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000560 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000561}
562
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000563static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
564 // check the attribute arguments.
565 if (Attr.getNumArgs() != 0) {
566 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
567 return;
568 }
569
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000570 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000571}
572
Chris Lattner803d0802008-06-29 00:43:07 +0000573static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000574 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000575 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000576 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000577 return;
578 }
579
Chris Lattner545dd342008-06-28 23:36:30 +0000580 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000581 Arg = Arg->IgnoreParenCasts();
582 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
583
584 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000585 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000586 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000587 return;
588 }
589
590 const char *TypeStr = Str->getStrData();
591 unsigned TypeLen = Str->getByteLength();
592 VisibilityAttr::VisibilityTypes type;
593
594 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
595 type = VisibilityAttr::DefaultVisibility;
596 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
597 type = VisibilityAttr::HiddenVisibility;
598 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
599 type = VisibilityAttr::HiddenVisibility; // FIXME
600 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
601 type = VisibilityAttr::ProtectedVisibility;
602 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000603 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000604 return;
605 }
606
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000607 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000608}
609
Chris Lattner0db29ec2009-02-14 08:09:34 +0000610static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
611 Sema &S) {
612 if (Attr.getNumArgs() != 0) {
613 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
614 return;
615 }
616
617 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
618 if (OCI == 0) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
620 return;
621 }
622
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000623 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000624}
625
626static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000627 if (Attr.getNumArgs() != 0) {
628 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
629 return;
630 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000631 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000632 QualType T = TD->getUnderlyingType();
633 if (!T->isPointerType() ||
634 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
635 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
636 return;
637 }
638 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000639 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000640}
641
Douglas Gregorf9201e02009-02-11 23:02:49 +0000642static void
643HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
644 if (Attr.getNumArgs() != 0) {
645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
646 return;
647 }
648
649 if (!isa<FunctionDecl>(D)) {
650 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
651 return;
652 }
653
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000654 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000655}
656
Steve Naroff9eae5762008-09-18 16:44:58 +0000657static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
658 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000659 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000661 return;
662 }
663
664 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000666 return;
667 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000668
669 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000670 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000671 type = BlocksAttr::ByRef;
672 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000673 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000674 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000675 return;
676 }
677
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000678 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000679}
680
Anders Carlsson77091822008-10-05 18:05:59 +0000681static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
682 // check the attribute arguments.
683 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
685 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000686 return;
687 }
688
689 int sentinel = 0;
690 if (Attr.getNumArgs() > 0) {
691 Expr *E = static_cast<Expr *>(Attr.getArg(0));
692 llvm::APSInt Idx(32);
693 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000694 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000695 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000696 return;
697 }
698 sentinel = Idx.getZExtValue();
699
700 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000701 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
702 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000703 return;
704 }
705 }
706
707 int nullPos = 0;
708 if (Attr.getNumArgs() > 1) {
709 Expr *E = static_cast<Expr *>(Attr.getArg(1));
710 llvm::APSInt Idx(32);
711 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000712 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000713 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000714 return;
715 }
716 nullPos = Idx.getZExtValue();
717
718 if (nullPos > 1 || nullPos < 0) {
719 // FIXME: This error message could be improved, it would be nice
720 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000721 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
722 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000723 return;
724 }
725 }
726
727 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000728 const FunctionType *FT = FD->getType()->getAsFunctionType();
729 assert(FT && "FunctionDecl has non-function type?");
730
731 if (isa<FunctionNoProtoType>(FT)) {
732 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
733 return;
734 }
735
736 if (!cast<FunctionProtoType>(FT)->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;
739 }
740 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
741 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000742 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000743 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000744 }
745 } else if (isa<BlockDecl>(d)) {
746 // Note! BlockDecl is typeless. Variadic diagnostics
747 // will be issued by the caller.
748 ;
749 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000750 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000751 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
752 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
753 : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000754 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000755 int m = Ty->isFunctionPointerType() ? 0 : 1;
756 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000757 return;
758 }
759 }
760 else {
761 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 */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000763 return;
764 }
Anders Carlsson77091822008-10-05 18:05:59 +0000765 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000766 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000767 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000768 return;
769 }
Fariborz Jahanian88f1ba02009-05-13 23:20:50 +0000770 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000771}
772
Chris Lattner026dc962009-02-14 07:37:35 +0000773static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
774 // check the attribute arguments.
775 if (Attr.getNumArgs() != 0) {
776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
777 return;
778 }
779
780 // TODO: could also be applied to methods?
781 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
782 if (!Fn) {
783 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000784 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000785 return;
786 }
787
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000788 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000789}
790
791static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000792 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000793 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000794 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000795 return;
796 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000797
798 // TODO: could also be applied to methods?
799 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
800 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000801 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000802 return;
803 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000804
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000805 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000806}
807
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000808static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
809 // check the attribute arguments.
810 if (Attr.getNumArgs() != 0) {
811 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
812 return;
813 }
814
815 // weak_import only applies to variable & function declarations.
816 bool isDef = false;
817 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
818 isDef = (!VD->hasExternalStorage() || VD->getInit());
819 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor72971342009-04-18 00:02:19 +0000820 isDef = FD->getBody(S.Context);
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000821 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
822 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000823 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000824 } else {
825 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000826 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000827 return;
828 }
829
830 // Merge should handle any subsequent violations.
831 if (isDef) {
832 S.Diag(Attr.getLoc(),
833 diag::warn_attribute_weak_import_invalid_on_definition)
834 << "weak_import" << 2 /*variable and function*/;
835 return;
836 }
837
838 D->addAttr(::new (S.Context) WeakImportAttr());
839}
840
Chris Lattner026dc962009-02-14 07:37:35 +0000841static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000842 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000843 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000845 return;
846 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000847
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000848 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000849 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000850 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000851 return;
852 }
853
Chris Lattner026dc962009-02-14 07:37:35 +0000854 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000855 if (!FD) {
856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000857 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000858 return;
859 }
860
861 // Currently, the dllimport attribute is ignored for inlined functions.
862 // Warning is emitted.
863 if (FD->isInline()) {
864 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
865 return;
866 }
867
868 // The attribute is also overridden by a subsequent declaration as dllexport.
869 // Warning is emitted.
870 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
871 nextAttr = nextAttr->getNext()) {
872 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
873 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
874 return;
875 }
876 }
877
Chris Lattner026dc962009-02-14 07:37:35 +0000878 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000879 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
880 return;
881 }
882
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000883 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000884}
885
Chris Lattner026dc962009-02-14 07:37:35 +0000886static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000887 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000888 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000889 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000890 return;
891 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000892
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000893 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000894 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000895 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000896 return;
897 }
898
Chris Lattner026dc962009-02-14 07:37:35 +0000899 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000900 if (!FD) {
901 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000902 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000903 return;
904 }
905
906 // Currently, the dllexport attribute is ignored for inlined functions,
907 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
908 if (FD->isInline()) {
909 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
910 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
911 return;
912 }
913
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000914 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000915}
916
Chris Lattner026dc962009-02-14 07:37:35 +0000917static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000918 // Attribute has no arguments.
919 if (Attr.getNumArgs() != 1) {
920 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
921 return;
922 }
923
924 // Make sure that there is a string literal as the sections's single
925 // argument.
926 StringLiteral *SE =
927 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
928 if (!SE) {
929 // FIXME
930 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
931 return;
932 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000933 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
934 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000935}
936
Chris Lattner803d0802008-06-29 00:43:07 +0000937static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000938 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000939 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000940 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000941 return;
942 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000943
944 // Attribute can be applied only to functions.
945 if (!isa<FunctionDecl>(d)) {
946 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000947 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000948 return;
949 }
950
951 // stdcall and fastcall attributes are mutually incompatible.
952 if (d->getAttr<FastCallAttr>()) {
953 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
954 << "stdcall" << "fastcall";
955 return;
956 }
957
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000958 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000959}
960
Chris Lattner803d0802008-06-29 00:43:07 +0000961static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000962 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000963 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000964 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000965 return;
966 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000967
968 if (!isa<FunctionDecl>(d)) {
969 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000970 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000971 return;
972 }
973
974 // stdcall and fastcall attributes are mutually incompatible.
975 if (d->getAttr<StdCallAttr>()) {
976 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
977 << "fastcall" << "stdcall";
978 return;
979 }
980
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000981 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000982}
983
Chris Lattner803d0802008-06-29 00:43:07 +0000984static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000985 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000986 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000987 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000988 return;
989 }
990
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000991 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000992}
993
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000994static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
995 // check the attribute arguments.
996 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000997 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000998 return;
999 }
1000
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001001 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001002}
1003
1004static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1005 // check the attribute arguments.
1006 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001007 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001008 return;
1009 }
1010
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001011 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001012}
1013
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001014static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001015 // Match gcc which ignores cleanup attrs when compiling C++.
1016 if (S.getLangOptions().CPlusPlus)
1017 return;
1018
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001019 if (!Attr.getParameterName()) {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1021 return;
1022 }
1023
1024 if (Attr.getNumArgs() != 0) {
1025 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1026 return;
1027 }
1028
1029 VarDecl *VD = dyn_cast<VarDecl>(d);
1030
1031 if (!VD || !VD->hasLocalStorage()) {
1032 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1033 return;
1034 }
1035
1036 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001037 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1038 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001039 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001040 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001041 Attr.getParameterName();
1042 return;
1043 }
1044
1045 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1046 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001047 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001048 Attr.getParameterName();
1049 return;
1050 }
1051
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001052 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001053 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001054 Attr.getParameterName();
1055 return;
1056 }
1057
Anders Carlsson89941c12009-02-07 23:16:50 +00001058 // We're currently more strict than GCC about what function types we accept.
1059 // If this ever proves to be a problem it should be easy to fix.
1060 QualType Ty = S.Context.getPointerType(VD->getType());
1061 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001062 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001063 S.Diag(Attr.getLoc(),
1064 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1065 Attr.getParameterName() << ParamTy << Ty;
1066 return;
1067 }
1068
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001069 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001070}
1071
Chris Lattner6b6b5372008-06-26 18:38:35 +00001072/// Handle __attribute__((format(type,idx,firstarg))) attributes
1073/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001074static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001075
Chris Lattner545dd342008-06-28 23:36:30 +00001076 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001078 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001079 return;
1080 }
1081
Chris Lattner545dd342008-06-28 23:36:30 +00001082 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001083 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001084 return;
1085 }
1086
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001087 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001088 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001089 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001090 return;
1091 }
1092
1093 // FIXME: in C++ the implicit 'this' function parameter also counts.
1094 // this is needed in order to be compatible with GCC
1095 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001096 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001097 unsigned FirstIdx = 1;
1098
Chris Lattner545dd342008-06-28 23:36:30 +00001099 const char *Format = Attr.getParameterName()->getName();
1100 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001101
1102 // Normalize the argument, __foo__ becomes foo.
1103 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1104 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1105 Format += 2;
1106 FormatLen -= 4;
1107 }
1108
1109 bool Supported = false;
1110 bool is_NSString = false;
1111 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001112 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001113
1114 switch (FormatLen) {
1115 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001116 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1117 case 6: Supported = !memcmp(Format, "printf", 6); break;
1118 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001119 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001120 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1121 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1122 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001123 break;
1124 }
1125
1126 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001127 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1128 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001129 return;
1130 }
1131
1132 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001133 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001134 llvm::APSInt Idx(32);
1135 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001136 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001137 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001138 return;
1139 }
1140
1141 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001142 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001143 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001144 return;
1145 }
1146
1147 // FIXME: Do we need to bounds check?
1148 unsigned ArgIdx = Idx.getZExtValue() - 1;
1149
1150 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001151 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001152
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001153 if (is_CFString) {
1154 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001155 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1156 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001157 return;
1158 }
1159 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001160 // FIXME: do we need to check if the type is NSString*? What are the
1161 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001162 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001163 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001164 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1165 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166 return;
1167 }
1168 } else if (!Ty->isPointerType() ||
1169 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001170 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001171 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1172 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001173 return;
1174 }
1175
1176 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001177 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001178 llvm::APSInt FirstArg(32);
1179 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001181 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001182 return;
1183 }
1184
1185 // check if the function is variadic if the 3rd argument non-zero
1186 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001187 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188 ++NumArgs; // +1 for ...
1189 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001190 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001191 return;
1192 }
1193 }
1194
Chris Lattner3c73c412008-11-19 08:23:25 +00001195 // strftime requires FirstArg to be 0 because it doesn't read from any
1196 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001197 if (is_strftime) {
1198 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001199 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1200 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001201 return;
1202 }
1203 // if 0 it disables parameter checking (to use with e.g. va_list)
1204 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001206 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001207 return;
1208 }
1209
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001210 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001211 Idx.getZExtValue(), FirstArg.getZExtValue()));
1212}
1213
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001214static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1215 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001216 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001217 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001218 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001219 return;
1220 }
1221
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001222 // Try to find the underlying union declaration.
1223 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001224 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001225 if (TD && TD->getUnderlyingType()->isUnionType())
1226 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1227 else
1228 RD = dyn_cast<RecordDecl>(d);
1229
1230 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001231 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001232 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001233 return;
1234 }
1235
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001236 if (!RD->isDefinition()) {
1237 S.Diag(Attr.getLoc(),
1238 diag::warn_transparent_union_attribute_not_definition);
1239 return;
1240 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001242 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1243 FieldEnd = RD->field_end(S.Context);
1244 if (Field == FieldEnd) {
1245 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1246 return;
1247 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001248
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001249 FieldDecl *FirstField = *Field;
1250 QualType FirstType = FirstField->getType();
1251 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1252 S.Diag(FirstField->getLocation(),
1253 diag::warn_transparent_union_attribute_floating);
1254 return;
1255 }
1256
1257 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1258 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1259 for (; Field != FieldEnd; ++Field) {
1260 QualType FieldType = Field->getType();
1261 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1262 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1263 // Warn if we drop the attribute.
1264 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1265 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1266 : S.Context.getTypeAlign(FieldType);
1267 S.Diag(Field->getLocation(),
1268 diag::warn_transparent_union_attribute_field_size_align)
1269 << isSize << Field->getDeclName() << FieldBits;
1270 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1271 S.Diag(FirstField->getLocation(),
1272 diag::note_transparent_union_first_field_size_align)
1273 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001274 return;
1275 }
1276 }
1277
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001278 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001279}
1280
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001281static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001282 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001283 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001285 return;
1286 }
Chris Lattner545dd342008-06-28 23:36:30 +00001287 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001288 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1289
1290 // Make sure that there is a string literal as the annotation's single
1291 // argument.
1292 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001293 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001294 return;
1295 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001296 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1297 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001298}
1299
Chris Lattner803d0802008-06-29 00:43:07 +00001300static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001301 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001302 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001304 return;
1305 }
1306
1307 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001308 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001309 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001310 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001311 Align = 128;
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001312 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001314 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001315
1316 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1317 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001318 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1320 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001321 return;
1322 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001323 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1324 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1325 << alignmentExpr->getSourceRange();
1326 return;
1327 }
1328
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001329 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001330}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001331
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001332/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001333/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001334///
1335/// Despite what would be logical, the mode attribute is a decl attribute,
1336/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1337/// 'G' be HImode, not an intermediate pointer.
1338///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001339static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001340 // This attribute isn't documented, but glibc uses it. It changes
1341 // the width of an int or unsigned int to the specified size.
1342
1343 // Check that there aren't any arguments
1344 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001345 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001346 return;
1347 }
1348
1349 IdentifierInfo *Name = Attr.getParameterName();
1350 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001352 return;
1353 }
1354 const char *Str = Name->getName();
1355 unsigned Len = Name->getLength();
1356
1357 // Normalize the attribute name, __foo__ becomes foo.
1358 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1359 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1360 Str += 2;
1361 Len -= 4;
1362 }
1363
1364 unsigned DestWidth = 0;
1365 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001366 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001367 switch (Len) {
1368 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001369 switch (Str[0]) {
1370 case 'Q': DestWidth = 8; break;
1371 case 'H': DestWidth = 16; break;
1372 case 'S': DestWidth = 32; break;
1373 case 'D': DestWidth = 64; break;
1374 case 'X': DestWidth = 96; break;
1375 case 'T': DestWidth = 128; break;
1376 }
1377 if (Str[1] == 'F') {
1378 IntegerMode = false;
1379 } else if (Str[1] == 'C') {
1380 IntegerMode = false;
1381 ComplexMode = true;
1382 } else if (Str[1] != 'I') {
1383 DestWidth = 0;
1384 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001385 break;
1386 case 4:
1387 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1388 // pointer on PIC16 and other embedded platforms.
1389 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001390 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001391 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001392 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001393 break;
1394 case 7:
1395 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001396 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001397 break;
1398 }
1399
1400 QualType OldTy;
1401 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1402 OldTy = TD->getUnderlyingType();
1403 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1404 OldTy = VD->getType();
1405 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001406 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1407 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001408 return;
1409 }
Eli Friedman73397492009-03-03 06:41:03 +00001410
1411 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1412 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1413 else if (IntegerMode) {
1414 if (!OldTy->isIntegralType())
1415 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1416 } else if (ComplexMode) {
1417 if (!OldTy->isComplexType())
1418 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1419 } else {
1420 if (!OldTy->isFloatingType())
1421 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1422 }
1423
Mike Stump390b4cc2009-05-16 07:39:55 +00001424 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1425 // and friends, at least with glibc.
1426 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1427 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001428 // FIXME: Make sure floating-point mappings are accurate
1429 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001430 QualType NewTy;
1431 switch (DestWidth) {
1432 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001433 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001434 return;
1435 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001436 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001437 return;
1438 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001439 if (!IntegerMode) {
1440 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1441 return;
1442 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001443 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001444 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001445 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001446 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001447 break;
1448 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001449 if (!IntegerMode) {
1450 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1451 return;
1452 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001453 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001454 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001455 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001456 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001457 break;
1458 case 32:
1459 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001460 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001461 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001462 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001463 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001464 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001465 break;
1466 case 64:
1467 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001468 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001469 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001470 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001471 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001472 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001473 break;
Eli Friedman73397492009-03-03 06:41:03 +00001474 case 96:
1475 NewTy = S.Context.LongDoubleTy;
1476 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001477 case 128:
1478 if (!IntegerMode) {
1479 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1480 return;
1481 }
1482 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001483 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001484 }
1485
Eli Friedman73397492009-03-03 06:41:03 +00001486 if (ComplexMode) {
1487 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001488 }
1489
1490 // Install the new type.
1491 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1492 TD->setUnderlyingType(NewTy);
1493 else
1494 cast<ValueDecl>(D)->setType(NewTy);
1495}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001496
Anders Carlssond87df372009-02-13 06:46:13 +00001497static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1498 // check the attribute arguments.
1499 if (Attr.getNumArgs() > 0) {
1500 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1501 return;
1502 }
Anders Carlssone896d982009-02-13 08:11:52 +00001503
Anders Carlsson5bab7882009-02-19 19:16:48 +00001504 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001505 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001506 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001507 return;
1508 }
1509
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001510 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001511}
1512
Anders Carlsson5bab7882009-02-19 19:16:48 +00001513static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1514 // check the attribute arguments.
1515 if (Attr.getNumArgs() != 0) {
1516 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1517 return;
1518 }
1519
Chris Lattnerc5197432009-04-14 17:02:11 +00001520 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001521 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001522 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001523 return;
1524 }
1525
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001526 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001527}
1528
Chris Lattnercf2a7212009-04-20 19:12:28 +00001529static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001530 // check the attribute arguments.
1531 if (Attr.getNumArgs() != 0) {
1532 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1533 return;
1534 }
1535
Chris Lattnerc5197432009-04-14 17:02:11 +00001536 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1537 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001538 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001539 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001540 return;
1541 }
1542
Chris Lattnerc5197432009-04-14 17:02:11 +00001543 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001544 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001545 return;
1546 }
1547
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001548 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001549}
1550
Fariborz Jahanianee760332009-03-27 18:38:55 +00001551static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1552 // check the attribute arguments.
1553 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001554 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001555 return;
1556 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001557
Fariborz Jahanianee760332009-03-27 18:38:55 +00001558 if (!isFunctionOrMethod(d)) {
1559 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001560 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001561 return;
1562 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001563
1564 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1565 llvm::APSInt NumParams(32);
1566 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1567 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1568 << "regparm" << NumParamsExpr->getSourceRange();
1569 return;
1570 }
1571
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001572 if (S.Context.Target.getRegParmMax() == 0) {
1573 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001574 << NumParamsExpr->getSourceRange();
1575 return;
1576 }
1577
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001578 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001579 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1580 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001581 return;
1582 }
1583
1584 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001585}
1586
Chris Lattner0744e5f2008-06-29 00:23:49 +00001587//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001588// Checker-specific attribute handlers.
1589//===----------------------------------------------------------------------===//
1590
1591static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1592 Sema &S) {
1593
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001594 QualType RetTy;
1595
1596 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1597 RetTy = MD->getResultType();
1598 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1599 RetTy = FD->getResultType();
1600 else {
1601 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1602 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001603 return;
1604 }
1605
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001606 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1607 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1608 << Attr.getName();
1609 return;
1610 }
1611
Ted Kremenekb71368d2009-05-09 02:44:38 +00001612 switch (Attr.getKind()) {
1613 default:
1614 assert(0 && "invalid ownership attribute");
1615 return;
1616 case AttributeList::AT_cf_returns_retained:
1617 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1618 return;
1619 case AttributeList::AT_ns_returns_retained:
1620 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1621 return;
1622 };
1623}
1624
1625//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001626// Top Level Sema Entry Points
1627//===----------------------------------------------------------------------===//
1628
Sebastian Redla89d82c2008-12-21 19:24:58 +00001629/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001630/// the attribute applies to decls. If the attribute is a type attribute, just
1631/// silently ignore it.
1632static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1633 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001634 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001635 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001636 case AttributeList::AT_objc_gc:
1637 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001638 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001639 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1640 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001641 case AttributeList::AT_always_inline:
1642 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001643 case AttributeList::AT_analyzer_noreturn:
1644 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001645 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1646 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1647 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1648 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1649 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1650 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001651 case AttributeList::AT_ext_vector_type:
1652 HandleExtVectorTypeAttr(D, Attr, S);
1653 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001654 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1655 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001656 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001657 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001658 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1659 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1660 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001661
1662 // Checker-specific.
1663 case AttributeList::AT_ns_returns_retained:
1664 case AttributeList::AT_cf_returns_retained:
1665 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1666
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001667 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001668 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001669 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001670 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001671 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001672 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001673 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001674 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001675 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1676 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001677 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001678 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001679 case AttributeList::AT_transparent_union:
1680 HandleTransparentUnionAttr(D, Attr, S);
1681 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001682 case AttributeList::AT_objc_exception:
1683 HandleObjCExceptionAttr(D, Attr, S);
1684 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001685 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001686 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001687 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001688 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001689 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1690 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001691 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001692 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001693 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001694 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001695 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001696 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001697 // Just ignore
1698 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001699 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001700 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001701 break;
1702 }
1703}
1704
1705/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1706/// attribute list to the specified decl, ignoring any type attributes.
1707void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1708 while (AttrList) {
1709 ProcessDeclAttribute(D, *AttrList, *this);
1710 AttrList = AttrList->getNext();
1711 }
1712}
1713
Chris Lattner0744e5f2008-06-29 00:23:49 +00001714/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1715/// it, apply them to D. This is a bit tricky because PD can have attributes
1716/// specified in many different places, and we need to find and apply them all.
1717void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1718 // Apply decl attributes from the DeclSpec if present.
1719 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1720 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001721
Chris Lattner0744e5f2008-06-29 00:23:49 +00001722 // Walk the declarator structure, applying decl attributes that were in a type
1723 // position to the decl itself. This handles cases like:
1724 // int *__attr__(x)** D;
1725 // when X is a decl attribute.
1726 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1727 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1728 ProcessDeclAttributeList(D, Attrs);
1729
1730 // Finally, apply any attributes on the decl itself.
1731 if (const AttributeList *Attrs = PD.getAttributes())
1732 ProcessDeclAttributeList(D, Attrs);
1733}