blob: 5154c66c6cba60c3288ab14eb3891755c55c04f1 [file] [log] [blame]
Chris Lattner6953a072008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare0ad2152008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerdc789562008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek26151d12008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6953a072008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattner2024f0a2008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Daniel Dunbar0ea20472008-10-19 02:04:16 +000027static const FunctionType *getFunctionType(Decl *d) {
Chris Lattner6953a072008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6953a072008-06-26 18:38:35 +000029 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30 Ty = decl->getType();
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32 Ty = decl->getType();
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
35 else
36 return 0;
37
38 if (Ty->isFunctionPointerType())
39 Ty = Ty->getAsPointerType()->getPointeeType();
Fariborz Jahanianabe6c232009-05-15 23:15:03 +000040 else if (Ty->isBlockPointerType())
41 Ty = Ty->getAsBlockPointerType()->getPointeeType();
Daniel Dunbar0ea20472008-10-19 02:04:16 +000042
43 return Ty->getAsFunctionType();
Chris Lattner6953a072008-06-26 18:38:35 +000044}
45
Daniel Dunbard1d847c2008-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 Dunbar0ea20472008-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 Dunbard1d847c2008-09-26 04:12:28 +000052static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000053 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000054}
55
Fariborz Jahanianabe6c232009-05-15 23:15:03 +000056/// isFunctionOrMethodOrBlock - Return true if the given decl has function
57/// type (function or function-typed variable) or an Objective-C
58/// method or a block.
59static bool isFunctionOrMethodOrBlock(Decl *d) {
60 if (isFunctionOrMethod(d))
61 return true;
62 // check for block is more involved.
63 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
64 QualType Ty = V->getType();
65 return Ty->isBlockPointerType();
66 }
67 return false;
68}
69
Daniel Dunbar0ea20472008-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 Jahanianabe6c232009-05-15 23:15:03 +000072/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Daniel Dunbar0ea20472008-10-19 02:04:16 +000073static bool hasFunctionProto(Decl *d) {
Fariborz Jahanianabe6c232009-05-15 23:15:03 +000074 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor4fa58902009-02-26 23:50:07 +000075 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanianabe6c232009-05-15 23:15:03 +000076 else {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000077 assert(isa<ObjCMethodDecl>(d));
78 return true;
79 }
80}
81
82/// getFunctionOrMethodNumArgs - Return number of function or method
83/// arguments. It is an error to call this on a K&R function (use
84/// hasFunctionProto first).
Daniel Dunbard1d847c2008-09-26 04:12:28 +000085static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Chris Lattner5c6b2c62009-02-20 18:43:26 +000086 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor4fa58902009-02-26 23:50:07 +000087 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanianabe6c232009-05-15 23:15:03 +000088 else if (VarDecl *V = dyn_cast<VarDecl>(d)) {
89 QualType Ty = V->getType();
90 if (Ty->isBlockPointerType()) {
91 const FunctionType *FT =
92 Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
93 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
94 return Proto->getNumArgs();
95
96 }
97 assert(false && "getFunctionOrMethodNumArgs - caused by block mishap");
98 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +000099 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000100}
101
102static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000103 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor4fa58902009-02-26 23:50:07 +0000104 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000105
Fariborz Jahanianabe6c232009-05-15 23:15:03 +0000106
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000108}
109
110static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000111 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000112 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000113 return proto->isVariadic();
114 } else {
115 return cast<ObjCMethodDecl>(d)->isVariadic();
116 }
117}
118
Chris Lattner6953a072008-06-26 18:38:35 +0000119static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000120 const PointerType *PT = T->getAsPointerType();
121 if (!PT)
Chris Lattner6953a072008-06-26 18:38:35 +0000122 return false;
123
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000124 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6953a072008-06-26 18:38:35 +0000125 if (!ClsT)
126 return false;
127
128 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
129
130 // FIXME: Should we walk the chain of classes?
131 return ClsName == &Ctx.Idents.get("NSString") ||
132 ClsName == &Ctx.Idents.get("NSMutableString");
133}
134
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000135static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
136 const PointerType *PT = T->getAsPointerType();
137 if (!PT)
138 return false;
139
140 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
141 if (!RT)
142 return false;
143
144 const RecordDecl *RD = RT->getDecl();
145 if (RD->getTagKind() != TagDecl::TK_struct)
146 return false;
147
148 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
149}
150
Chris Lattner2024f0a2008-06-29 00:16:31 +0000151//===----------------------------------------------------------------------===//
Chris Lattner2024f0a2008-06-29 00:16:31 +0000152// Attribute Implementations
153//===----------------------------------------------------------------------===//
154
Daniel Dunbar8716a012008-07-31 22:40:48 +0000155// FIXME: All this manual attribute parsing code is gross. At the
156// least add some helper functions to check most argument patterns (#
157// and types of args).
158
Chris Lattner703c52d2008-06-29 00:43:07 +0000159static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
160 Sema &S) {
Chris Lattner1c151132008-06-28 23:36:30 +0000161 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
162 if (tDecl == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000163 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner1c151132008-06-28 23:36:30 +0000164 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000165 }
166
Chris Lattner6953a072008-06-26 18:38:35 +0000167 QualType curType = tDecl->getUnderlyingType();
168 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000169 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000170 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000171 return;
172 }
Chris Lattner1c151132008-06-28 23:36:30 +0000173 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000174 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000175 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000176 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
177 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000178 return;
179 }
180 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
181 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000182 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000183 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6953a072008-06-26 18:38:35 +0000184 return;
185 }
186 // unlike gcc's vector_size attribute, the size is specified as the
187 // number of elements, not the number of bytes.
188 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
189
190 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000191 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
192 << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000193 return;
194 }
195 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner703c52d2008-06-29 00:43:07 +0000196 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6953a072008-06-26 18:38:35 +0000197 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner703c52d2008-06-29 00:43:07 +0000198 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6953a072008-06-26 18:38:35 +0000199}
200
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000201
202/// HandleVectorSizeAttribute - this attribute is only applicable to
203/// integral and float scalars, although arrays, pointers, and function
204/// return values are allowed in conjunction with this construct. Aggregates
205/// with this attribute are invalid, even if they are of the same size as a
206/// corresponding scalar.
207/// The raw attribute should contain precisely 1 argument, the vector size
208/// for the variable, measured in bytes. If curType and rawAttr are well
209/// formed, this routine will return a new vector type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000210static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000211 QualType CurType;
212 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
213 CurType = VD->getType();
214 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
215 CurType = TD->getUnderlyingType();
216 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000217 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
218 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000219 return;
220 }
221
222 // Check the attribute arugments.
Chris Lattner1c151132008-06-28 23:36:30 +0000223 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000225 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000226 }
Chris Lattner1c151132008-06-28 23:36:30 +0000227 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000228 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000229 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
231 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000232 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000233 }
234 // navigate to the base type - we need to provide for vector pointers,
235 // vector arrays, and functions returning vectors.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000236 if (CurType->isPointerType() || CurType->isArrayType() ||
237 CurType->isFunctionType()) {
Chris Lattner077fed52009-05-13 04:00:12 +0000238 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
239 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000240 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
241 do {
242 if (PointerType *PT = dyn_cast<PointerType>(canonType))
243 canonType = PT->getPointeeType().getTypePtr();
244 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
245 canonType = AT->getElementType().getTypePtr();
246 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
247 canonType = FT->getResultType().getTypePtr();
248 } while (canonType->isPointerType() || canonType->isArrayType() ||
249 canonType->isFunctionType());
250 */
251 }
Chris Lattner2a15f8e2009-05-13 05:13:44 +0000252 // the base type must be integer or float, and can't already be a vector.
253 if (CurType->isVectorType() ||
254 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000255 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000256 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000257 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000258 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-06-26 18:38:35 +0000259 // vecSize is specified in bytes - convert to bits.
260 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
261
262 // the vector size needs to be an integral multiple of the type size.
263 if (vectorSize % typeSize) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000264 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
265 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000266 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000267 }
268 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000269 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
270 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000271 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000272 }
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000273
274 // Success! Instantiate the vector type, the number of elements is > 0, and
275 // not required to be a power of 2, unlike GCC.
Chris Lattner703c52d2008-06-29 00:43:07 +0000276 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000277
278 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
279 VD->setType(CurType);
280 else
281 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6953a072008-06-26 18:38:35 +0000282}
283
Chris Lattner703c52d2008-06-29 00:43:07 +0000284static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000285 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000286 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000287 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000288 return;
289 }
290
291 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner16ded572009-03-04 06:34:08 +0000292 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000293 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
294 // If the alignment is less than or equal to 8 bits, the packed attribute
295 // has no effect.
296 if (!FD->getType()->isIncompleteType() &&
Chris Lattner703c52d2008-06-29 00:43:07 +0000297 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000298 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000299 << Attr.getName() << FD->getType();
Chris Lattner6953a072008-06-26 18:38:35 +0000300 else
Chris Lattner16ded572009-03-04 06:34:08 +0000301 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000302 } else
Chris Lattner65cae292008-11-19 08:23:25 +0000303 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000304}
305
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000306static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
307 // check the attribute arguments.
308 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000309 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000310 return;
311 }
312
313 // The IBOutlet attribute only applies to instance variables of Objective-C
314 // classes.
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000315 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner16ded572009-03-04 06:34:08 +0000316 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000317 else
Ted Kremenek3ccaa822009-02-17 22:20:20 +0000318 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000319}
320
Ted Kremenekc0af2542008-07-21 21:53:04 +0000321static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000322 // GCC ignores the nonnull attribute on K&R style function
323 // prototypes, so we ignore it as well
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000324 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000325 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000326 << Attr.getName() << 0 /*function*/;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000327 return;
328 }
329
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000330 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000331
332 // The nonnull attribute only applies to pointers.
333 llvm::SmallVector<unsigned, 10> NonNullArgs;
334
335 for (AttributeList::arg_iterator I=Attr.arg_begin(),
336 E=Attr.arg_end(); I!=E; ++I) {
337
338
339 // The argument must be an integer constant expression.
Ted Kremenek73dc0652008-12-04 19:38:33 +0000340 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000341 llvm::APSInt ArgNum(32);
342 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000343 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
344 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000345 return;
346 }
347
348 unsigned x = (unsigned) ArgNum.getZExtValue();
349
350 if (x < 1 || x > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner68f621c2008-11-19 07:22:31 +0000352 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000353 return;
354 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000355
356 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000357
358 // Is the function argument a pointer type?
Ted Kremenekba57bd92008-11-18 06:52:58 +0000359 QualType T = getFunctionOrMethodArgType(d, x);
360 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000361 // FIXME: Should also highlight argument in decl.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000362 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
363 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7af441b2008-09-01 19:57:52 +0000364 continue;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000365 }
366
367 NonNullArgs.push_back(x);
368 }
369
Ted Kremenek7af441b2008-09-01 19:57:52 +0000370 // If no arguments were specified to __attribute__((nonnull)) then all
371 // pointer arguments have a nonnull attribute.
372 if (NonNullArgs.empty()) {
Ted Kremenekba57bd92008-11-18 06:52:58 +0000373 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
374 QualType T = getFunctionOrMethodArgType(d, I);
375 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000376 NonNullArgs.push_back(I);
Ted Kremenekba57bd92008-11-18 06:52:58 +0000377 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000378
379 if (NonNullArgs.empty()) {
380 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
381 return;
382 }
Ted Kremenekc0af2542008-07-21 21:53:04 +0000383 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000384
385 unsigned* start = &NonNullArgs[0];
386 unsigned size = NonNullArgs.size();
387 std::sort(start, start + size);
Chris Lattner16ded572009-03-04 06:34:08 +0000388 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekc0af2542008-07-21 21:53:04 +0000389}
390
Chris Lattner703c52d2008-06-29 00:43:07 +0000391static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000392 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000393 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000394 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000395 return;
396 }
397
Chris Lattner1c151132008-06-28 23:36:30 +0000398 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000399 Arg = Arg->IgnoreParenCasts();
400 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
401
402 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000403 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000404 << "alias" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000405 return;
406 }
407
408 const char *Alias = Str->getStrData();
409 unsigned AliasLen = Str->getByteLength();
410
411 // FIXME: check if target symbol exists in current file
412
Chris Lattner16ded572009-03-04 06:34:08 +0000413 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6953a072008-06-26 18:38:35 +0000414}
415
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000416static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
417 Sema &S) {
418 // check the attribute arguments.
419 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000421 return;
422 }
Anders Carlssoneb12e782009-02-19 19:16:48 +0000423
Chris Lattner2a3e43d2009-04-14 17:02:11 +0000424 if (!isa<FunctionDecl>(d)) {
Anders Carlssoneb12e782009-02-19 19:16:48 +0000425 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000426 << Attr.getName() << 0 /*function*/;
Anders Carlssoneb12e782009-02-19 19:16:48 +0000427 return;
428 }
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000429
Chris Lattner16ded572009-03-04 06:34:08 +0000430 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000431}
432
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000433static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000434 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000435 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000436 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000438 return false;
Chris Lattner6953a072008-06-26 18:38:35 +0000439 }
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000440
Mike Stump115a0722009-04-29 19:03:13 +0000441 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
442 ValueDecl *VD = dyn_cast<ValueDecl>(d);
443 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000445 << Attr.getName() << 0 /*function*/;
Mike Stump115a0722009-04-29 19:03:13 +0000446 return false;
447 }
Chris Lattner6953a072008-06-26 18:38:35 +0000448 }
449
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000450 return true;
451}
452
453static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000454 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000455 d->addAttr(::new (S.Context) NoReturnAttr());
456}
457
458static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
459 Sema &S) {
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000460 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenek1ce91f22009-04-10 00:01:14 +0000461 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000462}
463
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000464static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
465 // check the attribute arguments.
466 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000468 return;
469 }
470
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000471 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000472 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000473 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000474 return;
475 }
476
Chris Lattner16ded572009-03-04 06:34:08 +0000477 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000478}
479
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000480static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
481 // check the attribute arguments.
482 if (Attr.getNumArgs() != 0) {
483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
484 return;
485 }
486
487 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbard0cc5242009-02-13 22:48:56 +0000488 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000489 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
490 return;
491 }
492 } else if (!isFunctionOrMethod(d)) {
493 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000494 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000495 return;
496 }
497
Chris Lattner16ded572009-03-04 06:34:08 +0000498 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb88b4f72009-02-13 19:23:53 +0000499}
500
Daniel Dunbar8716a012008-07-31 22:40:48 +0000501static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
502 // check the attribute arguments.
503 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
505 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000506 return;
507 }
508
509 int priority = 65535; // FIXME: Do not hardcode such constants.
510 if (Attr.getNumArgs() > 0) {
511 Expr *E = static_cast<Expr *>(Attr.getArg(0));
512 llvm::APSInt Idx(32);
513 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000514 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000515 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000516 return;
517 }
518 priority = Idx.getZExtValue();
519 }
520
Chris Lattner2a3e43d2009-04-14 17:02:11 +0000521 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000522 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000523 << Attr.getName() << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000524 return;
525 }
526
Chris Lattner16ded572009-03-04 06:34:08 +0000527 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar8716a012008-07-31 22:40:48 +0000528}
529
530static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
531 // check the attribute arguments.
532 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
534 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000535 return;
536 }
537
538 int priority = 65535; // FIXME: Do not hardcode such constants.
539 if (Attr.getNumArgs() > 0) {
540 Expr *E = static_cast<Expr *>(Attr.getArg(0));
541 llvm::APSInt Idx(32);
542 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000543 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000544 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000545 return;
546 }
547 priority = Idx.getZExtValue();
548 }
549
Anders Carlsson03e49652008-08-22 22:10:48 +0000550 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000552 << Attr.getName() << 0 /*function*/;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000553 return;
554 }
555
Chris Lattner16ded572009-03-04 06:34:08 +0000556 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar8716a012008-07-31 22:40:48 +0000557}
558
Chris Lattner703c52d2008-06-29 00:43:07 +0000559static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000560 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000561 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000563 return;
564 }
565
Chris Lattner16ded572009-03-04 06:34:08 +0000566 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000567}
568
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000569static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
570 // check the attribute arguments.
571 if (Attr.getNumArgs() != 0) {
572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
573 return;
574 }
575
Chris Lattner16ded572009-03-04 06:34:08 +0000576 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000577}
578
Chris Lattner703c52d2008-06-29 00:43:07 +0000579static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000580 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000581 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000583 return;
584 }
585
Chris Lattner1c151132008-06-28 23:36:30 +0000586 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000587 Arg = Arg->IgnoreParenCasts();
588 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
589
590 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000591 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000592 << "visibility" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000593 return;
594 }
595
596 const char *TypeStr = Str->getStrData();
597 unsigned TypeLen = Str->getByteLength();
598 VisibilityAttr::VisibilityTypes type;
599
600 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
601 type = VisibilityAttr::DefaultVisibility;
602 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
603 type = VisibilityAttr::HiddenVisibility;
604 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
605 type = VisibilityAttr::HiddenVisibility; // FIXME
606 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
607 type = VisibilityAttr::ProtectedVisibility;
608 else {
Chris Lattnerb1753422008-11-23 21:45:46 +0000609 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6953a072008-06-26 18:38:35 +0000610 return;
611 }
612
Chris Lattner16ded572009-03-04 06:34:08 +0000613 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6953a072008-06-26 18:38:35 +0000614}
615
Chris Lattnera9b98462009-02-14 08:09:34 +0000616static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
617 Sema &S) {
618 if (Attr.getNumArgs() != 0) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
620 return;
621 }
622
623 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
624 if (OCI == 0) {
625 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
626 return;
627 }
628
Chris Lattner16ded572009-03-04 06:34:08 +0000629 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattnera9b98462009-02-14 08:09:34 +0000630}
631
632static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000633 if (Attr.getNumArgs() != 0) {
634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
635 return;
636 }
Chris Lattnera9b98462009-02-14 08:09:34 +0000637 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000638 QualType T = TD->getUnderlyingType();
639 if (!T->isPointerType() ||
640 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
641 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
642 return;
643 }
644 }
Chris Lattner16ded572009-03-04 06:34:08 +0000645 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian82f54962009-01-13 23:34:40 +0000646}
647
Douglas Gregorfcb19192009-02-11 23:02:49 +0000648static void
649HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
650 if (Attr.getNumArgs() != 0) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
652 return;
653 }
654
655 if (!isa<FunctionDecl>(D)) {
656 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
657 return;
658 }
659
Chris Lattner16ded572009-03-04 06:34:08 +0000660 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorfcb19192009-02-11 23:02:49 +0000661}
662
Steve Naroffe1cecba2008-09-18 16:44:58 +0000663static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
664 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000666 << "blocks" << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000667 return;
668 }
669
670 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000672 return;
673 }
Steve Naroffe1cecba2008-09-18 16:44:58 +0000674
675 BlocksAttr::BlocksAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000676 if (Attr.getParameterName()->isStr("byref"))
Steve Naroffe1cecba2008-09-18 16:44:58 +0000677 type = BlocksAttr::ByRef;
678 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000679 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000680 << "blocks" << Attr.getParameterName();
Steve Naroffe1cecba2008-09-18 16:44:58 +0000681 return;
682 }
683
Chris Lattner16ded572009-03-04 06:34:08 +0000684 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroffe1cecba2008-09-18 16:44:58 +0000685}
686
Anders Carlsson244d99b2008-10-05 18:05:59 +0000687static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
688 // check the attribute arguments.
689 if (Attr.getNumArgs() > 2) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
691 << "0, 1 or 2";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000692 return;
693 }
694
695 int sentinel = 0;
696 if (Attr.getNumArgs() > 0) {
697 Expr *E = static_cast<Expr *>(Attr.getArg(0));
698 llvm::APSInt Idx(32);
699 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000700 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000701 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000702 return;
703 }
704 sentinel = Idx.getZExtValue();
705
706 if (sentinel < 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
708 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000709 return;
710 }
711 }
712
713 int nullPos = 0;
714 if (Attr.getNumArgs() > 1) {
715 Expr *E = static_cast<Expr *>(Attr.getArg(1));
716 llvm::APSInt Idx(32);
717 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000718 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000719 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000720 return;
721 }
722 nullPos = Idx.getZExtValue();
723
724 if (nullPos > 1 || nullPos < 0) {
725 // FIXME: This error message could be improved, it would be nice
726 // to say what the bounds actually are.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000727 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
728 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000729 return;
730 }
731 }
732
733 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner19252782009-03-17 23:03:47 +0000734 const FunctionType *FT = FD->getType()->getAsFunctionType();
735 assert(FT && "FunctionDecl has non-function type?");
736
737 if (isa<FunctionNoProtoType>(FT)) {
738 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
739 return;
740 }
741
742 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6dac16d2009-05-15 21:18:04 +0000743 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000744 return;
745 }
746 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
747 if (!MD->isVariadic()) {
Fariborz Jahanian6dac16d2009-05-15 21:18:04 +0000748 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000749 return;
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000750 }
751 } else if (isa<BlockDecl>(d)) {
752 // Note! BlockDecl is typeless. Variadic diagnostics
753 // will be issued by the caller.
754 ;
755 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000756 QualType Ty = V->getType();
Fariborz Jahanianc10357d2009-05-15 20:33:25 +0000757 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
758 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
759 : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000760 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6dac16d2009-05-15 21:18:04 +0000761 int m = Ty->isFunctionPointerType() ? 0 : 1;
762 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000763 return;
764 }
765 }
766 else {
767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian8dde59b2009-05-14 20:57:28 +0000768 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian157c4262009-05-14 20:53:39 +0000769 return;
770 }
Anders Carlsson244d99b2008-10-05 18:05:59 +0000771 } else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000772 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian8dde59b2009-05-14 20:57:28 +0000773 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson244d99b2008-10-05 18:05:59 +0000774 return;
775 }
Fariborz Jahanian79d29e72009-05-13 23:20:50 +0000776 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson244d99b2008-10-05 18:05:59 +0000777}
778
Chris Lattnerd2c66552009-02-14 07:37:35 +0000779static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
780 // check the attribute arguments.
781 if (Attr.getNumArgs() != 0) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
783 return;
784 }
785
786 // TODO: could also be applied to methods?
787 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
788 if (!Fn) {
789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000790 << Attr.getName() << 0 /*function*/;
Chris Lattnerd2c66552009-02-14 07:37:35 +0000791 return;
792 }
793
Chris Lattner16ded572009-03-04 06:34:08 +0000794 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattnerd2c66552009-02-14 07:37:35 +0000795}
796
797static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000798 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000799 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000801 return;
802 }
Daniel Dunbar42e41392009-03-06 06:39:57 +0000803
804 // TODO: could also be applied to methods?
805 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
806 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000807 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar42e41392009-03-06 06:39:57 +0000808 return;
809 }
Chris Lattner6953a072008-06-26 18:38:35 +0000810
Chris Lattner16ded572009-03-04 06:34:08 +0000811 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000812}
813
Daniel Dunbar42e41392009-03-06 06:39:57 +0000814static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
815 // check the attribute arguments.
816 if (Attr.getNumArgs() != 0) {
817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
818 return;
819 }
820
821 // weak_import only applies to variable & function declarations.
822 bool isDef = false;
823 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
824 isDef = (!VD->hasExternalStorage() || VD->getInit());
825 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregore3241e92009-04-18 00:02:19 +0000826 isDef = FD->getBody(S.Context);
Fariborz Jahaniane1d5e5a2009-05-04 19:35:12 +0000827 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
828 // We ignore weak import on properties and methods
Mike Stump1ba365c2009-03-18 17:39:31 +0000829 return;
Daniel Dunbar42e41392009-03-06 06:39:57 +0000830 } else {
831 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000832 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar42e41392009-03-06 06:39:57 +0000833 return;
834 }
835
836 // Merge should handle any subsequent violations.
837 if (isDef) {
838 S.Diag(Attr.getLoc(),
839 diag::warn_attribute_weak_import_invalid_on_definition)
840 << "weak_import" << 2 /*variable and function*/;
841 return;
842 }
843
844 D->addAttr(::new (S.Context) WeakImportAttr());
845}
846
Chris Lattnerd2c66552009-02-14 07:37:35 +0000847static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000848 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000849 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000851 return;
852 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000853
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000854 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000855 if (isa<VarDecl>(D)) {
Chris Lattner16ded572009-03-04 06:34:08 +0000856 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000857 return;
858 }
859
Chris Lattnerd2c66552009-02-14 07:37:35 +0000860 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000861 if (!FD) {
862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000863 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000864 return;
865 }
866
867 // Currently, the dllimport attribute is ignored for inlined functions.
868 // Warning is emitted.
869 if (FD->isInline()) {
870 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
871 return;
872 }
873
874 // The attribute is also overridden by a subsequent declaration as dllexport.
875 // Warning is emitted.
876 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
877 nextAttr = nextAttr->getNext()) {
878 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
879 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
880 return;
881 }
882 }
883
Chris Lattnerd2c66552009-02-14 07:37:35 +0000884 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
886 return;
887 }
888
Chris Lattner16ded572009-03-04 06:34:08 +0000889 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000890}
891
Chris Lattnerd2c66552009-02-14 07:37:35 +0000892static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000893 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000894 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000896 return;
897 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000898
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000899 // Attribute can be applied only to functions or variables.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000900 if (isa<VarDecl>(D)) {
Chris Lattner16ded572009-03-04 06:34:08 +0000901 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000902 return;
903 }
904
Chris Lattnerd2c66552009-02-14 07:37:35 +0000905 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000906 if (!FD) {
907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000908 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000909 return;
910 }
911
912 // Currently, the dllexport attribute is ignored for inlined functions,
913 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
914 if (FD->isInline()) {
915 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
917 return;
918 }
919
Chris Lattner16ded572009-03-04 06:34:08 +0000920 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000921}
922
Chris Lattnerd2c66552009-02-14 07:37:35 +0000923static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar97509722009-02-12 17:28:23 +0000924 // Attribute has no arguments.
925 if (Attr.getNumArgs() != 1) {
926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
927 return;
928 }
929
930 // Make sure that there is a string literal as the sections's single
931 // argument.
932 StringLiteral *SE =
933 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
934 if (!SE) {
935 // FIXME
936 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
937 return;
938 }
Chris Lattner16ded572009-03-04 06:34:08 +0000939 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
940 SE->getByteLength())));
Daniel Dunbar97509722009-02-12 17:28:23 +0000941}
942
Chris Lattner703c52d2008-06-29 00:43:07 +0000943static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000944 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000945 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000947 return;
948 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000949
950 // Attribute can be applied only to functions.
951 if (!isa<FunctionDecl>(d)) {
952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000953 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000954 return;
955 }
956
957 // stdcall and fastcall attributes are mutually incompatible.
958 if (d->getAttr<FastCallAttr>()) {
959 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
960 << "stdcall" << "fastcall";
961 return;
962 }
963
Chris Lattner16ded572009-03-04 06:34:08 +0000964 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000965}
966
Chris Lattner703c52d2008-06-29 00:43:07 +0000967static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000968 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000969 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000970 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000971 return;
972 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000973
974 if (!isa<FunctionDecl>(d)) {
975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +0000976 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov79626942008-12-23 22:24:07 +0000977 return;
978 }
979
980 // stdcall and fastcall attributes are mutually incompatible.
981 if (d->getAttr<StdCallAttr>()) {
982 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
983 << "fastcall" << "stdcall";
984 return;
985 }
986
Chris Lattner16ded572009-03-04 06:34:08 +0000987 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000988}
989
Chris Lattner703c52d2008-06-29 00:43:07 +0000990static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000991 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000992 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000993 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000994 return;
995 }
996
Chris Lattner16ded572009-03-04 06:34:08 +0000997 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000998}
999
Anders Carlssondd6791c2008-10-05 23:32:53 +00001000static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1001 // check the attribute arguments.
1002 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001004 return;
1005 }
1006
Chris Lattner16ded572009-03-04 06:34:08 +00001007 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssondd6791c2008-10-05 23:32:53 +00001008}
1009
1010static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1011 // check the attribute arguments.
1012 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001013 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001014 return;
1015 }
1016
Chris Lattner16ded572009-03-04 06:34:08 +00001017 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssondd6791c2008-10-05 23:32:53 +00001018}
1019
Anders Carlsson677e38f2009-01-31 01:16:18 +00001020static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001021 // Match gcc which ignores cleanup attrs when compiling C++.
1022 if (S.getLangOptions().CPlusPlus)
1023 return;
1024
Anders Carlsson677e38f2009-01-31 01:16:18 +00001025 if (!Attr.getParameterName()) {
1026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1027 return;
1028 }
1029
1030 if (Attr.getNumArgs() != 0) {
1031 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1032 return;
1033 }
1034
1035 VarDecl *VD = dyn_cast<VarDecl>(d);
1036
1037 if (!VD || !VD->hasLocalStorage()) {
1038 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1039 return;
1040 }
1041
1042 // Look up the function
Douglas Gregor09be81b2009-02-04 17:27:36 +00001043 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1044 Sema::LookupOrdinaryName);
Anders Carlsson677e38f2009-01-31 01:16:18 +00001045 if (!CleanupDecl) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001046 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001047 Attr.getParameterName();
1048 return;
1049 }
1050
1051 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1052 if (!FD) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001053 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001054 Attr.getParameterName();
1055 return;
1056 }
1057
Anders Carlsson677e38f2009-01-31 01:16:18 +00001058 if (FD->getNumParams() != 1) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001059 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlsson677e38f2009-01-31 01:16:18 +00001060 Attr.getParameterName();
1061 return;
1062 }
1063
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001064 // We're currently more strict than GCC about what function types we accept.
1065 // If this ever proves to be a problem it should be easy to fix.
1066 QualType Ty = S.Context.getPointerType(VD->getType());
1067 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedman7ce98682009-04-26 01:30:08 +00001068 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson9b3264d2009-02-07 23:16:50 +00001069 S.Diag(Attr.getLoc(),
1070 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1071 Attr.getParameterName() << ParamTy << Ty;
1072 return;
1073 }
1074
Chris Lattner16ded572009-03-04 06:34:08 +00001075 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlsson677e38f2009-01-31 01:16:18 +00001076}
1077
Chris Lattner6953a072008-06-26 18:38:35 +00001078/// Handle __attribute__((format(type,idx,firstarg))) attributes
1079/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +00001080static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001081
Chris Lattner1c151132008-06-28 23:36:30 +00001082 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001083 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +00001084 << "format" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001085 return;
1086 }
1087
Chris Lattner1c151132008-06-28 23:36:30 +00001088 if (Attr.getNumArgs() != 2) {
Chris Lattner65cae292008-11-19 08:23:25 +00001089 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6953a072008-06-26 18:38:35 +00001090 return;
1091 }
1092
Fariborz Jahanianabe6c232009-05-15 23:15:03 +00001093 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001094 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001095 << Attr.getName() << 0 /*function*/;
Chris Lattner6953a072008-06-26 18:38:35 +00001096 return;
1097 }
1098
1099 // FIXME: in C++ the implicit 'this' function parameter also counts.
1100 // this is needed in order to be compatible with GCC
1101 // the index must start in 1 and the limit is numargs+1
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001102 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6953a072008-06-26 18:38:35 +00001103 unsigned FirstIdx = 1;
1104
Chris Lattner1c151132008-06-28 23:36:30 +00001105 const char *Format = Attr.getParameterName()->getName();
1106 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +00001107
1108 // Normalize the argument, __foo__ becomes foo.
1109 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1110 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1111 Format += 2;
1112 FormatLen -= 4;
1113 }
1114
1115 bool Supported = false;
1116 bool is_NSString = false;
1117 bool is_strftime = false;
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001118 bool is_CFString = false;
Chris Lattner6953a072008-06-26 18:38:35 +00001119
1120 switch (FormatLen) {
1121 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001122 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1123 case 6: Supported = !memcmp(Format, "printf", 6); break;
1124 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +00001125 case 8:
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001126 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1127 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1128 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001129 break;
1130 }
1131
1132 if (!Supported) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001133 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1134 << "format" << Attr.getParameterName()->getName();
Chris Lattner6953a072008-06-26 18:38:35 +00001135 return;
1136 }
1137
1138 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001139 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +00001140 llvm::APSInt Idx(32);
1141 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001142 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001143 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001144 return;
1145 }
1146
1147 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001149 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001150 return;
1151 }
1152
1153 // FIXME: Do we need to bounds check?
1154 unsigned ArgIdx = Idx.getZExtValue() - 1;
1155
1156 // make sure the format string is really a string
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001157 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6953a072008-06-26 18:38:35 +00001158
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001159 if (is_CFString) {
1160 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1162 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbarbddb14a2008-09-26 03:32:58 +00001163 return;
1164 }
1165 } else if (is_NSString) {
Chris Lattner6953a072008-06-26 18:38:35 +00001166 // FIXME: do we need to check if the type is NSString*? What are
1167 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +00001168 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001169 // FIXME: Should highlight the actual expression that has the
1170 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001171 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1172 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001173 return;
1174 }
1175 } else if (!Ty->isPointerType() ||
1176 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1177 // FIXME: Should highlight the actual expression that has the
1178 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +00001179 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1180 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001181 return;
1182 }
1183
1184 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001185 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +00001186 llvm::APSInt FirstArg(32);
1187 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001189 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001190 return;
1191 }
1192
1193 // check if the function is variadic if the 3rd argument non-zero
1194 if (FirstArg != 0) {
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001195 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001196 ++NumArgs; // +1 for ...
1197 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +00001198 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +00001199 return;
1200 }
1201 }
1202
Chris Lattner65cae292008-11-19 08:23:25 +00001203 // strftime requires FirstArg to be 0 because it doesn't read from any
1204 // variable the input is just the current time + the format string.
Chris Lattner6953a072008-06-26 18:38:35 +00001205 if (is_strftime) {
1206 if (FirstArg != 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001207 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1208 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001209 return;
1210 }
1211 // if 0 it disables parameter checking (to use with e.g. va_list)
1212 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001213 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001214 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001215 return;
1216 }
1217
Chris Lattner16ded572009-03-04 06:34:08 +00001218 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6953a072008-06-26 18:38:35 +00001219 Idx.getZExtValue(), FirstArg.getZExtValue()));
1220}
1221
Chris Lattnerf6690152008-06-29 00:28:59 +00001222static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1223 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001224 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001225 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001226 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +00001227 return;
1228 }
1229
Douglas Gregor144b06c2009-04-29 22:16:16 +00001230 // Try to find the underlying union declaration.
1231 RecordDecl *RD = 0;
Eli Friedman283b3622008-09-02 05:19:23 +00001232 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor144b06c2009-04-29 22:16:16 +00001233 if (TD && TD->getUnderlyingType()->isUnionType())
1234 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1235 else
1236 RD = dyn_cast<RecordDecl>(d);
1237
1238 if (!RD || !RD->isUnion()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001239 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001240 << Attr.getName() << 1 /*union*/;
Chris Lattner6953a072008-06-26 18:38:35 +00001241 return;
1242 }
1243
Douglas Gregor144b06c2009-04-29 22:16:16 +00001244 if (!RD->isDefinition()) {
1245 S.Diag(Attr.getLoc(),
1246 diag::warn_transparent_union_attribute_not_definition);
1247 return;
1248 }
Chris Lattner6953a072008-06-26 18:38:35 +00001249
Douglas Gregor144b06c2009-04-29 22:16:16 +00001250 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1251 FieldEnd = RD->field_end(S.Context);
1252 if (Field == FieldEnd) {
1253 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1254 return;
1255 }
Eli Friedman283b3622008-09-02 05:19:23 +00001256
Douglas Gregor144b06c2009-04-29 22:16:16 +00001257 FieldDecl *FirstField = *Field;
1258 QualType FirstType = FirstField->getType();
1259 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1260 S.Diag(FirstField->getLocation(),
1261 diag::warn_transparent_union_attribute_floating);
1262 return;
1263 }
1264
1265 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1266 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1267 for (; Field != FieldEnd; ++Field) {
1268 QualType FieldType = Field->getType();
1269 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1270 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1271 // Warn if we drop the attribute.
1272 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1273 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1274 : S.Context.getTypeAlign(FieldType);
1275 S.Diag(Field->getLocation(),
1276 diag::warn_transparent_union_attribute_field_size_align)
1277 << isSize << Field->getDeclName() << FieldBits;
1278 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1279 S.Diag(FirstField->getLocation(),
1280 diag::note_transparent_union_first_field_size_align)
1281 << isSize << FirstBits;
Eli Friedman283b3622008-09-02 05:19:23 +00001282 return;
1283 }
1284 }
1285
Douglas Gregor144b06c2009-04-29 22:16:16 +00001286 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6953a072008-06-26 18:38:35 +00001287}
1288
Chris Lattnerf6690152008-06-29 00:28:59 +00001289static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001290 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001291 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001292 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001293 return;
1294 }
Chris Lattner1c151132008-06-28 23:36:30 +00001295 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +00001296 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1297
1298 // Make sure that there is a string literal as the annotation's single
1299 // argument.
1300 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001301 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +00001302 return;
1303 }
Chris Lattner16ded572009-03-04 06:34:08 +00001304 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1305 SE->getByteLength())));
Chris Lattner6953a072008-06-26 18:38:35 +00001306}
1307
Chris Lattner703c52d2008-06-29 00:43:07 +00001308static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001309 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001310 if (Attr.getNumArgs() > 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001312 return;
1313 }
1314
1315 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +00001316 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +00001317 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbare892c042009-02-18 20:06:09 +00001318 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6953a072008-06-26 18:38:35 +00001319 Align = 128;
Chris Lattner16ded572009-03-04 06:34:08 +00001320 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6953a072008-06-26 18:38:35 +00001321 return;
Chris Lattner6953a072008-06-26 18:38:35 +00001322 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001323
1324 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1325 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +00001326 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001327 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1328 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001329 return;
1330 }
Daniel Dunbar3ed413a2009-02-16 23:37:57 +00001331 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1332 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1333 << alignmentExpr->getSourceRange();
1334 return;
1335 }
1336
Chris Lattner16ded572009-03-04 06:34:08 +00001337 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001338}
Chris Lattnerdc789562008-06-27 22:18:37 +00001339
Chris Lattnerf6690152008-06-29 00:28:59 +00001340/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +00001341/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +00001342///
1343/// Despite what would be logical, the mode attribute is a decl attribute,
1344/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1345/// 'G' be HImode, not an intermediate pointer.
1346///
Chris Lattnerf6690152008-06-29 00:28:59 +00001347static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +00001348 // This attribute isn't documented, but glibc uses it. It changes
1349 // the width of an int or unsigned int to the specified size.
1350
1351 // Check that there aren't any arguments
1352 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001353 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerdc789562008-06-27 22:18:37 +00001354 return;
1355 }
1356
1357 IdentifierInfo *Name = Attr.getParameterName();
1358 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001359 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +00001360 return;
1361 }
1362 const char *Str = Name->getName();
1363 unsigned Len = Name->getLength();
1364
1365 // Normalize the attribute name, __foo__ becomes foo.
1366 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1367 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1368 Str += 2;
1369 Len -= 4;
1370 }
1371
1372 unsigned DestWidth = 0;
1373 bool IntegerMode = true;
Eli Friedman29fa4262009-03-03 06:41:03 +00001374 bool ComplexMode = false;
Chris Lattnerdc789562008-06-27 22:18:37 +00001375 switch (Len) {
1376 case 2:
Eli Friedman29fa4262009-03-03 06:41:03 +00001377 switch (Str[0]) {
1378 case 'Q': DestWidth = 8; break;
1379 case 'H': DestWidth = 16; break;
1380 case 'S': DestWidth = 32; break;
1381 case 'D': DestWidth = 64; break;
1382 case 'X': DestWidth = 96; break;
1383 case 'T': DestWidth = 128; break;
1384 }
1385 if (Str[1] == 'F') {
1386 IntegerMode = false;
1387 } else if (Str[1] == 'C') {
1388 IntegerMode = false;
1389 ComplexMode = true;
1390 } else if (Str[1] != 'I') {
1391 DestWidth = 0;
1392 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001393 break;
1394 case 4:
1395 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1396 // pointer on PIC16 and other embedded platforms.
1397 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001398 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001399 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001400 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +00001401 break;
1402 case 7:
1403 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +00001404 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001405 break;
1406 }
1407
1408 QualType OldTy;
1409 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1410 OldTy = TD->getUnderlyingType();
1411 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1412 OldTy = VD->getType();
1413 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001414 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1415 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerdc789562008-06-27 22:18:37 +00001416 return;
1417 }
Eli Friedman29fa4262009-03-03 06:41:03 +00001418
1419 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1420 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1421 else if (IntegerMode) {
1422 if (!OldTy->isIntegralType())
1423 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1424 } else if (ComplexMode) {
1425 if (!OldTy->isComplexType())
1426 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1427 } else {
1428 if (!OldTy->isFloatingType())
1429 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1430 }
1431
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001432 // FIXME: Sync this with InitializePredefinedMacros; we need to match
1433 // int8_t and friends, at least with glibc.
1434 // FIXME: Make sure 32/64-bit integers don't get defined to types of
1435 // the wrong width on unusual platforms.
1436 // FIXME: Make sure floating-point mappings are accurate
1437 // FIXME: Support XF and TF types
Chris Lattnerdc789562008-06-27 22:18:37 +00001438 QualType NewTy;
1439 switch (DestWidth) {
1440 case 0:
Chris Lattner65cae292008-11-19 08:23:25 +00001441 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001442 return;
1443 default:
Chris Lattner65cae292008-11-19 08:23:25 +00001444 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001445 return;
1446 case 8:
Eli Friedman29fa4262009-03-03 06:41:03 +00001447 if (!IntegerMode) {
1448 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1449 return;
1450 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001451 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001452 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001453 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001454 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001455 break;
1456 case 16:
Eli Friedman29fa4262009-03-03 06:41:03 +00001457 if (!IntegerMode) {
1458 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1459 return;
1460 }
Chris Lattnerdc789562008-06-27 22:18:37 +00001461 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001462 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001463 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001464 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001465 break;
1466 case 32:
1467 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001468 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001469 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001470 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001471 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001472 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001473 break;
1474 case 64:
1475 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001476 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001477 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001478 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001479 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001480 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001481 break;
Eli Friedman29fa4262009-03-03 06:41:03 +00001482 case 96:
1483 NewTy = S.Context.LongDoubleTy;
1484 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001485 case 128:
1486 if (!IntegerMode) {
1487 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1488 return;
1489 }
1490 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman29fa4262009-03-03 06:41:03 +00001491 break;
Chris Lattnerdc789562008-06-27 22:18:37 +00001492 }
1493
Eli Friedman29fa4262009-03-03 06:41:03 +00001494 if (ComplexMode) {
1495 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerdc789562008-06-27 22:18:37 +00001496 }
1497
1498 // Install the new type.
1499 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1500 TD->setUnderlyingType(NewTy);
1501 else
1502 cast<ValueDecl>(D)->setType(NewTy);
1503}
Chris Lattnera72440d2008-06-29 00:23:49 +00001504
Anders Carlssonf607f532009-02-13 06:46:13 +00001505static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1506 // check the attribute arguments.
1507 if (Attr.getNumArgs() > 0) {
1508 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1509 return;
1510 }
Anders Carlsson73007792009-02-13 08:11:52 +00001511
Anders Carlssoneb12e782009-02-19 19:16:48 +00001512 if (!isFunctionOrMethod(d)) {
Anders Carlssonf607f532009-02-13 06:46:13 +00001513 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001514 << Attr.getName() << 0 /*function*/;
Anders Carlssonf607f532009-02-13 06:46:13 +00001515 return;
1516 }
1517
Chris Lattner16ded572009-03-04 06:34:08 +00001518 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssonf607f532009-02-13 06:46:13 +00001519}
1520
Anders Carlssoneb12e782009-02-19 19:16:48 +00001521static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1522 // check the attribute arguments.
1523 if (Attr.getNumArgs() != 0) {
1524 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1525 return;
1526 }
1527
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001528 if (!isa<FunctionDecl>(d)) {
Anders Carlssoneb12e782009-02-19 19:16:48 +00001529 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001530 << Attr.getName() << 0 /*function*/;
Anders Carlssoneb12e782009-02-19 19:16:48 +00001531 return;
1532 }
1533
Chris Lattner16ded572009-03-04 06:34:08 +00001534 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlssoneb12e782009-02-19 19:16:48 +00001535}
1536
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001537static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner429e56a2009-04-14 16:30:50 +00001538 // check the attribute arguments.
1539 if (Attr.getNumArgs() != 0) {
1540 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1541 return;
1542 }
1543
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001544 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1545 if (Fn == 0) {
Chris Lattner429e56a2009-04-14 16:30:50 +00001546 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001547 << Attr.getName() << 0 /*function*/;
Chris Lattner429e56a2009-04-14 16:30:50 +00001548 return;
1549 }
1550
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001551 if (!Fn->isInline()) {
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001552 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner2a3e43d2009-04-14 17:02:11 +00001553 return;
1554 }
1555
Douglas Gregor67e11442009-04-28 06:37:30 +00001556 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner429e56a2009-04-14 16:30:50 +00001557}
1558
Fariborz Jahanian97703432009-03-27 18:38:55 +00001559static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1560 // check the attribute arguments.
1561 if (Attr.getNumArgs() != 1) {
Eli Friedman404adca2009-03-27 21:06:47 +00001562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian97703432009-03-27 18:38:55 +00001563 return;
1564 }
Eli Friedman404adca2009-03-27 21:06:47 +00001565
Fariborz Jahanian97703432009-03-27 18:38:55 +00001566 if (!isFunctionOrMethod(d)) {
1567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001568 << Attr.getName() << 0 /*function*/;
Fariborz Jahanian97703432009-03-27 18:38:55 +00001569 return;
1570 }
Eli Friedman404adca2009-03-27 21:06:47 +00001571
1572 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1573 llvm::APSInt NumParams(32);
1574 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1575 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1576 << "regparm" << NumParamsExpr->getSourceRange();
1577 return;
1578 }
1579
Anton Korobeynikov8fcffeb2009-04-03 23:38:25 +00001580 if (S.Context.Target.getRegParmMax() == 0) {
1581 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman404adca2009-03-27 21:06:47 +00001582 << NumParamsExpr->getSourceRange();
1583 return;
1584 }
1585
Anton Korobeynikov5f079ba2009-04-04 10:27:50 +00001586 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov8fcffeb2009-04-03 23:38:25 +00001587 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1588 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman404adca2009-03-27 21:06:47 +00001589 return;
1590 }
1591
1592 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanian97703432009-03-27 18:38:55 +00001593}
1594
Chris Lattnera72440d2008-06-29 00:23:49 +00001595//===----------------------------------------------------------------------===//
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001596// Checker-specific attribute handlers.
1597//===----------------------------------------------------------------------===//
1598
1599static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1600 Sema &S) {
1601
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001602 QualType RetTy;
1603
1604 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1605 RetTy = MD->getResultType();
1606 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1607 RetTy = FD->getResultType();
1608 else {
1609 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1610 << Attr.getName() << 3 /* function or method */;
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001611 return;
1612 }
1613
Ted Kremenek4330d8d2009-05-13 21:07:32 +00001614 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1615 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1616 << Attr.getName();
1617 return;
1618 }
1619
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001620 switch (Attr.getKind()) {
1621 default:
1622 assert(0 && "invalid ownership attribute");
1623 return;
1624 case AttributeList::AT_cf_returns_retained:
1625 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1626 return;
1627 case AttributeList::AT_ns_returns_retained:
1628 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1629 return;
1630 };
1631}
1632
1633//===----------------------------------------------------------------------===//
Chris Lattnera72440d2008-06-29 00:23:49 +00001634// Top Level Sema Entry Points
1635//===----------------------------------------------------------------------===//
1636
Sebastian Redl6e67d9b2008-12-21 19:24:58 +00001637/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner703c52d2008-06-29 00:43:07 +00001638/// the attribute applies to decls. If the attribute is a type attribute, just
1639/// silently ignore it.
1640static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1641 switch (Attr.getKind()) {
Daniel Dunbar8716a012008-07-31 22:40:48 +00001642 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001643 case AttributeList::AT_address_space:
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +00001644 case AttributeList::AT_objc_gc:
1645 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner703c52d2008-06-29 00:43:07 +00001646 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001647 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1648 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbar0a2da712008-10-28 00:17:57 +00001649 case AttributeList::AT_always_inline:
1650 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek1ce91f22009-04-10 00:01:14 +00001651 case AttributeList::AT_analyzer_noreturn:
1652 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001653 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1654 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1655 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1656 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1657 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1658 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001659 case AttributeList::AT_ext_vector_type:
1660 HandleExtVectorTypeAttr(D, Attr, S);
1661 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001662 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1663 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001664 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001665 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001666 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1667 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1668 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001669
1670 // Checker-specific.
1671 case AttributeList::AT_ns_returns_retained:
1672 case AttributeList::AT_cf_returns_retained:
1673 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1674
Daniel Dunbar8716a012008-07-31 22:40:48 +00001675 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar97509722009-02-12 17:28:23 +00001676 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001677 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +00001678 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001679 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb88b4f72009-02-13 19:23:53 +00001680 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001681 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001682 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattnerd2c66552009-02-14 07:37:35 +00001683 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1684 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001685 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar42e41392009-03-06 06:39:57 +00001686 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001687 case AttributeList::AT_transparent_union:
1688 HandleTransparentUnionAttr(D, Attr, S);
1689 break;
Chris Lattnera9b98462009-02-14 08:09:34 +00001690 case AttributeList::AT_objc_exception:
1691 HandleObjCExceptionAttr(D, Attr, S);
1692 break;
Douglas Gregorfcb19192009-02-11 23:02:49 +00001693 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanian82f54962009-01-13 23:34:40 +00001694 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroffe1cecba2008-09-18 16:44:58 +00001695 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson244d99b2008-10-05 18:05:59 +00001696 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001697 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1698 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlsson677e38f2009-01-31 01:16:18 +00001699 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssonf607f532009-02-13 06:46:13 +00001700 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlssoneb12e782009-02-19 19:16:48 +00001701 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman404adca2009-03-27 21:06:47 +00001702 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson402735d2009-02-13 08:16:43 +00001703 case AttributeList::IgnoredAttribute:
Chris Lattnerc305c342009-04-25 18:44:54 +00001704 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson402735d2009-02-13 08:16:43 +00001705 // Just ignore
1706 break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001707 default:
Anders Carlssonf607f532009-02-13 06:46:13 +00001708 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner703c52d2008-06-29 00:43:07 +00001709 break;
1710 }
1711}
1712
1713/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1714/// attribute list to the specified decl, ignoring any type attributes.
1715void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1716 while (AttrList) {
1717 ProcessDeclAttribute(D, *AttrList, *this);
1718 AttrList = AttrList->getNext();
1719 }
1720}
1721
Chris Lattnera72440d2008-06-29 00:23:49 +00001722/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1723/// it, apply them to D. This is a bit tricky because PD can have attributes
1724/// specified in many different places, and we need to find and apply them all.
1725void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1726 // Apply decl attributes from the DeclSpec if present.
1727 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1728 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +00001729
Chris Lattnera72440d2008-06-29 00:23:49 +00001730 // Walk the declarator structure, applying decl attributes that were in a type
1731 // position to the decl itself. This handles cases like:
1732 // int *__attr__(x)** D;
1733 // when X is a decl attribute.
1734 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1735 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1736 ProcessDeclAttributeList(D, Attrs);
1737
1738 // Finally, apply any attributes on the decl itself.
1739 if (const AttributeList *Attrs = PD.getAttributes())
1740 ProcessDeclAttributeList(D, Attrs);
1741}